aloss has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I'm a Perl newbie and have an issue with the PDF::API2. I need to create some formatted text and so I want to use this library to create a PDF document. Being a German, I need to deal with some special characters ("Umlauts"). I tried a while around but whatever I tried, all gives the same result, see below.

The essential part of the code I use:

#!/usr/local/ActivePerl-5.16/bin/perl -w use strict; use warnings; use PDF::API2; # Prepare PDF my $pdf = PDF::API2->new( -file => "Test.pdf"); my $page = $pdf->page; $page->mediabox( 'A4' ); # Set the font - but none of these do any difference: my $font = $pdf->corefont('Helvetica', -encoding => "utf-8"); #my $font = $pdf->corefont('Helvetica', -encoding => "UTF8"); #my $font = $pdf->corefont('Helvetica', -encoding => "UTF-8"); #my $font = $pdf->corefont('Helvetica', -encoding => "utf8"); #my $font = $pdf->corefont('Verdana'); #my $font = $pdf->ttfont('Arial.ttf'); # This even gives me an error d +uring compilation # Set up some text my $text = $page->text(); $text->font($font, 20); $text->translate(100, 700); $text->text("Umlaut: Ä ä Ö ö Ü ü ß"); $pdf->save; $pdf->end();

The output in the PDF looks like:

Umlaut: € Š ... š † Ÿ §

The system I use is a mac OS X 10.9.5 and ActivePerl 5.16, the PDF::API2 lib is version 2.023.

Any help appreciated!

Replies are listed 'Best First'.
Re: PDF::API2 with special characters
by Anonymous Monk on Oct 06, 2014 at 12:39 UTC

    You've got umlauts directly in your source - how is your Perl source file encoded? If it's UTF-8, you need to tell Perl that by saying use utf8;. Or, try using the hex escapes, e.g. "\x{00E4}" for a-umlaut.

    (Disclaimer: I don't have experience with PDF::API2.)

      > You've got umlauts directly in your source

      Yes. What's the alternative? In the original source I used a variable instead - same issue.

      > how is your Perl source file encoded?

      How can I find this out? "file" just tells me it's a 'shell text executable'. The editor (Komodo) doesn't ask me when saving.

      >If it's UTF-8, you need to tell Perl that by saying use utf8;.

      I tried 'use utf8;' but then I get compile errors "Malformed UTF-8 character (unexpected continuation byte 0x80, with no preceding start byte) at ./Test3.pl line 25."

      > Or, try using the hex escapes, e.g. "\x{00E4}" for a-umlaut.

      You mean I need to replace all Umlauts by hex escapes? Honestly, this is not an option.

        Yes. What's the alternative?

        You can use non-ASCII characters in your Perl source as long as you know what encoding the file is in, and you tell Perl. My suggestion would be to save the file as UTF-8 and add use utf8;.

        The editor (Komodo) doesn't ask me when saving.

        A quick Google search offers the following solution to change the encoding of a file: "right click the file in the left pane, select property, In the File Preferences, find File Settings section, and you see the Encoding selection menu." (https://community.activestate.com/node/10558)

        You mean I need to replace all Umlauts by hex escapes?

        No, that was meant as a test: If you use hex escapes, and your problems go away, then you know the issue was the encoding of the Perl source file. If they don't go away, the source is likely PDF::API2, or, if you're getting your text from a source outside of the Perl source, that source.