Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

PDF::API2 text problem

by samwalker04 (Initiate)
on Aug 30, 2012 at 23:05 UTC ( #990872=perlquestion: print w/replies, xml ) Need Help??
samwalker04 has asked for the wisdom of the Perl Monks concerning the following question:

My application receives a PDF that the user uploads, then I use PDF::API2 to open the file, print a few words in 10 point font at the top left corner of the first page of the PDF, then save it to a folder. Pretty simple.

I figured out how to do this by trial and error, and it has worked fine for months, but I recently found a problem. A user uploaded a PDF and the text was printed on the first page but instead of the top left corner in 10 point font, it was printed in the middle of the page in tiny microscopic font.

What was different about this PDF? Well it was PDF Version 1.3, which is pretty old, so I thought maybe that was the problem. But then I tested a different 1.3 PDF and it worked fine.*

*It worked fine only when I commented out the code in the API2 module that checks PDF version and actually returns an error when I try to work with a PDF of version 1.3. I have no idea why this code doesn't give an error when I upload the original, problematic 1.3 PDF. I am leaving this code commented out because I really need to be able to process older PDFs if possible.

In summary, I have no idea why my code is putting my text in the wrong place and at the wrong size for this one document that happens to be PDF Version 1.3 (Don't know if that is even relevant). Here is my ugly code that may be fraught with errors, please help!

use PDF::API2; my $file = shift; #receive input file as param my $pdf = PDF::API2->open($file); my $page = $pdf->openpage(1); #open the first page my $font = $pdf->corefont('Helvetica-Bold'); my $text = $page->text(); $text->font($font,10); #10-point font size $text->translate(15,770); #top left corner.. 0,0 origin starts in the +bottom left corner $text->text('blah blah sample text'); $pdf->saveas($file);

Replies are listed 'Best First'.
Re: PDF::API2 text problem
by snoopy (Deacon) on Aug 30, 2012 at 23:32 UTC
    I've found that directly inserting text into a PDF can be fraught with danger. I suspect that the text/graphics state has been left in an altered state; I.E. its undergone rotation or translation etc. I've found it more robust to recreate each page in the PDF and import each page as a form element. Then output any new text or graphics.
    #!/usr/bin/perl use warnings; use strict; use PDF::API2; my $file = shift (@ARGV); my $pdf_in = PDF::API2->open($file); my $pdf = PDF::API2->new; my $pagenum = 1; my $page_in = $pdf_in->openpage($pagenum); my $page = $pdf->page(0); $page->mediabox($page_in->get_mediabox); $page->trimbox($page_in->get_trimbox); my $gfx = $page->gfx; my $xo = $pdf->importPageIntoForm($pdf_in, $pagenum); $gfx->formimage($xo, 0, 0, # x y 1); # scale my $font = $pdf->corefont('Helvetica-Bold'); my $text = $page->text(); $text->font($font,10); #10-point font size $text->translate(15,770); #top left corner.. 0,0 origin starts in the +bottom left corner $text->text('blah blah sample text'); $pdf->saveas($file);
      Thanks for the suggestion! Seems like that should work, as long as I wrap most of that code in a loop to make sure I import every page of the PDF, not just the first page. I'll give this a shot tomorrow and report back my results.
        It's working great. I simply wrapped the code in a while loop to process every page, but only stamp the new text on the first page. Thank you so much for your help.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990872]
Front-paged by Corion
[holli]: hessenrap...
[holli]: love it :)
[Corion]: acapella hessian rap, but they also do melodic stuff, like Shout (Tears for Fears)
[karlgoethebier]: Rap rhymes to crap. Ouch!
[karlgoethebier]: Unne hip unne hop unne schoppe inne kopp...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2017-11-18 19:33 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.