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

Hi everyone,
I have some problems about the postscript method that I cannot figure out. Does anyone know about it and could help me fix it ?
Now, I have a canvas with a certain items on it and all I want to do is to use the postscript method to dump it out and send the ps file to the printer.
What I expect to have is a single or multiple pages printed out but What I get was a blank page. Here is the program :
#!perl use warnings; use strict; use Tk; my $mw=MainWindow->new(-title=>"Print out the canvas"); my c=$mw->Scrolled("Canvas")->pack(-expand=>1,-fill=>'both'); my $canvas=$c->Subwidget("canvas"); $canvas->createLine(100,100,20,20,-arrow=>'both',-arrowshape=>[10,10,1 +0]); $canvas->createOval(100,80,65,75,-fill=>"blue"); $canvas->createRectangle(30,50,60,80, -fill=>"red",-width=>5); $canvas->createText(150,1100,-text=>"check postscript",-justify=>"left +"); $canvas->createOval(150,1150,300,1300,-fill=>"blue"); $canvas->createRectangle(150,1300,400,1350, -fill=>"red",-width=>5); $canvas->configure(-scrollregion=>[$canvas->bbox("all")]); $canvas->update(); my $button=$canvas->Button(-text=>"Print",-command=>\&buttonDo) ->pack(-side=>"bottom"); MainLoop; sub buttonDo{ $canvas->update; my @coord=$canvas->bbox('all'); my $maxwidth=$coord[2]; my $maxheight=$coord[3]; $canvas->postscript( '-x'=>'0','-y'=>'0', -colormode=>'color', -file=> "file.ps", -rotate=>0, -width=>$maxwidth, -height=>$maxheight, -pageheight=>'10.5 i', -pagewidth=>'8 i'); system("lpr -S serverN-P printerN file.ps") == 0 or die $!; }
I suppose to get all the items of my canvas and then dump them out with the postscript method because I already used $canvas->bbox().
I tried to use pageheight and pagewidth to scale all my items in one page 8x11 inches but it seems not to work somehow.
I also used width and height options to specify the canvas surface that I want to print but all I get is blank.
Did I do something wrong ?
or is it a problem of the resolution of my printer since it cut all the items out the standard frame before printing?
Thanks so much.

Replies are listed 'Best First'.
Re: $canvas->postscript method - unexpected result
by zentara (Cardinal) on Jun 25, 2008 at 12:00 UTC
    I tried your script, and the page is blank because the postscript anchor is centered by default, and your items are at the extreme top and bottom. Additionally, if you read the perldoc for the postscript method, it states if BOTH pageheight and pagewidth are specified, pagewidth is only used..... that lops off your top and bottom from view. The following works for me, on my smaller screen.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw=MainWindow->new(-title=>"Print out the canvas"); my $c=$mw->Scrolled("Canvas")->pack(-expand=>1,-fill=>'both'); my $canvas=$c->Subwidget("canvas"); $canvas->createLine(100,100,20,20,-arrow=>'both',-arrowshape=>[10,10,1 +0]); $canvas->createOval(100,80,65,75,-fill=>"blue"); $canvas->createRectangle(30,50,60,80, -fill=>"red",-width=>5); $canvas->createText(150,1100,-text=>"check postscript",-justify=>"left +"); $canvas->createOval(150,1150,300,1300,-fill=>"blue"); $canvas->createRectangle(150,1300,400,1350, -fill=>"red",-width=>5); $canvas->configure(-scrollregion=>[$canvas->bbox("all")]); $canvas->update(); my $button=$canvas->Button(-text=>"Print",-command=>\&buttonDo) ->pack(-side=>"bottom"); MainLoop; sub buttonDo{ $canvas->update; my @coord=$canvas->bbox('all'); my $maxwidth=$coord[2]; my $maxheight=$coord[3]; $canvas->postscript( '-x'=>'0','-y'=>'0', -colormode=>'color', -file=> "$0.ps", -rotate=>0, -width=>$maxwidth, -height=>$maxheight, -pageanchor => 'n', -pageheight=>'8.0 i', #to fit my screen ); # system("lpr -S serverN-P printerN file.ps") == 0 or die $!; }

    I'm not really a human, but I play one on earth CandyGram for Mongo
      I tried what you suggested but I can only print out the items on the top left corner and loose everything below them which are my 3 last items. How come it works for you and not for me?
      Maybe it's a problem of my printer, I'm not sure.
      Another point is the max height here is 1353 which is larger than my screen (8 i ~ 764 pixels) I suppose, so I think the pageheight option doesn't do its job to scale everything in my screen. That maybe explains why it doesn't work.
      I think of using a loop to split my canvas into multiple parts and dump them out accordingly like this :
      for (my $i='1'; $i<$maxheight; $i+=850){ ++$currentPage;print "$currentPage\n"; $canvas->postscript('-x'=>'0', '-y'=>$i, -width=>$maxwidth, -height=>850, -colormode=>'color', -file=>"$i.ps", -pageanchor=>'n', -pageheight=>'10.5 i'); system("lpr -S ilfpsc6vs01 -P nw1h305c $i.ps") == 0 or die $!; }
      What do you think?
      I know it seems goofy. The problem is I have to pipe every single ps file to the printer. I hope there might have a way to do this better.
        I'm not sure, I don't use postscript too often except to get full output and convert to a graphic like jpg, which I can scale. The pageheight option is supposed to scale things down to the page height (with a consequential width scaling). By experiment, I had to go down to 8 to see it all, even though my screen can handle 10. Maybe you need to experiment or check the settings for your printer..... maybe you have a "letter vs. A4" issue, a "landscape vs portrait issue", or something similar?

        You can always use ImageMagick to convert the full postscript to jpg(png, etc), then scale it to fit your printer's output. Sorry, I'm just not much of a printer expert. ;-(


        I'm not really a human, but I play one on earth CandyGram for Mongo
Re: $canvas->postscript method - unexpected result
by Anonymous Monk on Jun 25, 2008 at 09:06 UTC
    You have typo my c should be my $c and
    system("lpr -S serverN-P printerN file.ps") == 0 or die $!; should be
    #system("lpr -S serverN-P printerN file.ps") == 0 or die $!;