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 | |
by dimv (Novice) on Aug 16, 2010 at 04:21 UTC | |
Re: PDF::API2 Page Dimensions Layout
by talexb (Chancellor) on Aug 16, 2010 at 01:54 UTC | |
Re: PDF::API2 Page Dimensions Layout
by aquarium (Curate) on Aug 16, 2010 at 03:35 UTC |