PDF.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« PDFPages.pm ↑ Summary PDFInfo.pm »
1 package Catmandu::Importer::PDF;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Catmandu::Util qw(:is);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Poppler;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use Moo;
7  
8 our $VERSION = '0.012';
9  
10 with 'Catmandu::Importer';
11  
12 has poppler_glib_version => (
13 is => "ro",
14 init_arg => undef,
15 lazy => 1,
16 builder => "_build_poppler_glib_version"
17 );
18 has has_date_bug => (
19 is => "ro",
20 init_arg => undef,
21 lazy => 1,
22 builder => "_build_has_date_bug"
23 );
24 has date_offset => (
25 is => "ro",
26 init_arg => undef,
27 lazy => 1,
28 default => sub {
29 require DateTime;
30 DateTime->now( time_zone => "local" )->offset();
31 }
32 );
33 sub _build_poppler_glib_version {
34 require ExtUtils::PkgConfig;
35 ExtUtils::PkgConfig->modversion('poppler-glib');
36 }
37 sub _build_has_date_bug {
38 require version;
39 version->parse( $_[0]->poppler_glib_version() ) < version->parse("0.45.0") ? 1 : 0;
40 }
41  
42 sub _createDoc {
43 my $self = $_[0];
44  
45 my $pdf = Poppler::Document->new_from_file( $self->file );
46  
47 my $num_pages = $pdf->get_n_pages();
48  
49 my $record = {
50 document => {
51 version => $pdf->get_pdf_version_string(),
52 title => $pdf->get_title(),
53 author => $pdf->get_author(),
54 subject => $pdf->get_subject(),
55 keywords => $pdf->get_keywords(),
56 creator => $pdf->get_creator(),
57 producer => $pdf->get_producer(),
58 creation_date => $pdf->get_creation_date(),
59 modification_date => $pdf->get_modification_date(),
60 metadata => $pdf->get_metadata()
61 },
62 pages => []
63 };
64  
65 for(my $i = 0;$i < $num_pages;$i++){
66 my $page = $pdf->get_page($i);
67 my $text = $page->get_text();
68 my($w,$h) = $page->get_size;
69 my $label = $page->get_label();
70  
71 my $p = {
72 width => $w,
73 height => $h,
74 label => $label,
75 text => $text
76 };
77  
78 push @{ $record->{pages} },$p;
79 }
80  
81 if( $self->has_date_bug() ) {
82  
83 if( is_natural( $record->{document}->{creation_date} ) ){
84 $record->{document}->{creation_date} += $self->date_offset();
85 }
86 if( is_natural( $record->{document}->{modification_date} ) ){
87 $record->{document}->{modification_date} += $self->date_offset();
88 }
89  
90 }
91  
92 $record;
93 }
94  
95 sub generator {
96 my ($self) = @_;
97  
98 return sub {
99 state $doc = undef;
100  
101 unless($doc){
102 $doc = $self->_createDoc();
103 return $doc;
104 }
105 return;
106  
107 }
108 }
109 sub DESTROY {
110 my ($self) = @_;
111 close($self->fh);
112 }
113  
114 =encoding utf8
115  
116 =head1 NAME
117  
118 Catmandu::Importer::PDF - Catmandu importer to extract data from one pdf
119  
120 =begin markdown
121  
122 # STATUS
123  
124 [![Build Status](https://travis-ci.org/LibreCat/Catmandu-Importer-PDF.svg?branch=master)](https://travis-ci.org/LibreCat/Catmandu-Importer-PDF)
125 [![Coverage](https://coveralls.io/repos/LibreCat/Catmandu-Importer-PDF/badge.png?branch=master)](https://coveralls.io/r/LibreCat/Catmandu-Importer-PDF)
126 [![CPANTS kwalitee](http://cpants.cpanauthors.org/dist/Catmandu-Importer-PDF.png)](http://cpants.cpanauthors.org/dist/Catmandu-Importer-PDF)
127  
128 =end markdown
129  
130 =head1 SYNOPSIS
131  
132 #From the command line
133  
134 #Export pdf information, and text
135  
136 $ catmandu convert PDF --file input.pdf to YAML
137  
138 #In a script
139  
140 use Catmandu::Sane;
141  
142 use Catmandu::Importer::PDF;
143  
144 my $importer = Catmandu::Importer::PDF->new( file => "/tmp/input.pdf" );
145  
146 $importer->each(sub{
147  
148 my $pdf = $_[0];
149 #..
150  
151 });
152  
153 =head1 EXAMPLE OUTPUT IN YAML
154  
155 document:
156 author: ~
157 creation_date: 1207274644
158 creator: PDFplus
159 keywords: ~
160 metadata: ~
161 modification_date: 1421574847
162 producer: "Nobody at all"
163 subject: ~
164 title: "Hello there"
165 version: PDF-1.6
166 pages:
167 - label: Cover Page
168 height: 878
169 width: 595
170 text: "Hello world"
171  
172 =head1 INSTALLATION
173  
174 In order to install this package you need the following system packages installed
175  
176 =over
177  
178 =item Centos
179  
180 Requires Centos 7 at minimum. Centos 6 only has poppler-glib 0.12.
181  
182 * perl-devel
183  
184 * make
185  
186 * gcc
187  
188 * gcc-c++
189  
190 * libyaml-devel
191  
192 * libyaml
193  
194 * poppler-glib ( >= 0.16 )
195  
196 * poppler-glib-devel ( >= 0.16 )
197  
198 * gobject-introspection-devel
199  
200 =item Ubuntu
201  
202 Requires Ubuntu 14 at minimum.
203  
204 * libpoppler-glib8
205  
206 * libpoppler-glib-dev
207  
208 * gobject-introspection
209  
210 * libgirepository1.0-dev
211  
212 =back
213  
214 =head1 NOTES
215  
216 * L<Catmandu::Importer::PDF> returns one record, containing both document information, and page text
217  
218 * L<Catmandu::Importer::PDFPages> returns multiple records, each for each page
219  
220 * L<Catmandu::Importer::PDFInfo> returns one record, containing document information
221  
222 =head1 KNOWN ISSUES
223  
224 * Due to a bug in older versions of poppler-glib (bug #94173), the creation_date and modification_date can be returned in local time, instead of utc. This module tries to fix that.
225  
226 * Some versions of Poppler add form feeds and newlines to a text line, while others don't.
227  
228 =head1 AUTHORS
229  
230 Nicolas Franck C<< <nicolas.franck at ugent.be> >>
231  
232 =head1 SEE ALSO
233  
234 L<Catmandu::Importer::PDFInfo>, L<Catmandu::Importer::PDFPages>, L<Catmandu>, L<Catmandu::Importer> , L<Poppler>
235  
236 =cut
237  
238 1;
« PDFPages.pm ↑ Summary PDFInfo.pm »