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.
|