Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

PDF::API2 Page Dimensions Layout

by dimv (Novice)
on Aug 16, 2010 at 00:48 UTC ( [id://855178]=perlquestion: print w/replies, xml ) Need Help??

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

The project: Convert letter-sized PDFs to legal, add the job identifier and folio to the bottom left.

Of course my first thought was - a quick 5 minute perl script! Loaded up PDF::API2, read the docs, and got to coding. Below is the snippet we are concerned about:

#!perl # CLI arguments: # 0 = path to PDF to convert # 1 = job identifier, 5 digit string # 2 = folio start number, no default # include modules use PDF::API2; # declarations use constant mm => 25.4 / 72; use constant in => 1 / 72; use constant pt => 1; # load pdf from passed argument my $pdf = PDF::API2->open($ARGV[0]) || die("Unable to open PDF"); # font definitions my %font = ( Helvetica => { Bold => $pdf->corefont('Helvetica-Bold',-encoding => 'latin1 +' ), Roman => $pdf->corefont('Helvetica',-encoding => 'latin1' ), Italic => $pdf->corefont('Helvetica-Oblique',-encoding => 'lat +in1' ), }, Verdana => { Bold => $pdf->corefont('Verdana-Bold',-encoding => 'latin1' +), Roman => $pdf->corefont('Verdana',-encoding => 'latin1' ), Italic => $pdf->corefont('Verdana-Italic',-encoding => 'latin1 +' ), BoldItalic => $pdf->corefont('Verdana-BoldItalic',-encoding => + 'latin1' ), }, ); # get total number of pages my $pagenumber = $pdf->pages; # folio offset, if we dont want to start at 1 my $offset = int($ARGV[2]-1); for ($count=1; $count<=$pagenumber; $count++) { # get the current page my $page = $pdf->openpage($count); # resize the pdf to legal $page->mediabox('Legal'); $page->cropbox('Legal'); $page->bleedbox('Legal'); $page->trimbox('Legal'); $page->artbox('Legal'); # construct a text box my $text = $page->text; # define font, face, pointsize $text->font( $font{"Verdana"}{"Roman"}, 12/pt ); # font color (not background) $text->fillcolor("#000000"); # x/y coords, remember cartesian coordinates, left,bottom = 0,0 $text->translate( 74/pt, 3/pt ); # actual text to place, text_right is single line look at text_blo +ck for para's $text->text_right($ARGV[1] . "_" . ($count+$offset)); }

The problem: When we change the page dimensions to Legal, all the "space" is added at the top where as we want all the space to be at the bottom. This is to be expected, since I'm guessing it is just adding more to the pages x,y, and 0,0 being the bottom left. Meaning a larger page dimension would increase the page from the top-right and out.

How would one go about tackling this issue? I cannot change the existing pdf before it gets to me, though. Is there some function I'm missing to offset the current contents by the difference of letter and legal (increasing their 'y' so they all move up the page), or do I should I parse the PDF and completely rebuild it?

Thank you for any considerations, or opinions I should take to remedy this.

Replies are listed 'Best First'.
Re: PDF::API2 Page Dimensions Layout
by snoopy (Curate) on Aug 16, 2010 at 02:56 UTC
    Maybe, you could take a similar approach to Re: PDF page numbering?

    This involves rebuilding each of the pages and overlaying the original page using the importPageIntoForm method.

      That worked beautifully! Thank you so much! Here is the final code in case anyone is interested:

      #!perl # Command Line Arguments:: # 0 = path to original pdf # 1 = job identifier # 2 = folio start - must pass 1 # include modules use PDF::API2; # declarations use constant mm => 25.4 / 72; use constant in => 1 / 72; use constant pt => 1; # load pdf from passed argument my $pdf = PDF::API2->open($ARGV[0]) || die("Unable to open PDF"); # new pdf to hold legal size my $pdf_out = PDF::API2->new; # font definition my %font = ( Verdana => { Bold => $pdf_out->corefont('Verdana-Bold',-encoding => 'lati +n1' ), Roman => $pdf_out->corefont('Verdana',-encoding => 'latin1' ) +, Italic => $pdf_out->corefont('Verdana-Italic',-encoding => 'la +tin1' ) } ); # get total number of pages my $pagenumber = $pdf->pages; # folio offset my $offset = int($ARGV[2]-1); for ($count=1; $count<=$pagenumber; $count++) { # get the current page/new page my $page = $pdf->openpage($count); my $page_out = $pdf_out->page(0); # resize new pdf page to legal $page_out->mediabox('Legal'); $page_out->cropbox('Legal'); $page_out->bleedbox('Legal'); $page_out->trimbox('Legal'); $page_out->artbox('Legal'); # turn old pdf into graphic # import into new pdf at offset my $gfx = $page_out->gfx; my $xo = $pdf_out->importPageIntoForm($pdf, $count); # 612x1008 = legal # 612x792 = letter # y diff is 216 $gfx->formimage($xo, 0, 216, # x y 1); # scale # add our folio identifier # construct a text box my $text = $page_out->text; # define font, face, pointsize $text->font( $font{"Verdana"}{"Roman"}, 12/pt ); # font color (not background) $text->fillcolor("#000000"); # x/y coords, remember cartesian coordinates, left,bottom = 0,0 # for a 8.5x11 doc, the bottom left viewable (not in bleed/crop ar +ea) is approx 68,0 ? $text->translate( 74/pt, 3/pt ); # actual text to place, text_right is single line look at text_blo +ck for para's $text->text_right($ARGV[1] . "_" . ($count+$offset)); } # save and close $pdf_out->saveas("C:\\dp\\perlTest.pdf"); $pdf_out->end(); exit
Re: PDF::API2 Page Dimensions Layout
by talexb (Chancellor) on Aug 16, 2010 at 01:54 UTC

    In the PostScript universe, the origin (0,0) is at the bottom left, which is why the space is added at the top of the page. Whatever the page size becomes, the bottom left is always at the bottom left.

    Solving it would involve moving the origin up by 3.5*resolution in dots per inch, I guess -- but it's been about ten years since I used that module, so I'm not sure how that's done.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: PDF::API2 Page Dimensions Layout
by aquarium (Curate) on Aug 16, 2010 at 03:35 UTC
    Is it possible to set page to center horizontally and vertically? In which case one would embed the existing letter size pdf page into the larger size page and set center for the wrapping page.
    the hardest line to type correctly is: stty erase ^H

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://855178]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found