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

PDF::API2::Simple documents the ttfont method as having only one parameter. I suggest trying:

s{$pdf->pdf->ttfont($pdf,'FreeMono.ttf');} {$pdf->pdf->ttfont('FreeMono.ttf');}

Replies are listed 'Best First'.
Re^2: External font with PDF::API2::Simple
by Iceman1 (Novice) on Sep 20, 2011 at 03:37 UTC
    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();
      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
        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.