Hi Monks,

Below is a cut-down version of my code which allows me to enter characters like e-acute (by typing Alt-130) onto a web form, and it writes them back to the web page OK, but when it writes it to a PDF, the acute chars don't appear correctly.  I've worked around this for a couple of chars with the "quick hack" you can see in the code, but I'd like to have a proper fix for all special chars.  I'm guessing I might need to "use utf8", or something, and I have read some articles on Unicode & UTF8, but haven't worked out what I need to do here yet.

How can I get this code to write the special chars to the PDF file correctly, without the "quick hack"?

Also, am I supposed to have:

Content-Type: text/html; charset=utf-8\n
AND
<meta charset='UTF-8'>
Or what?  Seems a bit duplicated.
#!/usr/bin/perl use CGI; use PDF::API2; use constant mm => 25.4 / 72; $cgi = new CGI; $f1 = $cgi->param(f1); #Content-Type: text/html; charset=utf-8\n # <meta charset='UTF-8'> if (defined($f1)) { open (FILE, ">utf8_test1.out") or die "Can't open outfile"; print FILE $f1; close FILE; open (FILE, "<utf8_test1.out") or die "Can't open infile"; $f2 = <FILE>; close FILE; $pdf = PDF::API2->new(); $font1 = $pdf->corefont('Arial'); $page = $pdf->page; # Add blank page $page->mediabox(210/mm, 297/mm); $text = $page->text(); $text->font($font1, 28); $text->translate(20/mm ,280/mm); # A quick hack to handle a couple of special chars $f2 =~ s/\303\251/\351/g; # e-acute $f2 =~ s/\303\272/\372/g; # u-acute $text->text('PDF Output:' . $f2); $pdf->saveas('utf8_test1.pdf'); } print <<EOF; Content-Type: text/html; charset=utf-8\n <!DOCTYPE html> <html lang='en-NZ'> <head> <title>Test UTF-8</title> <meta charset='UTF-8'> </head> <body> <form method='post'> Input: <input type='text' name='f1' value='$f1'> <br> <input type='submit' name='submit' value='Submit'> <br> Output: $f1 </form> </body> </html> EOF

Thanks.


In reply to Write special chars to PDF. UTF8? by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.