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

I created pdf documents with PDF::API2 and PDF::Reuse, but the search function in adobe reader doesn't work. I used UTF-8 characters in both packages and an external *.ttf font to see the proper characters. Everything seems to be fine, but the search option doesn't work. Any idea? /The code is more complicated but I reduced it for better understanding./ The reduced code:

use PDF::Reuse; use Encode; prFile('test.pdf'); #reading UTF-8 data from file open FILE, "<testFile.txt"; $content = ""; while (<FILE>) { $content .= $_; } prTTFont('arial.ttf'); prText(100, 100, decode('UTF-8', $content)); prEnd(); close FILE;

Replies are listed 'Best First'.
Re: PDF search problem
by roboticus (Chancellor) on Jul 04, 2012 at 00:09 UTC

    stolara:

    I couldn't reproduce the problem here with your code. I keep getting the message:

    $ perl 979790.pl Wide character in syswrite at /usr/local/share/perl/5.10.1/PDF/Reuse.p +m line 977, <FILE> line 212.

    I'm supposing that either you're not handling UTF-8 cleanly enough in your program, or you're not providing adequate test data to reproduce it. (Try adding a single UTF-8 string in your program, so we don't need to locate a UTF-8 file.)

    I went ahead and bodged something up to create a simple PDF with UTF-8 to see if I could find the text:

    #!/usr/bin/perl use strict; use warnings; use utf8; use PDF::API2; my $content='&#8750; E&#8901;da = Q, roboticus n &#8594; &#8734;, &#87 +21; f(i) = &#8719; g(i)'; my $pdf = PDF::API2->new(); my $page = $pdf->page(); $page->mediabox('Letter'); my $font = $pdf->ttfont('/usr/share/cups/fonts/FreeMono.ttf', -encodin +g=>'utf-8'); my $text = $page->text(); $text->font($font, 20); $text->translate(15,15); $text->text($content); $pdf->saveas('test_603.pdf');

    It properly created the file, and Adobe Reader displayed it as I expected. But, as you reported in your setup, Adobe Reader wouldn't find a simple, little robot embedded in the text.

    $ perl --version This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi Linux Boink 2.6.32-39-generic #86-Ubuntu SMP Mon Feb 13 21:47:32 UTC 2 +012 i686 GNU/Linux PDF::API2 2.019 Adobe Reader 9.3.2 04/01/2010

    Update: Fixed the broken code tag. (I can't believe I missed it in preview!) Also: I cut & pasted the code into the text window, but it seems to have recoded the example string: '∮ E⋅da = Q, marco n → ∞, ∑ f(i) = ∏ g(i)'. I couldn't find a way (google wasn't very helpful to me, I can't seem to exclude enough irrelevent nodes) to get the utf-8 stuff into the code listing.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      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

Re: PDF search problem
by zentara (Cardinal) on Jul 04, 2012 at 14:32 UTC
    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

      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