Gtk2 Printing and HTML

2010-08-06 Thread Nyall
On the topic of printing using perl and gtk2, I'm wondering if anyone
has any tips or suggestions on how to print a html page? Are there any
good modules for rendering html to a cairo surface? I've done a bit of
searching on this in the past, and ran across a few command-line tools
for converting html to pdf, which could then be loaded into the
program -- but I'm really hoping for something a bit more elegant than
this.

Regards,
Nyall Dawson
___
gtk-perl-list mailing list
gtk-perl-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list


Re: Gtk2 Printing and HTML

2010-08-06 Thread zentara
On Fri, 6 Aug 2010 16:50:20 +1000
Nyall ny...@zombiepigs.net wrote:

On the topic of printing using perl and gtk2, I'm wondering if anyone
has any tips or suggestions on how to print a html page? Are there any
good modules for rendering html to a cairo surface? I've done a bit of
searching on this in the past, and ran across a few command-line tools
for converting html to pdf, which could then be loaded into the
program -- but I'm really hoping for something a bit more elegant than
this.

Hi, been away for awhile, but recently I used Gtk2::Webkit with success.

It will allow you to display a web page in a native Gtk2 window, which you then
could save with the normal window-to-pixbuf code.

This is a crude example, but it does show how to detect full loading of each
url.

good luck, :-)
zentara

#!/usr/bin/perl
use strict;
use warnings;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;
use Gtk2::WebKit;


my $url = shift || 'http://www.youtube.com/watch?v=9HIybxmMerA';


my $win = Gtk2::Window-new;
$win-set_default_size(800, 600);
$win-signal_connect(destroy = sub { Gtk2-main_quit });

my $view = Gtk2::WebKit::WebView-new;
$view-signal_connect( 'notify::progress' = \notify_progress, undef );
$view-signal_connect( 'load_finished' = \load_finished, undef );
my $sw = Gtk2::ScrolledWindow-new;
$sw-add($view);


my $vbox = Gtk2::VBox-new( FALSE, 6 );
$vbox-set_size_request(0,0);
$win-add($vbox);
$vbox-set_border_width(2);


$vbox-pack_start( $sw,TRUE, TRUE, 0 );
$vbox-pack_start (Gtk2::HSeparator-new, FALSE, FALSE, 0);

my $button = Gtk2::Button-new_from_stock('gtk-quit');
$vbox-pack_start( $button, FALSE, FALSE, 0 );
$button-signal_connect( clicked = \delete_event );

my $button1 = Gtk2::Button-new_from_stock('Screenshot');
$vbox-pack_start( $button1, FALSE, FALSE, 0 );
$button1-signal_connect( clicked = \screenshot );

$win-show_all;

$view-open($url ); 

Gtk2-main;

sub notify_progress{
   my $load_progress = $view-get('progress');
print $load_progress\n;
}

sub load_finished{
  print load complete\n;
 # screenshot();  # do auto screenshot on full load
return 0;
}

#
sub delete_event {
Gtk2-main_quit;
return FALSE;
}  
#
sub screenshot{

#we are going to save the $view
my ($width, $height) = $view-window-get_size;

# create blank pixbuf to hold the image
my $gdkpixbuf = Gtk2::Gdk::Pixbuf-new ('rgb',
0,
8,
$width,
$height);

$gdkpixbuf-get_from_drawable ($view-window, 
 undef, 0, 0, 0, 0, $width, $height);

#only jpeg and png is supported  it's 'jpeg', not 'jpg'
$gdkpixbuf-save ($0.jpg, 'jpeg', quality = 100);

print screenshot\n;

return FALSE;
}

#$pixbuf-save ($filename, 'jpeg', quality = '100');
#  Currently only a few parameters exist.  JPEG images can be saved
#  with a quality parameter; its value should be in the range
#  [0,100].  Text chunks can be attached to PNG images by specifying
#  parameters of the form tEXt::key, where key is an ASCII string of
#  length 1-79.  The values are UTF-8 encoded strings.  
__END__





___
gtk-perl-list mailing list
gtk-perl-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list


Re: Gtk2::PrintOperation

2010-08-06 Thread Jeffrey Ratcliffe
On 5 August 2010 23:19, Mario Kemper mario.kem...@googlemail.com wrote:
 This should work:

Thanks for the help. This line got me one step further:

  Gtk2::Gdk::Cairo::Context::set_source_pixbuf( $cr, $pixbuf, 0, 0 );

Why isn't set_source_pixbuf in Cairo::Context?

I was still getting a PDF where the image scaling was mostly such that
it was so far off the paper that nothing was visible. I found this,
though:

http://www.gtkforums.com/about6520.html

which works. My working callback:

 $op-signal_connect( draw_page = sub {
  my ($op, $context) = @_;
  my $cr = $context-get_cairo_context;

  # Context dimensions
  my $pwidth  = $context-get_width;
  my $pheight = $context-get_height;

  # Image dimensions
  my $pixbuf = Gtk2::Gdk::Pixbuf-new_from_file( $slist-{data}[0][2] );
  my $iwidth  = $pixbuf-get_width;
  my $iheight = $pixbuf-get_height;

  # Scale context to fit image
  my $scale = $pwidth / $iwidth;
  $scale = $pheight / $iheight if ( $pheight / $iheight  $scale );
  $cr-scale( $scale, $scale );

  # Set source pixbuf
  Gtk2::Gdk::Cairo::Context::set_source_pixbuf( $cr, $pixbuf, 0, 0 );

  # Paint
  $cr-paint;

  return;
 } );
___
gtk-perl-list mailing list
gtk-perl-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list