in reply to Ho to produce Unicode text with PDF::Report?

Re^2: PDF::API2 / unicode characters leads me to believe that the PDF core fonts are not Unicode-compatible. I take it you are using those?

Having taken a closer look at PDF::Report, I'm left with the impression that there's no real way to get it to use external fonts. You could shell out to PDF::API2 (which it wraps) using ->getPDFAPI2Object() and then call ->ttfont() on that, but there's no way in PDF::Report to apply the resulting font object to your text, so you'd have to keep using ->getPDFAPI2Object() -- to add text, then to add pages, and so on, and in the end it'd be easier to just use PDF::API2 directly.

Here's some skeleton code that Works For Me:

#!usr/bin/perl use strict; use utf8; use PDF::API2; my $pdf = PDF::API2->new(); my $page = $pdf->page(); my $font = $pdf->ttfont('./DejaVuSans.ttf'); my $text = $page->text(); $text->font($font, 20); $text->translate(100, 700); $text->text('...'); $pdf->saveas('test.pdf');

(Unicode string stripped out due to the Monastery not being willing to handle it.)

Replies are listed 'Best First'.
Re^2: Ho to produce Unicode text with PDF::Report?
by flamey (Scribe) on Jun 29, 2014 at 13:14 UTC
    Thank you. I was really hoping to take advantage of the ::Report's simplicity. I guess I'll have to learn the hard way to do it.

      You're welcome. Perhaps you could ask the author of PDF::Report to add support for using external (TTF) fonts? Another method like the following would likely be all that's needed:

      sub setTTFont { my ( $self, $fontfile, $size )= @_; if (exists $self->{__font_cache}->{$fontfile}) { $self->{font} = $self->{__font_cache}->{$fontfile}; } else { $self->{font} = $self->{pdf}->ttfont($fontfile); $self->{__font_cache}->{$fontfile} = $self->{font}; } $self->{fontname} = $fontfile; }

      In other words, pretty much exactly the same as ->setFont(), but calling ->ttfont() rather than ->corefont() on the underlying PDF::API2 object.

      (Notes - a) this'd go into PDF::Report, of course, not your script; b) completely untested; c) uses the same fontcache as ->setFont(), which may or may not be a good design choice; d) could be refactored to avoid code duplication (e.g. have this and ->setFont() call an internal helper routine).)