1
votes

I'm using CAM::PDF to merge several PDF files. This works fine. Now I need to add a stamp on each page using PDF::API2. This works fine for some pages, but not for others.

PDF files created with wkhtmltopdf seem to have their coordination system flipped and the scaling is off as well.

When running through the pages I add a stamp like this:

my $pdf2       = PDF::API2->open_scalar($pdf_data);
my $page_count = $pdf2->pages;
for my $i ( 1 .. $page_count ) {
    my $page    = $pdf2->openpage($i);
    my $content = $page->gfx();
    my $text    = $page->text();
    $content->linewidth(2);
    $content->rectxy( 5, 10, 140, 40 );
    $content->stroke;
    my $font = $pdf2->ttfont('calibri.ttf');
    $text->scale( 1.0, 1.0 );
    $text->font( $font, 12 );
    $text->translate( 10, 14 );
    $text->text( sprintf( 'PAGINA %d VAN %d', $i, $page_count ) );
    $text->translate( 10, 26 );
    $text->text('some ID');
}
my $pdf_data = $pdf2->stringify;

Now, the pages that are from the wkhtmltopdf have a tiny box with even tinier text in the top left corner (but inside the page margins) and it's mirrored. The non-wkhtmltopdf page has a properly sized box with properly sized text in the bottom left corner (ignoring page margins).

Using $content->scale and $content->rotate(180) I can properly display the stamp on the pages created by wkhtmltopdf. But then the other pages are messed up.

So, is there any way to make sure that each document has the same orientation, rotation and scale on all pages?

2
One strange observation is that when I set a DPI of 70 with wkhtmltopdf the size of the stamp seems to match. Still the text and coordinates seem to be mirrored.Htbaa
Also when I don't use CAM::PDF and just pass a single PDF document generated by wkhtmltopdf straight to PDF::API2 the same problem occurs.Htbaa
See this answer which solves this by using the importPageIntoForm method then overlaying new text and graphics.dwarring
@snoopy thanks I'll look into that.Htbaa

2 Answers

2
votes

++Htbaa has already answered HOW to get around this problem.

I've had a chance to look at wkhtmltopdf. This answer describes WHY it's output is causing problems.

I wrote a trivial /tmp/hw.html

<html>
    <body>hello world!</body>
</html>

Then created and uncompressed a pdf:

% wkhtmltopdf --version
Name:
  wkhtmltopdf 0.9.9
  ...
% xvfb-run wkhtmltopdf /tmp/hw.html /tmp/hw.pdf
% pdftk /tmp/hw.pdf output /tmp/hw1.pdf uncompress

Here's what the page content (object 8) looks like.

8 0 obj <</Length 751>>stream /GSa gs /CSp cs /CSp CS 0.060000000 0 0
-0.060000000 28.3200000 813.679999 cm q q Q Q q q Q q /CSp cs 0 0 0
scn /GSa gs Q Q q 0 0 m 8963.99983 0 l 8963.99983 345.507488 l 0
345.507488 l 0 0 l h W* n q /CSp cs 0 0 0 scn /GSa gs /CSp cs 1 1 1
scn /GSa gs q 9.59743022 0 0 9.59743022 0 0 cm 0 0 934 36 re f Q Q q
9.59743022 0 0 9.59743022 0 0 cm /CSp cs 0 0 0 scn /GSa gs 0 0 0 SCN 0
w 2 J 2 j [] 0 d q /CSp cs 0 0 0 scn /GSa gs BT /F7 16 Tf 1 0 0 -1 0 0
Tm 8 -24 Td <0001> Tj 9 0 Td <0002> Tj 9 0 Td <0003> Tj 4 0 Td <0003>
Tj 4 0 Td <0004> Tj 9 0 Td <0005> Tj 4 0 Td <0006> Tj 12 0 Td <0004>
Tj 9 0 Td <0007> Tj 5 0 Td <0003> Tj 4 0 Td <0008> Tj 9 0 Td <0009> Tj
ET Q Q Q q q 12 0 0 12 0 0 cm /CSp cs 0 0 0 scn /GSa gs 0 0 0 SCN 0 w
2 J 2 j [] 0 d Q Q

endstream endobj

The answer is in the very first instruction on the page /GSa gs is setting the graphics state, occurs before the first q instruction (save graphics state).

So the page is being left in an untidy graphics state. When further content is then added to the PDF by PDF::API2, it is using the altered state.

1
votes

The answer pointed out by @snoopy seems to fix the problem I'm experiencing. Text and image size of the stamp-pdf/overlay-pdf are the same size on all pages.