Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl: the Markov chain saw
 
PerlMonks  

Re: Example Of Using CAM::PDF Like HTML::TokeParser

by Khen1950fx (Canon)
on Oct 08, 2011 at 23:25 UTC ( #930395=note: print w/ replies, xml ) Need Help??


in reply to Example Of Using CAM::PDF Like HTML::TokeParser

Would something like this help? I'm still trying to get a handle on it, and this is what I have so far.

#!/usr/bin/perl use strict; use warnings; use Devel::SimpleTrace; use CAM::PDF; use CAM::PDF::Content; use CAM::PDF::PageText; use Data::Dumper::Concise; my $file = '/root/Desktop/sample1.pdf'; binmode STDOUT, ":encoding(utf8)"; my $pdf = CAM::PDF->new($file); for my $pagenum(1 .. $pdf->numPages) { my $contentTree = $pdf->getPageContentTree($pagenum) or next; $contentTree->validate() or die $@; print Dumper($contentTree->render('CAM::PDF::Renderer::Dump')); $pdf->setPageContent(2,$pagenum); last; }


Comment on Re: Example Of Using CAM::PDF Like HTML::TokeParser
Download Code
Re^2: Example Of Using CAM::PDF Like HTML::TokeParser
by Limbic~Region (Chancellor) on Oct 11, 2011 at 00:42 UTC
    Khen1950fx,
    In short, yes. I am still playing but this was a significant step in the right direction. Please let me know what else you come up with.

    Cheers - L~R

      Here's what I have now. I borrowed hdump from the examples directory of HTML::Parser. Then I used CAM::PDF::GS to make a gs log file.
      #!/usr/bin/perl use strict; use warnings; use CAM::PDF; use Data::Dumper::Concise; use base qw(CAM::PDF::GS::NoText); my $file = shift @ARGV; my $log = '/root/Desktop/gs.log'; binmode STDOUT, ":encoding(utf8)"; open STDOUT, '>', $log; my $pdf = CAM::PDF->new($file); my $contentTree = $pdf->getPageContentTree(5); my $gs = $contentTree->computeGS; print Dumper($gs): close STDOUT;
      From the cmdline do
      perl gscript.pl /path/to/pdf
      Then I used hdump to examine gs.log:
      #!/usr/bin/perl -w use strict; use HTML::TokeParser; use Data::Dumper::Concise; $| = 1; sub h { my ( $event, $line, $column, $text, $tagname, $attr ) = @_; my (@d) = uc( substr( $event, 0, 1 ) ) . " L$line C$column"; substr( $text, 40 ) = "..." if length $text > 40; push @d, $text; push @d, $tagname if defined $tagname; push @d, $attr if $attr; print Dumper(@d); } my $p = HTML::Parser->new( api_version => 3 ); $p->handler( default => \&h, "event, line, column, text, tagname, attr +" ); $p->parse_file( @ARGV ? shift : *STDIN );
      From the cmdline:
      perl hdump /path/to/gs.log
      I hope that it's useful for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2014-04-21 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls