Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to use Simplified Chinese word in the PDF::API2?

by yourself (Novice)
on Nov 02, 2006 at 09:42 UTC ( [id://581862]=perlquestion: print w/replies, xml ) Need Help??

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 (Priest) 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');

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://581862]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found