File Coverage

blib/lib/Poppler.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Poppler 1.01;
2              
3             =encoding utf8
4              
5             =head1 NAME
6              
7             Poppler - Bindings to the poppler PDF rendering library
8              
9             =head1 SYNOPSIS
10              
11             use Poppler;
12              
13             # initialize using filename
14             my $pdf = Poppler::Document->new_from_file( 'file.pdf' );
15              
16             # or, initialize using scalar data
17             open my $fh, '<:raw', 'file.pdf';
18             read ($fh, my $data, -s 'file.pdf')
19             close $fh;
20             my $pdf = Poppler::Document->new_from_data( $data );
21              
22             # get some general info
23             my $n_pages = $pdf->get_n_pages;
24             my $title = $pdf->get_title;
25             # etc ...
26              
27             # get the first page
28             my $page = $pdf->get_page( 0 );
29              
30             # get page size
31             my ($w, $h) = $page->get_size;
32              
33             # or, for backward compatibility
34             my $dims = $page->get_size; # a Poppler::Dimension object
35             my $w = $dims->get_width;
36             my $h = $dims->get_height;
37              
38             # do other fancy things (get page links, annotations, movies, etc)
39             # (see poppler-glib documentation for details)
40              
41             # render to a Cairo surface
42             use Cairo;
43             my $surface = Cairo::ImageSurface->create( 'argb32', 100, 100 );
44             my $cr = Cairo::Context->create( $surface );
45             $page->render_to_cairo( $cr );
46             $cr->show_page;
47              
48             =head1 ABSTRACT
49              
50             Bindings to the poppler PDF library via the Glib interface. Allows
51             querying of a PDF file structure and rendering to various output targets.
52              
53             =head1 DESCRIPTION
54              
55             The C module provides complete bindings to the poppler PDF library
56             through the Glib interface. Find out more about poppler at
57             L.
58              
59             As of version 1.01, no XS is used directly but bindings are provided using
60             GObject introspection and the L module. See the
61             L for a brief example of how the module can be used. For
62             detailed documentation on the available classes and methods, see the poppler
63             glib documentation for the C libraries and the L
64             documentation for a description of how methods are mapped between the C
65             libraries and the Perl namespace.
66              
67             =head1 CONSTRUCTORS
68              
69             =over
70              
71             =item new_from_file ($filename)
72              
73             Takes a system path or URI to a PDF file as an argument and returns a
74             Poppler::Document object. The C library itself requires a full
75             URI (e.g. "file:///home/user/file.pdf") but this module attempts to convert
76             regular system paths if provided via the L module.
77              
78             =item new_from_data ($data)
79              
80             Takes a PDF data chunk as an argument and returns a Poppler::Document object.
81              
82             =back
83              
84             =head1 METHODS
85              
86             For details on the classes and methods available beyond the constructors
87             listed above, please refer to the canonical documentation for the C library
88             listed under L. A general discussion of how these classes
89             and methods map to the Perl equivalents can be found in the
90             L documentation. Generally speaking, a C function
91             such as 'poppler_document_get_title' would map to
92             'Poppler::Document->get_title'. Methods that return a G
93              
94             =cut
95              
96 1     1   25441 use strict;
  1         3  
  1         40  
97 1     1   6 use warnings;
  1         2  
  1         40  
98 1     1   6 use Carp qw/croak/;
  1         6  
  1         68  
99 1     1   7 use Cwd qw/abs_path/;
  1         7  
  1         50  
100 1     1   6 use Exporter;
  1         2  
  1         46  
101 1     1   1289 use File::ShareDir;
  1         35038  
  1         59  
102 1     1   1541 use Glib::Object::Introspection;
  0            
  0            
