use warnings; use strict; use Tk; my $mw = tkinit; my $c = $mw->Canvas()->pack; $c->createText(20,20, -text => "some text to test\nand one more line", -font => ['Helvetica'], -anchor => 'w', ); $mw->Button(-text => 'print', -command => sub{print_canvas( $c )}, )->pack; MainLoop; sub print_canvas{ my $c = shift; # draw a mark to enforce a minimum printing area $c->createLine qw/15c 20c 15.1c 20c -fill white/; #calculate printing area, add margin to width and height my @bbox = $c->bbox('all'); my $width = $bbox[2] - $bbox[0] + 50; my $height = $bbox[3] - $bbox[1] + 90; my $ps = $c->postscript( -height => $height, -width => $width, -x => '-1c', -y => '-1c', ); my $gs_cmd = 'gswin32c.exe'; my $gs_options = '-sDEVICE=mswinpr2 -dEPSFitPage'; open my $writeme, "|-","$gs_cmd $gs_options -" or die "Can't open pipe to gs: $!"; print $writeme $ps or die " Error in printing to pipe: $!"; close $writeme or die " Error in closing pipe: $!";; }