Joe_ has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks.
I've recently started learning Perl/Tk and it gave me all sorts of crazy ideas for applications I'd like to develop. For one of these ideas, I'd like to know if it's possible to have a widget open a PDF file within the GUI front-end. I mean, a sort of preview window showing me the contents of a PDF file. I tried Google but no joy... If that's not possible within Perl/Tk, are there other (preferably portable) ways of doing this?

Replies are listed 'Best First'.
Re: Including a PDF in Perl/Tk
by zentara (Cardinal) on Jun 26, 2012 at 10:24 UTC
    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

      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

      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
Re: Including a PDF in Perl/Tk
by Joe_ (Beadle) on Jun 26, 2012 at 10:36 UTC

    It seems that I can embed a window inside a toplevel widget as long as I know that window's id. I tried using the ID obtained from xwininfo, but the top-level window doesn't even show up in my application. I get an error saying that I can't set both the 'container' and 'use' options!

      Hi, embedding will only work if the program supports the -into-xid option. I looked, and xpdf does not have that option available. If you can find a pdf viewer that does, then the following node shows how to embed it... embedding xterm into a Tk app. You can substitute your app for the xterm.

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

        I see... That's good to know; thanks.

Re: Including a PDF in Perl/Tk
by Anonymous Monk on Jun 26, 2012 at 04:45 UTC

      Thank you for the suggestions. I have looked into PDF::Tk but it seems to spit out the binary contents of the PDF, which obviously won't work.

Re: Including a PDF in Perl/Tk
by Joe_ (Beadle) on Jul 06, 2012 at 00:49 UTC

    For anyone who's still following this thread, I've found some new information. It seems that there is/used to be a Tcl/Tk widget called PDF Canvas. I checked CPAN and there seems to be no Perl port of it.

    I'm still hoping to find a way to embed the Cairo surface into a Tk widget. If I do, I'll let you know here.