Skip to content

Instantly share code, notes, and snippets.

@emasaka
Created August 7, 2011 12:47
Show Gist options
  • Select an option

  • Save emasaka/1130347 to your computer and use it in GitHub Desktop.

Select an option

Save emasaka/1130347 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Encode;
use PDF::API2;
my $pdf = PDF::API2->open($ARGV[0]) or die;
for my $p (1 .. $pdf->pages) {
my $page = $pdf->openpage($p);
next unless $page->{Annots};
for my $a ($page->{Annots}->elementsof) {
$a->realise;
next unless $a->{Contents};
my $text = $a->{Contents}->val;
eval { $text = decode('utf-16', $text); }; # error in ascii string
$text =~ tr/\r/\n/;
say encode('utf-8', $text) if $text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment