Started writing this before thanos1983 update (use of 'DejaVuSans.ttf' as an OK modern font, too :) ), so, for FWIW:

The "core" Helvetica font uses single-byte built-in encoding, which doesn't have greek characters.

In fact, in modern times it is not advised to use any of Adobe 14 "core", not-to-be-embedded fonts, they belong to the era of 20+ years ago, when storage space was at a premium. Even if you think that you produce (and consume) PDFs in very controlled, ascii-only environment.

That's said, the "core" font which contains greek and other math characters is called 'Symbol'. You give normal, utf8 Perl strings as arguments to PDF::API2 methods, everything will be encoded for you automatically.

use strict;
use warnings;
use utf8;
use PDF::API2;

my $pdf  = PDF::API2-> new;
my $page = $pdf-> page;
my $text = $page-> text;

my $core_font = $pdf-> corefont( 'Symbol' );

$text-> font( $core_font, 20 );
$text-> translate( 50, 700 );
$text-> text( 'ω ∞' );

$pdf-> saveas( 'test.pdf' );

The output is a 5 KB file, which, in addition to necessary overhead, contains a lot of bloat. PDF::API2 doesn't do it quite optimal with "core" fonts. Let's insert this before last line:

delete @$core_font{ qw/
    Encoding
    FirstChar
    LastChar
    Name
    Widths
/};

The output is 986 bytes. The bad part, however, is that, while PDF looks OK on-screen, text extraction (e.g. copy-paste to Notepad), in both cases above, is broken when I check with Adobe Reader DC (i.e. latest) -- garbage is copied. Maybe Adobe doesn't care about "core" Symbol any more. However, both Firefox and Edge extract greek symbols correctly.

The right way is to use embeddable, modern, having good Unicode support i.e. large code-points repertoire, TrueType fonts. Again, give "utf8 Perl strings as arguments to PDF::API2 methods, everything will be encoded for you automatically".

use strict;
use warnings;
use feature 'say';
use utf8;
use PDF::API2;

my $pdf  = PDF::API2-> new;
my $page = $pdf-> page;
my $text = $page-> text;

my $ttf_font = $pdf-> ttfont( 'DejaVuSans.ttf' );

$text-> font( $ttf_font, 20 );
$text-> translate( 50, 700 );
$text-> text( 'ω ∞ latin אב åçß юя' );

$pdf-> saveas( 'test.pdf' );

Here the text string sports greek, (extended-)latin, hebrew and cyrillic characters. It displays OK on-screen and text can be extracted even with backward Reader DC. File size is 55 KB, however.


In reply to Re: PDF::API2 printing non ascii characters by vr
in thread PDF::API2 printing non ascii characters by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.