in reply to Including a PDF in Perl/Tk

There is some work in Gtk2 to use the Poppler library to display pdfs, but on Tk, if pdftk dosn't work for you, I would suggest using ImageMagick to convert the pdf to png or jpg, then use Tk to display it. That won't be perfect as links and paging will be lost. Another suggestion is to fork open xpdf from Tk, in it's own window.

If all you want is a preview window, then converting with ImageMagick to a thumbnail might be good. See Conver PDF to Image( JPG , JPEG , etc. ) and here is a simple converter. You can use IM blobs to avoid writing temp files, if needed. There are ways for Tk to directly display IM blobs; see Tk and ImageMagick

#!/usr/bin/perl use warnings; use strict; use Image::Magick; my $img = Image::Magick->new; $img->Read('zzimage.pdf'); $img->Rotate(-90); #if portrait/lanscape causes problem $img->set('magick'=>'jpeg'); $img->Write(filename=>'zzzzimage.jpg'); __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Including a PDF in Perl/Tk
by Joe_ (Beadle) on Jun 26, 2012 at 10:38 UTC

    Forking xpdf or okular from within Tk seems a better option for me. Can you please offer me hints (or point me to the proper documentation) on how I can do this? Or maybe I misunderstood and you actually mean opening an xpdf window independent of Perl altogether? This obviously won't work because I want it to be tied to my GUI.

      Well, this fork is still controlled by Tk, in that you can open and kill it, so it is still tied to the GUI. This example is as simple as they come, and there may be other pdf viewers which allow you to open them with a piped open, allowing you to send load and control signals to their STDIN, but I havn't found one like that yet. Probably a Poppler based viewer would be needed for that. However, this method, shown below, would be quite usable. You could make thumbnails of your pdf's as I described earlier, then have click blindings on each thumbnail to open that pdf with xpdf. Xpdf works quite well from my experience. You could even open multiple pdf's simultaneously with this method, by storing filenames and pids in a hash, but I will leave that complexity to you. :-)
      #!/usr/bin/perl -w use Tk; use strict; use Proc::Killfam; my $mw = MainWindow->new; my $pid; # global for killing viewer my $pdf = 'z.pdf'; my $button = $mw->Button(-text => 'Open PDF Viewer', -command => [\&xpdf_test, $pdf] )->pack; my $button1 = $mw->Button(-text => 'Close PDF Viewer', -command => \&xpdf_close )->pack; MainLoop(); sub xpdf_test { my $pdf = shift; $pid = fork; if ( $pid < 0 ) { die "Fork failed: $!\n" } elsif ( $pid == 0 ) { # forked code system ("xpdf -g '400x500+100+100' $pdf"); } } sub xpdf_close{ killfam 9, $pid; }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        Thanks. I get the idea. It's not exactly what I wanted, but it's a useful trick nonetheless. I guess the best thing about it is that it can work with any application, not just PDF viewers...

Re^2: Including a PDF in Perl/Tk
by Joe_ (Beadle) on Jun 28, 2012 at 19:08 UTC

    I'd really like to thank you for bringing the Poppler library to my attention. A quick search revealed to me that there's a Perl binding for the Poppler library that can be used to render PDFs in conjunction with Cairo. If the Cairo surface can be embedded in a Canvas widget, then I've found exactly what I need! I will test this soon and let you know :). Thanks again!

      Thanks for directing me to the Poppler module. I'm going to test it out. In the meantime, I don't know about putting a CairoSurface on a Canvas ( I would use Goo::Canvas), but normally it is just put in a DrawingArea. I'll let you know if I figure a way with a Canvas. You might also want to ask on the gtk-perl maillist. The archives there may have an example too.

      UPDATE: changed Cairo code example to one that actually uses Poppler

      #!/usr/bin/perl use Cairo; use Gtk2 '-init'; use Glib qw(TRUE FALSE); use Poppler; my $path = 'file:///home/zentara/perlplay/Gtk2/cairo/z.pdf'; my $o = Poppler::Document->new_from_file($path); # or if you want to open the pdf in perl, or use some othe source: # open (PDF, "<FILE.PDF"); # read (PDF, my $data, -s "FILE.PDF"); # close (PDF); # my $o = Poppler::Document->new_from_data($data, length($data)); my $page = $o->get_page( 0 ); my $dimension = $page->get_size; warn $dimension->get_width; warn $dimension->get_height; my ( $width, $height ) = ( $dimension->get_width, $dimension->get_heig +ht ); my $window = Gtk2::Window->new( 'toplevel' ); $window->signal_connect( 'delete_event' => sub { Gtk2->main_quit; } ); my $surface = Cairo::ImageSurface->create ('argb32', $width, $height); my $cr = Cairo::Context->create ($surface); # make a background rectangle filled white so screenshot is legible $cr->rectangle( 0, 0, $width, $height ); $cr->set_source_rgb( 1, 1, 1 ); #white $cr->fill; $page->render_to_cairo( $cr ); $cr->show_page; # make a visible widget to display the CairoSurface my $drawable = Gtk2::DrawingArea->new; $drawable->size( $width, $height ); $drawable->signal_connect( 'expose_event' => \&on_expose_event, $surface ); $window->add( $drawable ); $window->show_all; # take a screenshot $surface->write_to_png ("$0.png"); Gtk2->main; sub on_expose_event { my ( $widget, $event, $surface ) = @_; my $cr = Gtk2::Gdk::Cairo::Context->create( $widget->window ); $cr->set_source_surface( $surface, 0, 0 ); $cr->paint; $cr->show_page; return FALSE; } __END__

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh