Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

by zentara (Archbishop)
on Sep 20, 2011 at 10:55 UTC ( #926899=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^4: External font with PDF::API2::Simple
by Speedy G (Initiate) on Apr 27, 2012 at 23:58 UTC
    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: 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 = "欢迎进入Perl的世界 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();
Re^4: External font with PDF::API2::Simple
by Iceman1 (Novice) on Sep 21, 2011 at 04:23 UTC
    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/ line 435.
    The subroutine in 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
        Simple sure has left home :-)
        Is there a way I could use the PDF::API2 code you show within a program that is otherwise using PDF::API2::Simple?
        Or would I have to convert all of my code to PDF::API2?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://926899]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-06-21 22:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.