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);

In reply to PDF::API2 text problem by samwalker04

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.