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

I am using the PDF::API2 module for creating pdf files. How can I use an otf font in my pdf? I used

PDF::API2::addFontDirs '/home/user/www/project/fonts/';
my $font = $pdf->ttfont('/home/user/www/project/fonts/myfont.otf', -encoding => 'latin1');
$text->font($font, 12);
$text->translate(100, 100);
$text->text('Hello World');

But it doesn't work. Anybody having an idea?

Thanks

Replies are listed 'Best First'.
Re: PDF::API2 font
by almut (Canon) on Sep 23, 2009 at 08:59 UTC

    I think the font directory should not be part of the fontname in ->ttfont(...)  (update: actually, it doesn't matter, both work... but then you wouldn't need to use addFontDirs() ).

    This works for me:

    #!/usr/bin/perl use PDF::API2; PDF::API2::addFontDirs('/opt/Adobe/Reader9/Resource/Font'); my $pdf = PDF::API2->new(); my $page = $pdf->page(); my $text = $page->text(); my $font = $pdf->ttfont('MyriadPro-Regular.otf', -encoding => 'latin1' +); $text->font($font, 12); $text->translate(100, 100); $text->text('Hello World'); $pdf->saveas("hello.pdf");

    Which error(s) are you getting? What exactly doesn't work, i.e. what does happen?

      Thank you.

      I am not getting any errors, but the warning - 'Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.10.0/PDF/API2/Basic/TTF/Cmap.pm line 258.'

      But when I am using a ttf font, it works fine. The problem is with only the otf font. The pdf is being created. But when I open that pdf, it gets closed immediately. And no thumbnail preview available of that pdf file.

        It could be a problem with the specific font, or a subtle bug in PDF::API2 triggered by that very font... But it's hard to tell without knowing more about which font it is (I presume it's not really named "myfont.otf").

        What happens when you try a different OpenType font, for example the one I used in the snippet above (it ships with recent versions of Adobe Reader, so you might already have it, or at least can easily get it).

        Also, which version of PDF::API2 are you using?  And which PDF reader? (I'm asking because not all readers can handle embedded OpenType fonts)

Re: PDF::API2 font
by Anonymous Monk on Sep 23, 2009 at 07:24 UTC
    I haven't used PDF::API2 but, you could