in reply to PDF search problem

I had trouble getting the example codes to work, but PDF::API2 / unicode characters has some pertinent info. Just from some hacking at a PDF::API2 example, this code writes out and is searchable using xpdf. I don't have acroread going because they don't have a 64 bit version yet for linux. If you download this code, open it in a unicode aware editor and resave it with Encoding set to Unicode UTF-8, it will come in from Perlmonks as Western ISO-8859-1. The Geany Editor works real nice for this kind of stuff.
#!/usr/bin/perl use warnings; use strict; use PDF::API2; my $pdf=PDF::API2->new; my $f1=$pdf->corefont('Helvetica',1); foreach my $fn (qw( arial arialbold arialitalic arialbolditalic )) { foreach my $en (qw( latin1 macroman )) { my $font=$pdf->corefont($fn,-encode => $en); print STDERR qq($fn - $en ---\n); my $page = $pdf->page; my $txt=$page->text; $txt->translate(100,750); $txt->font($font,50); $txt->lead(50); $txt->text('Hello World !'); $txt->cr; $txt->font($font,20); $txt->text("german spec.chars: ae='ä' AE='Ä' oe='ö' "); $txt->cr; $txt->text("german spec.chars: OE='Ö' ue='ü' UE='Ü' ss='ß' "); $txt->cr; $txt->font($f1,20); $txt->text("This is font: $fn ($en)"); $txt->font($font,20); foreach my $x (0..15) { foreach my $y (0..15) { $txt->translate(50+(33*$x),50+(33*$y)); $txt->text(chr($y*16+$x)); } } } } $pdf->saveas("$0.pdf"); $pdf->end(); exit; __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: PDF search problem
by stolara (Initiate) on Jul 09, 2012 at 20:30 UTC

    Hi guys!

    It still doesn't work. An input file which fails for me, you can find here:
    http://jumbo26.uw.hu/testFile.txt

    Andras

      This script works for me on your testFile.txt. The line setting arial font caused an error, so I commented it out. Make sure you save the testFile.txt as utf8. Also, I didn't need the decode, just use the $content. Also, the $content line was sort of long, so I only used the last portion to observe the accents.
      #!/usr/bin/perl use warnings; use strict; use PDF::Reuse; use utf8::all; prFile('test.pdf'); #reading UTF-8 data from file open FILE, "<testFile.txt" or die "$!\n";; my $content = ""; while (<FILE>) { $content .= $_; } print "$content\n"; prText(100, 500, $content ); prEnd(); close FILE;

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        Hi!

        Thanks you are right, but I im in a damonic trap:

        • if I use the default font, search in pdf works fine, but if there are special charactert in the pdf like hungarian ő,ű the pdf doesn't open at all!
        • if I change the font type with prTTFont('arial.ttf'); the pdf opens fine with the special characters but search in pdf doesn't work...

        Any other idea? :-)