17
Apr/090
Apr/090
Fix string encoding when manipulating PDFs with PDF::Reuse in perl
Writing a perl based application I needed to add some text to a PDF template file. For this task I use the module PDF::Reuse. I had some problem with umlauts and the string encoding which were added dynamically to the PDF with the prText function.
sub create_pdf { my ($sourceFile, $targetFile, $name1, $name2) = @_; # Output file prFile($targetFile); prCompress(1); prFont('Arial'); prFontSize(9); prMbox(0, 0, 420, 270); prForm({ file => $sourceFile, page => 1, rotate => -90, x => 0, y => 275 }); prText(140, 225, $name1); prText(140, 208, $name2); prEnd(); }
For example when I added the string Blä to the PDF file I got BlXXX in the rendered PDF.
create_pdf('template.pdf', 'target.pdf', 'Blä', 'Blä');
To fix this issue I needed to decode the UTF strings before putting them in the PDF. For this task I the used utf::decode function.
Adding the following code before using the prText() functions fixes the problem.
# Decode name to get umlauts shown utf8::decode($name1); utf8::decode($name2);



























