in reply to Re: Including a PDF in Perl/Tk
in thread Including a PDF in Perl/Tk

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!

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