Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

External font with PDF::API2::Simple

by Iceman1 (Novice)
on Sep 19, 2011 at 10:16 UTC ( [id://926708]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I am trying to use an external font (FreeMono.ttf) with PDF::API2::Simple but get the following error:

cannot find font '' ... at /usr/local/share/perl/5.10.1/PDF/API2/Resource/CIDFont/TrueType/FontFile.pm line 424.

#!/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($pdf,'FreeMono.ttf'); $pdf->add_font('MonoFont',$font_obj); $pdf->set_font('MonoFont'); # 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: External font with PDF::API2::Simple
by zentara (Archbishop) on Sep 19, 2011 at 12:03 UTC
    This is untested, but the same thing happens with adding custom fonts to graphics modules. You need to put a copy of the font into the cwd and refer to it as './FreeMono.ttf', or give the complete full system path to the font, such as '/usr/local/share/fonts/ttf/FreeMono.ttf' ( depends on where your system puts them)

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks zentara, I have tried both of those but it is still giving the same error message :-(
Re: External font with PDF::API2::Simple
by keszler (Priest) on Sep 19, 2011 at 22:58 UTC

    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');}
      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

Log In?
Username:
Password:

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

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

    No recent polls found