-
Notifications
You must be signed in to change notification settings - Fork 44
/
pdf2txt.pl
executable file
·65 lines (55 loc) · 1.85 KB
/
pdf2txt.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -T
#
# Author: Hari Sekhon
# Date: 2013-02-01 16:22:00 +0000 (Fri, 01 Feb 2013)
#
# https://github.com/HariSekhon/DevOps-Perl-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help improve or steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
$DESCRIPTION="Tool for converting PDF to text for text analytics. Rustled this up quickly in Cloudera to analyze our internal KB for an unofficial topic challenge, which I won as a result of using this and a Java MapReduce word count :)
Operates as a standard unix filter program taking any number of PDF files as arguments (or -f/--pdf comma separated files or PDF content in standard input) and outputting the text to standard output.
See also Apache PDFBox and pdf2text unix tool";
$VERSION = "0.1";
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
use CAM::PDF;
use CAM::PDF::PageText;
$github_repo = "tools";
my $filename;
%options = (
"f|pdf=s" => [ \$filename, "PDF File to extract TXT from" ],
);
get_options();
my @filenames = split(/,/, $filename) if $filename;
push(@filenames, @ARGV);
push(@filenames, "-") unless @filenames;
my $pdf;
foreach(@filenames){
if($_ eq "-"){
print "PDF STDIN:\n";
} else {
print "PDF filename: $_";
}
$pdf = CAM::PDF->new($_);
#my $pageone_tree = $pdf->getPageContentTree(4);
#print CAM::PDF::PageText->render($pageone_tree);
unless($pdf){
print "No valid PDF detected, skipping...\n";
next;
}
foreach(1..$pdf->numPages()){
# TODO: this doesn't really work well, tried getPageContent which works even less well...
print "page $_: " . $pdf->getPageText($_) . "\n";
}
}