PhilterPaper / Perl-PDF-Builder

Extended version of the popular PDF::API2 Perl-based PDF library for creating, reading, and modifying PDF documents
https://www.catskilltech.com/FreeSW/product/PDF%2DBuilder/title/PDF%3A%3ABuilder/freeSW_full
Other
6 stars 7 forks source link

Distorted text in PDFs created with browsers #168

Closed Tekki closed 3 years ago

Tekki commented 3 years ago

PDF::Builder 3.022, Perl 5.34.0 on Debian

If text is added to a PDF created with one of the following browsers it appears distorted (wrong size, mirrored):

Not affected: Firefox.

How to reproduce:

#!/usr/bin/env perl
use strict;
use warnings;

use PDF::Builder;

my $file = $ARGV[0] || 'download.pdf';

my $pdf  = PDF::Builder->open($file);
my $page = $pdf->openpage(1);

my $font = $pdf->corefont('Helvetica-Bold');
my $text = $page->text();
$text->font($font, 12);
$text->translate(200, 200);
$text->text('Abcde');

$pdf->saveas("stamped_$file");
PhilterPaper commented 3 years ago

This sounds like it's a known issue with the Chrome engine. In certain circumstances, such as adding headers and footers to a page, it (Chrome) sets up the coordinate system vertically reversed (0 at top, about 3300 units high). Unfortunately, it leaves the PDF's coordinate system in this state, so anything you add to it will be mirrored. The solution is to add a PDF command to your text stream to reset the coordinate system to the standard orientation and size, before adding any more new content.

See in the POD for "Content.pm" a discussion of using $content->add() to add a cm command to fix the scaling and orientation. Let me know if it does the job for you.

Tekki commented 3 years ago

To add the sample code from the Content.pm docs definitely solves my issue. I found a second case in PDFs with producer 'Microsoft: Print To PDF', where the scale has to be set to approximately 0.75. Thanks a lot for your feedback and your help!