lib/PDF/OCR2/Base.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/PDF/OCR2/Page.pm ↑ Summary
1 package PDF::OCR2::Base;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use vars qw($VERSION @EXPORT_OK @ISA @EXPORT $CHECK_PDF $REPAIR_XREF %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)/g;
7 @ISA = qw/Exporter/;
8 @EXPORT_OK = qw(check_pdf get_abs_pdf repair_xref);
9 %EXPORT_TAGS = ( all => \@EXPORT_OK );
10 4 ▶use LEOCHARRE::Debug;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 *CHECK_PDF = \$PDF::OCR2::CHECK_PDF;
13 $CHECK_PDF = 1; # turn this on by default
14  
15 *REPAIR_XREF = \$PDF::OCR2::REPAIR_XREF;
16  
17 sub check_pdf {
18 $_[0] or confess('missing arg');
19 require PDF::API2;
20 eval { PDF::API2->open($_[0]) } ? 1 : 0;
21 }
22  
23 sub get_abs_pdf {
24 my $arg = shift;
25 $arg or confess('missing arg to check_pdf');
26  
27 debug();
28  
29 require Cwd;
30 my $abs = Cwd::abs_path($arg)
31 or warn("Can't resolve with Cwd::abs_path() '$arg'")
32 and return;
33  
34 -f $abs
35 or warn("Not file on disk: $abs")
36 and return;
37  
38 no warnings;
39 if ( $CHECK_PDF ){
40 warn("CHECK_PDF flag is on..");
41 check_pdf($abs)
42 and return $abs;
43  
44 warn("PDF::API2 cannot open '$abs', maybe bad xref? $@");
45  
46 if ( $REPAIR_XREF ){
47 warn("REPAIR_XREF flag is on..")
48 and return repair_xref($abs);
49 }
50
51 return;
52 }
53  
54 return $abs;
55 }
56  
57  
58 sub repair_xref {
59 my $abs = shift;
60 my $abs_to = shift;
61  
62 unless ($abs_to ) {
63 $abs_to = $abs;
64  
65 $abs_to=~s/(\.\w{1,5})$/_repaired_xref_table$1/
66 or warn("Cannot match extension into '$abs'")
67 and return;
68 }
69  
70 debug();
71  
72 require File::Which;
73 my $bin = File::Which::which('pdftk')
74 or warn("repair_xref() Can't find path to pdftk.")
75 and return;
76  
77 #sprintf "%s.repaired_xref_table_%s.%s.pdf", $abs , time(), ( int rand 100 );
78 my @a = ('pdftk', $abs, 'output', $abs_to );
79  
80 ( system(@a) == 0 )
81 or warn("pdftk fails on '@a'\n -error: $!,$@")
82 and return;
83
84 #check it again..
85 check_pdf($abs_to)
86 ? ( warn("Repaired xref table.") and return $abs_to )
87 : ( warn("Could not repair xref table.") and unlink $abs_to and return );
88  
89  
90 }
91  
92  
93  
94  
95  
96  
97  
98 1;
99  
100  
101 __END__
102  
103 =pod
104  
105 =head1 NAME
106  
107 PDF::OCR2::Base
108  
109 =head1 DESCRIPTION
110  
111 =head1 SUBS
112  
113 None are exported by default.
114  
115 =head2 check_pdf()
116  
117 Argument is path to pdf file.
118 Checks with PDF::API2.
119 Returns true or false, warns of any errors.
120  
121 This uses an eval. Because PDF::API2 would otherwise die out on loading a bad pdf.
122  
123 =head2 get_abs_pdf()
124  
125 Argument is abs path to pdf.
126  
127 May attempt to check a pdf, and repair an xref table (not on the original file).
128 See L<PDF::OCR2>.
129  
130  
131 =head1 AUTHOR
132  
133 Leo Charre leocharre at cpan dot org
134  
135 =head1 COPYRIGHT
136  
137 Copyright (c) 2009 Leo Charre. All rights reserved.
138  
139 =head1 LICENSE
140  
141 This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".
142  
143 =head1 DISCLAIMER
144  
145 This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
146  
147 See the "GNU General Public License" for more details.
148  
149 =cut
« lib/PDF/OCR2/Page.pm ↑ Summary