103             use URI::file;
104             use FindBin;
105             use Poppler::Page::Dimension;
106              
107             our @ISA = qw(Exporter);
108              
109             my $_POPPLER_BASENAME = 'Poppler';
110             my $_POPPLER_VERSION = '0.18';
111             my $_POPPLER_PACKAGE = 'Poppler';
112              
113             =head2 Customizations and overrides
114              
115             In order to make things more Perlish, C customizes the API generated
116             by L in a few spots:
117              
118             =over
119              
120             =cut
121              
122             # - Customizations ---------------------------------------------------------- #
123              
124             =item * The array ref normally returned by the following functions is flattened
125             into a list:
126              
127             =over
128              
129             =item Poppler::Document::get_attachments
130              
131             =item Poppler::Page::get_link_mapping
132              
133             =item Poppler::Page::find_text
134              
135             =item Poppler::Page::find_text_with_options
136              
137             =item Poppler::Page::get_annot_mapping
138              
139             =item Poppler::Page::get_form_field_mapping
140              
141             =item Poppler::Page::get_image_mapping
142              
143             =item Poppler::Page::get_link_mapping
144              
145             =item Poppler::Page::get_selection_region
146              
147             =item Poppler::Page::get_text_attributes
148              
149             =item Poppler::Page::get_text_attributes_for_area
150              
151             =back
152              
153             =cut
154              
155             my @_POPPLER_FLATTEN_ARRAY_REF_RETURN_FOR = qw/
156             Poppler::Document::get_attachments
157             Poppler::Page::get_link_mapping
158             Poppler::Page::find_text
159             Poppler::Page::find_text_with_options
160             Poppler::Page::get_annot_mapping
161             Poppler::Page::get_form_field_mapping
162             Poppler::Page::get_image_mapping
163             Poppler::Page::get_link_mapping
164             Poppler::Page::get_selection_region
165             Poppler::Page::get_text_attributes
166             Poppler::Page::get_text_attributes_for_area
167             /;
168              
169             =item * The following functions normally return a boolean and additional out
170             arguments, where the boolean indicates whether the out arguments are valid.
171             They are altered such that when the boolean is true, only the additional out
172             arguments are returned, and when the boolean is false, an empty list is
173             returned.
174              
175             =over
176              
177             =item Poppler::Document::get_id
178              
179             =item Poppler::Page::get_text_layout
180              
181             =item Poppler::Page::get_text_layout_for_area
182              
183             =item Poppler::Page::get_thumbnail_size
184              
185             =back
186              
187             =cut
188              
189             my @_POPPLER_HANDLE_SENTINEL_BOOLEAN_FOR = qw/
190             Poppler::Document::get_id
191             Poppler::Page::get_text_layout
192             Poppler::Page::get_text_layout_for_area
193             Poppler::Page::get_thumbnail_size
194             /;
195              
196              
197             # - Wiring ------------------------------------------------------------------ #
198              
199             sub import {
200              
201             Glib::Object::Introspection->setup (
202             search_path => File::ShareDir::dist_dir('Poppler'),
203             basename => $_POPPLER_BASENAME,
204             version => $_POPPLER_VERSION,
205             package => $_POPPLER_PACKAGE,
206             flatten_array_ref_return_for => \@_POPPLER_FLATTEN_ARRAY_REF_RETURN_FOR,
207             handle_sentinel_boolean_for => \@_POPPLER_HANDLE_SENTINEL_BOOLEAN_FOR,
208             );
209              
210             # call into Exporter for the unrecognized arguments; handles exporting and
211             # version checking
212             Poppler->export_to_level (1, @_);
213              
214             }
215              
216             # - Overrides --------------------------------------------------------------- #
217              
218             =item * Perl reimplementations of C,
219             C, and C
220             are provided which remove the need to provide filenames as URIs (e.g.
221             "file:///absolute/path"). The module accepts either real URIs or regular
222             system paths and will convert as necessary using the C module. Any of
223             these formats should work:
224              
225             $p = Poppler::Document->new_from_file( 'file:///home/user/file.pdf' );
226             $p = Poppler::Document->new_from_file( '/home/user/file.pdf' );
227             $p = Poppler::Document->new_from_file( 'file.pdf' );
228              
229             # likewise for save()
230             # likewise for save_a_copy()
231              
232             =cut
233              
234             sub Poppler::Document::new_from_file {
235              
236             my ($class, $fn, $pwd) = @_;
237              
238             $fn = URI::file->new_abs($fn)
239             if (! URI->new($fn)->has_recognized_scheme);
240             my $doc = Glib::Object::Introspection->invoke(
241             'Poppler', 'Document', 'new_from_file', $class, $fn, $pwd);
242              
243             return $doc;
244              
245             }
246              
247             sub Poppler::Document::save {
248              
249             my ($class, $fn) = @_;
250              
251             $fn = URI::file->new_abs($fn)
252             if (! URI->new($fn)->has_recognized_scheme);
253             my $bool = Glib::Object::Introspection->invoke(
254             'Poppler', 'Document', 'save', $class, $fn);
255              
256             return $bool;
257              
258             }
259              
260             sub Poppler::Document::save_a_copy {
261              
262             my ($class, $fn) = @_;
263              
264             $fn = URI::file->new_abs($fn)
265             if (! URI->new($fn)->has_recognized_scheme);
266             my $bool = Glib::Object::Introspection->invoke(
267             'Poppler', 'Document', 'save_a_copy', $class, $fn);
268              
269             return $bool;
270              
271             }
272              
273             sub Poppler::Page::get_size {
274              
275             my ($class) = @_;
276              
277             my ($w,$h) = Glib::Object::Introspection->invoke(
278             'Poppler', 'Page', 'get_size', $class);
279             return Poppler::Page::Dimension->new($w, $h)
280             if (! wantarray);
281             return($w, $h);
282              
283             }
284              
285             =back
286              
287             =cut
288              
289             1;
290              
291             __END__