Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

PDF::Template and character encodings

by geektron (Curate)
on Oct 15, 2007 at 21:28 UTC ( #645033=perlquestion: print w/replies, xml ) Need Help??
geektron has asked for the wisdom of the Perl Monks concerning the following question:

I've googled, I've super-searched, yet I still can't find a working solution to a character set problem.

I have accented characters (á, for example) stored in a mysql database. HTML rendering is fine without converting the characters to HTMLEntities, but when I try to create a PDF with the same data and PDF::Template, I get a mangled "~Aj" combination.

From this google groups thread, setting a pdf_encoding in the xml template would be sufficient, e.g.:

<pdftemplate name="masterList" pdf_encoding='ISO-8859-1'>

But when I add pdf_encoding to my template, I get the following error:

PDFlib Error [1552] PDF_findfont: Font 'Helvetica' with encoding 'ISO-8859-1': Couldn't find encoding 'ISO-8859-1'

I've tried 'latin1', 'iso-8859-1', and 'utf-8' as valid encodings, but each one throws the above error (with the encoding substituted, of course).

I'm presuming the error is with PDFlib (it's throwing the error), but I'm hoping someone here has dealt with the same (or similar) issue and can give me a pointer or two.

EDIT 1) condensed template.xml

<pdftemplate name="masterList" pdf_encoding='iso8859-1'> <pagedef PAGE_WIDTH="612" PAGE_HEIGHT="792"> <font face="Helvetica" h="10"/> <LOOP NAME="ROWS"> <section> <row x='30' h="20"> <LOOP NAME='COLUMN'> <textbox w="50%"><VAR NAME="FIRST_NAME"/> <VAR NAME="LAST_NAM +E"/></textbox> </LOOP> </row> </section> </LOOP> </pagedef> </pdftemplate>
The template is just fed the needed (nested) array of hashrefs to properly populate the VARs

Replies are listed 'Best First'.
Re: PDF::Template and character encodings
by stvn (Monsignor) on Oct 15, 2007 at 22:35 UTC

    I don't know about with PDF::Template, but when I use PDFLib, I do this in order to make accented characters like that work:

    my $pdf = pdflib_pl::PDF_new(); pdflib_pl::PDF_set_parameter($pdf, "textformat", "utf8");
    of course this means that the text you are inserting must be utf8 and not Latin-1, but Encode can take care of that for you.

    Now as for how to make it all work with PDF::Template, I suggest you do a little source diving, the code is readable and the internals are very consistent so it should be pretty obvious where you would need to patch stuff.

      I actually have already tried source diving in PDF::Template. The patch mentioned in the originally referenced thread didn't get me any closer to resolving the problem, and I don't see any way to pass raw PDFlib::* values to PDF::Template. I really hate making modifications to the source itself; such changes get lost when/if the module gets updated.
        I really hate making modifications to the source itself; such changes get lost when/if the module gets updated.

        Push it back upstream to dragonchild, I suspect if you include a test he would accept the patch.

Re: PDF::Template and character encodings
by shmem (Canon) on Oct 15, 2007 at 21:45 UTC
    qwurx [shmem] ~ > xlsfonts | grep -i helvetica | grep 8859 -adobe-helvetica-bold-o-normal--10-100-75-75-p-60-iso8859-1 -adobe-helvetica-bold-o-normal--10-100-75-75-p-60-iso8859-15 -adobe-helvetica-bold-o-normal--10-100-75-75-p-60-iso8859-2 ...

    Did you try with iso8859-1?


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      i just did, and not only did that setting mangle the PDF output, it didn't address the root problem ... :-\
        That might actually be a sign of progress. Is the PDF_findfont error still thrown? If it isnt, if it's different, there might be something wrong with your input files.

        Do you mind posting a condensed example showing the problem? It's far easier to help if there's something to see, and not being forced at stabbing blindfolded into the fog (guessing wildly, that is).


        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://645033]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-02-21 17:35 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (314 votes). Check out past polls.