Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: XPDF pdftotext page loop

by Kenosis (Priest)
on Oct 11, 2012 at 06:07 UTC ( #998369=note: print w/ replies, xml ) Need Help??


in reply to Re^2: XPDF pdftotext page loop
in thread XPDF pdftotext page loop

Since pdftotext defaults to inserting form feed characters between pages, you can examine each line for a form feed character as an indication of pagination:

use strict; use warnings; my $i = 0; my $pageNum = 1; open my $fh, "pdftotext -layout multipage.pdf - |" or die $!; print "---------- Begin Page $pageNum ----------\n"; while ( my $line = <$fh> ) { if ( $line =~ /\xC/ ) { print "\n---------- End Page $pageNum ----------\n"; $pageNum++; print "---------- Begin Page $pageNum ----------\n"; } $i++; print "\n<div class=\"line\"><div>$i</div>$line</div>"; } close $fh;

Another option which may serve you is using CAM::PDF:

use strict; use warnings; use CAM::PDF; my $pdf = CAM::PDF->new('multipage.pdf'); for my $pageNumber ( 1 .. $pdf->numPages() ) { my $pageText = $pdf->getPageText($pageNumber); my @certainLines = ( split /\n/, $pageText )[ 9 .. 14 ]; print "---------- Lines 10 - 15 on Page $pageNumber ----------\n"; print +( join "\n", @certainLines ) . "\n"; print "---------- End Page $pageNumber ----------\n"; }

The above shows how to grab a range of text lines from the converted pdf page. You may find, however, that pdftotext does a better rendering job.

Hope this helps!


Comment on Re^3: XPDF pdftotext page loop
Select or Download Code
Re^4: XPDF pdftotext page loop
by Anonymous Monk on Oct 11, 2012 at 12:59 UTC
    That's great. you can't beat pdftotext for rendering the contents of the file being read but I appreciate the CAM::PDF example for a quick alternative that will work in some circumstances.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2015-07-06 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls