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\nAND
<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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |