Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: External font with PDF::API2::Simple

by Iceman1 (Novice)
on Sep 20, 2011 at 03:37 UTC ( [id://926821]=note: print w/replies, xml ) Need Help??


in reply to Re: External font with PDF::API2::Simple
in thread External font with PDF::API2::Simple

Thank you keszler, I have changed that but now I get a different error message:
Can't call method "text" without a package or object reference at /usr/share/perl5/PDF/API2/Simple.pm line 435.
I have included adjusted test code below.
#!/usr/bin/perl use strict "vars"; use PDF::API2::Simple; # Start new PDF my $page_number = 1; my $pdf = PDF::API2::Simple->new( file => 'test.pdf', header => \&header, footer => \&footer ); # Set a custom font my $font_obj = $pdf->pdf->ttfont('FreeMono.ttf'); $pdf->add_font('MonoFont',$font_obj); # Add a page to the PDF $pdf->add_page(); # Insert some text # Set text color $pdf->fill_color( '#000000' ); for (my $i = 0; $i < 35; $i++) { my $text = "$i - Example text"; $pdf->text( $text, x => $pdf->margin_left, autoflow => 'on' ); } # Save PDF to disk $pdf->save();

Replies are listed 'Best First'.
Re^3: External font with PDF::API2::Simple
by zentara (Archbishop) on Sep 20, 2011 at 10:55 UTC
    Have you read closely the perldoc for the module?
    You can optionally pass a font object and font size if you have loade +d an external font. my $font_obj = $pdf->pdf->ttfont('new_font.ttf'); $pdf->add_font('NewFont', $font_obj, '12'); Refer to PDF::API2 for the various font methods, like "ttfont", + available.

    Just from that, it appears that your $pdf->add_font('MonoFont',$font_obj); needs a font size specification added.

    Furthermore, looking through the examples in PDF::API2, there are code constructs like

    # from examples/*02_truetype_fonts-pl foreach $fe (qw( adobe-standard cp437 cp850 latin1 )) { print STDERR "$fn ($fe)\n"; $font=$pdf->ttfont($fn , -encode =>$fe); # from examples/*02_synthetic_fonts-pl use PDF::API2; use PDF::API2::SynFont; $pdf=PDF::API2->new; $f1=$pdf->corefont('Arial',-encode=>'latin1'); $f2=$pdf->corefont('Helvetica-Bold',-encode=>'latin1'); $f3=$pdf->corefont('Helvetica-Oblique',-encode=>'latin1'); $f4=$pdf->corefont('Helvetica-BoldOblique',-encode=>'latin1'); my $font=PDF::API2::SynFont->new_api($pdf,$f1,-bold=>4);

    so you may need to specify an encoding, or at least run some simple examples to see how to get your font code right.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      I'd like to clarify this thread since it wasn't conclusive, and it's the only thread that I found with this issue. I stumbled upon the same issue, and the documentation isn't clear. First PDF::API2::Simple does NOT support different encoding other than the default 8-bit encoding. PDF::API2 does support unicode, but you cannot load a corefont to the PDF and then do add it using $PDF->add_font() since it's a PDF::API2::Simple method, and it can't handle different encodings. PDF::API2::Simple only allows you to load external fonts, but only with 8 bit encoding. To use different encodings you have to use the full PDF::API2 module. The best doc for a pdf newb that I found was the following: http://pdfapi2.sourceforge.net/pdfapi2_for_fun_and_profit_APW2005.pdf Cheers, Speedy G
        I have suffered the issue for adding Chinese font to my PDF document, i tried with the tutorial of the model, and it worked perfectly. But we need to put the add_font method after add_page(), otherwise the error will be same as we mentioned in this thread. Here is my code.
        use PDF::API2::Simple; use Encode; use Gaia::Common::Utils; my $pdf = PDF::API2::Simple->new( file => 'root/tmp/foo.pdf', width => 595, height => 842 ); my $font = $pdf->pdf->ttfont('root/statics/font/stsong.ttf'); $pdf->add_page(); $pdf->add_font('', $font, '12' ); # note this line need to be after ab +ove line. $str = "&#27426;&#36814;&#36827;&#20837;Perl&#30340;&#19990;&#30028; H +ello World"; $str = decode("utf-8", $str); #$str = encode("utf-8", $str); # Count $pdf->text( $str, x => 100, y => 700, font_size => 12, ); $pdf->save();
      Hi zentara
      Thanks for the reply.
      I have studied the perl doc extensively but have not been able to work it out.

      I missed that but as per your notes and their example, I have added a font size but still get the same error:

      my $font_obj = $pdf->pdf->ttfont('freemono.ttf'); $pdf->add_font('MonoFont',$font_obj,12); Can't call method "text" without a package or object reference at /usr +/share/perl5/PDF/API2/Simple.pm line 435.
      The subroutine in Simple.pm that has line 435 is:
      sub add_font { my ($self, $font_name, $font_obj, $font_size) = @_; if ( $font_obj ) { LINE 435 --> $self->current_page->text->font($font_obj, $font_size); $self->fonts->{$font_name} = $font_obj; } if ( ! exists $self->fonts->{$font_name} ) { $self->fonts->{$font_name} = $self->pdf->corefont($font_name); } $self->current_font( $self->fonts->{$font_name} ); }
      I have been unable to see where I'm going wrong.
      I presume that what PDF::API2::Simple sends to PDF::API2 is already coded within PDF::API2::Simple, so I figured their example should be all that is needed for it to work?

      Would you have any other suggestions that would help track it down or do you think I am missimg something with the above?
      I would appreciate any help you could offer with this very frustrating problem.
        Why not go back to PDF::API2 ? because the simple has left PDF::API2::Simple. :-)

        This works for me with a generic ttf font

        #!/usr/bin/perl use strict; use warnings; use PDF::API2; my $text = "Some text "; my $pdf = PDF::API2->new(); my $page = $pdf->page; my $txt = $page->text; #my $font = $pdf->corefont('Helvetica-Bold', -encoding => 'latin1'); # alternatively, use TrueType fonts: # my $font = $pdf->ttfont('Verdana-Bold.ttf', -encoding => 'latin1'); my $font = $pdf->ttfont('./Generic.ttf', -encoding => 'latin1'); $txt->font($font, 25); $txt->translate(200, 550); $txt->fillcolor('black'); $txt->text($text); $pdf->saveas("$0.pdf");

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

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

    No recent polls found