17
Apr/09
0

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);
Filed under: Perl
9
Feb/09
1

Handing timeouts in perl scripts

Perl Writing some Nagios check plugins I wrote some code to catch timeouts. These timeouts can occur for several reasons. Mostly some network connections via SSH, SNMP or HTTP-Gets which run longer than the default check timeout in Nagios. I usually use a global check timeout of 15 seconds in Nagios. This means a check script which runs more than 15 seconds will be killed. In this case Nagios returns a CRITICAL state with a “(Service check timed out)” message. This is bad cause I don’t want those timeouts to result in CRITICAL state and some CRITICAL notifications.

Now read how to catch such timeouts…

Filed under: Perl