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

Hi!

When I use PDF::Create or PDF::API2, I have the problems.

#!/usr/bin/perl use strict; use warnings; use PDF::API2; use Encode; use Encode::CN; use utf8; my $name = encode("euc-cn",'中文'); my $pdf = PDF::API2->open('ns.pdf'); $pdf->saveas("ns001.pdf"); $pdf = PDF::API2->open("ns001.pdf"); my $page = $pdf->openpage(1); $page->mediabox('A4'); my $fnt = $pdf->corefont('Courier'); my $gfx = $page->gfx; $gfx->textlabel(220,690,$fnt,20,$name); $pdf->update;

How can I output the Simplified Chinese Code with Correct 'Encoding'? When I use 'gb2312' or 'utf-8', there has the wrong output.

Thanks!

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: How to use Simplified Chinese word in the PDF::API2?
by ForgotPasswordAgain (Vicar) on Nov 02, 2006 at 10:24 UTC

    It's not clear to me what the problem is, since I can't run the script. But do you mean to use decode (from Encode.pm), instead of encode?

    UPDATE: I looked at textlabel in PDF::API2::Content, and if you trace it back it goes to:

    sub add { my $self=shift @_; if(scalar @_>0) { $self->{' stream'}.=encode("iso-8859-1",($self->{' stream'}=~m +|\s$|o?'':' ').join(' ',@_).' '); } $self; }

    which is calling encode iso-8859-1 on whatever gets passed in. Oops.

Re: How to use Simplified Chinese word in the PDF::API2?
by Khen1950fx (Canon) on Nov 03, 2006 at 03:25 UTC
    I've been going over and over this for quite some time now. I believe the problem has to do with textlabel. Try this instead:

    $gfx->textlabel(220,690,$fnt,20,'name');

      I tried this way and it worked. Need to load a customized font file for your local font.
      use PDF::API2; use Encode; # Create a blank PDF file my $pdf = PDF::API2->new(); # Add a blank page my $page = $pdf->page(); my $font = $pdf->ttfont('/tmp/stsong.ttf'); my $ustring = "欢迎进入Perl PDF的&#19990 +;界"; $ustring = decode("utf-8", $ustring); # Add some text to the page my $text = $page->text(); $text->font($font, 20); $text->translate(80, 710); $text->text($ustring); $pdf->saveas('/tmp/foo.pdf');