lib/PDF/Imposition.pm

filter: Statementstmt 100% 95 / 95 Branchbran 100% 8 / 8 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 22 / 22 Podpod 100% 4 / 4 totaltotal 95.0% 135 / 142 CC 9 SCAR 8.2
CC
9
Cov
97%
BUILDARGS
6.0
impose
3.0
_build_job_name
2.1
CRAP 2.3
« lib/PDF/Imposition/Schema2side.pm ↑ Summary script/pdf-impose.pl »
1 package PDF::Imposition;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Types::Standard qw/Enum Object Maybe Str HashRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use PDF::Cropmarks;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 DEBUG => $ENV{AMW_DEBUG},
17 10 };
18  
19 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 PDF::Imposition - Perl module to manage the PDF imposition
24  
25 =head1 VERSION
26  
27 Version 0.26
28  
29 =cut
30  
31 10 our $VERSION = '0.26';
32  
33 91 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
34 91 return "PDF::Imposition $VERSION PDF::Cropmarks "
35 . $PDF::Cropmarks::VERSION;
36 }
37  
38  
39 =head1 SYNOPSIS
40  
41 This module is meant to simplify the so-called imposition, i.e.,
42 rearrange the pages of a PDF to get it ready to be printed and folded,
43 with more logical pages placed on the sheet, usually (but not
44 exclusively) on recto and verso.
45  
46 This is what the routine looks like:
47  
48 use PDF::Imposition;
49 my $imposer = PDF::Imposition->new(file => "test.pdf",
50 outfile => "out.pdf",
51 # or # suffix => "-imposed",
52 signature => "40-80",
53 cover => 0,
54 schema => "2up");
55 $imposer->impose;
56 print "Output left in " . $imposer->outfile;
57  
58  
59 Please note that you don't pass the PDF dimensions (which are
60 extracted from the source PDF itself by the class, using the very
61 first page: if you want imposition, I do the reasonable assumption you
62 have all the pages with the same dimensions).
63  
64 =head1 METHODS
65  
66 =head2 Costructor options and accessors
67  
68 =head3 file
69  
70 The input file
71  
72 =head3 outfile
73  
74 The output file
75  
76 =head3 suffix
77  
78 The suffix of the output file (don't mix the two options).
79  
80 =head3 schema
81  
82 The schema to use.
83  
84 =over 4
85  
86 =item 2up
87  
88 See L<PDF::Imposition::Schema2up>
89  
90 =item duplex2up
91  
92 See L<PDF::Imposition::Schemaduplex2up>
93  
94 =item 2down
95  
96 See L<PDF::Imposition::Schema2down>
97  
98 =item 2x4x1
99  
100 See L<PDF::Imposition::Schema2x4x1>
101  
102 =item 2x4x2
103  
104 See L<PDF::Imposition::Schema2x4x2>
105  
106 =item 2side
107  
108 See L<PDF::Imposition::Schema2side>
109  
110 =item 4up
111  
112 See L<PDF::Imposition::Schema4up>
113  
114 =item 1x4x2cutfoldbind
115  
116 See L<PDF::Imposition::Schema1x4x2cutfoldbind>
117  
118 =item 1repeat2top
119  
120 See L<PDF::Imposition::Schema1repeat2top>
121  
122 =item 1repeat2side
123  
124 See L<PDF::Imposition::Schema1repeat2side>
125  
126 =item 1repeat4
127  
128 See L<PDF::Imposition::Schema1repeat4>
129  
130 =item ea4x4
131  
132 See L<PDF::Imposition::Schemaea4x4>
133  
134 =item 1x8x2
135  
136 See L<PDF::Imposition::Schema1x8x2>
137  
138 =item 1x1
139  
140 See L<PDF::Imposition::Schema1x1>
141  
142 =back
143  
144 =head3 cover
145  
146 If the last logical page must be placed at the very end, B<after> the
147 blank pages used to pad the signature. (C<2up>, C<2down>
148 C<1x4x2cutfoldbind>, C<4up>, C<1x1> only).
149  
150 Often it happens that we want the last page of the pdf to be the last
151 one on the physical booklet after folding. If C<cover> is set to a
152 true value, the last page of the logical pdf will be placed on the
153 last page of the last signature.
154  
155 =head3 signature
156  
157 The signature (integer multiple of four or range): C<2up> and C<2down> only.
158  
159 =head3 paper
160  
161 Passing this option triggers the cropmarks. While the original
162 dimensions are left unchanged, this size represents the size of the
163 logical page which is actually imposed.
164  
165 For example, you have a PDF in a6, you pass C<a5> as paper, and schema
166 C<2up>, you are going to get an a4 with 2 a6 with cropmarks.
167  
168 This option is passed to L<PDF::Cropmarks>. See the module
169 documentation for the accepted values.
170  
171 =head3 title
172  
173 The title to set in the PDF meta information. Defaults to the basename.
174  
175 =head2 Cropmarks options
176  
177 The following options are passed verbatim to L<PDF::Cropmarks>. See
178 the module documentation for the meaning and accepted values.
179  
180 =head3 paper_thickness
181  
182 Defaults to C<0.1mm>
183  
184 =head3 font_size
185  
186 Defaults to C<8pt>
187  
188 =head3 cropmark_offset
189  
190 Defaults to C<1mm>
191  
192 =head3 cropmark_length
193  
194 Defaults to C<12mm>
195  
196 =head2 impose
197  
198 Main method which does the actual job. You have to call this to get
199 your file. It returns the output filename.
200  
201 =head2 version
202  
203 Return the version string.
204  
205 =cut
206  
207 91 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=6 SCAR=17.9
Pod documented
208 91 my ($class, %options) = @_;
209 91 ▶ my $schema = lc(delete $options{schema} || '2up'); # default
Condition100%2/2
delete $options{'schema'} || '2up'
l!l
7516
MC/DC100%1/1
delete $options{'schema'} || '2up'
delete $options{'schema'}
Truth table100%2/2
A
A= delete $options{'schema'}
Aresult
11
00
210 91 $options{_version} = $class->version;
211 91 my $loadclass = __PACKAGE__ . '::Schema' . $schema;
212 91 my %our_options;
213 91 foreach my $cropmark_opt (qw/paper
214 cropmark_offset
215 cropmark_length
216 font_size
217 paper_thickness/) {
218 455 ▶ if (exists $options{$cropmark_opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{$cropmark_opt}) 60 395 455
219 60 $our_options{$cropmark_opt} = $options{$cropmark_opt};
220 }
221 }
222 91 load $loadclass;
223 90 ▶ unless ($options{title}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"title"}) 89 1 90
224 89 ▶ if ($options{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"file"}) 68 21 89
225 68 $options{title} = basename($options{file});
226 }
227 }
228  
229 90 my $imposer = $loadclass->new(%options);
230 90 $our_options{imposer} = $imposer;
231 90 $our_options{schema} = $schema;
232 90 $our_options{_schema_class} = $loadclass;
233 90 $our_options{title} = $options{title};
234 90 $our_options{_schema_options} = { %options };
235 90 return \%our_options;
236 }
237  
238 10 has schema => (is => 'ro',
239 required => 1,
240 isa => Enum[__PACKAGE__->available_schemas]);
241  
242 10 has _schema_class => (is => 'ro',
243 isa => Str,
244 required => 1);
245  
246 10 has _schema_options => (is => 'ro',
247 isa => HashRef,
248 required => 1);
249  
250 10 has imposer => (is => 'rwp',
251 required => 1,
252 handles => [qw/file outfile suffix
253 cover
254 signature
255 computed_signature
256 total_pages
257 orig_width
258 orig_height
259 dimensions
260 total_output_pages
261 /],
262 isa => Object);
263  
264 10 has paper => (is => 'ro',
265 isa => Maybe[Str]);
266  
267 has paper_thickness => (is => 'ro',
268 isa => Str,
269 67 ▶ default => sub { '0.1mm' });
Subroutine __ANON__: called CC=1 SCAR=0.0
270  
271 has cropmark_offset => (is => 'ro',
272 isa => Str,
273 88 ▶ default => sub { '1mm' });
Subroutine __ANON__: called CC=1 SCAR=0.0
274  
275 has cropmark_length => (is => 'ro',
276 isa => Str,
277 87 ▶ default => sub { '12mm' });
Subroutine __ANON__: called CC=1 SCAR=0.0
278  
279 has font_size => (is => 'ro',
280 isa => Str,
281 86 ▶ default => sub { '8pt' });
Subroutine __ANON__: called CC=1 SCAR=0.0
282  
283 10 has title => (is => 'ro',
284 isa => Maybe[Str]);
285  
286 10 has job_name => (is => 'lazy',
287 isa => Str);
288  
289 28 ▶sub _build_job_name {
Subroutine _build_job_name: called CC=2 SCAR=7.6
290 28 my $self = shift;
291 28 ▶ my $name = $self->title || basename($self->file);
Condition33%1/3
$self->title || basename($self->file)
l!l&&r!l&&!r
2800
MC/DC0%0/2
$self->title || basename($self->file)
$self->title basename($self->file)
Truth table33%1/3
A || B
A= $self->titleB= basename($self->file)
ABresult
1X1
011
000
292 28 return $name;
293 }
294  
295 68 ▶sub impose {
Subroutine impose: called CC=3 SCAR=11.0
Pod documented
296 68 my $self = shift;
297 68 my $tmpdir = File::Temp->newdir(CLEANUP => !DEBUG);
298 68 ▶ if (my $cropmark_paper = $self->paper) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cropmark_paper = $self->paper) 28 40 68
299 28 my %imposer_options = %{ $self->_schema_options };
300 # clone the parameter and set outfile and file
301  
302 28 ▶ $imposer_options{outfile} ||= $self->imposer->output_filename;
Condition66%2/3
$imposer_options{"outfile"} ||= $self->imposer->output_filename
l!l&&r!l&&!r
2710
MC/DC0%0/2
$imposer_options{"outfile"} ||= $self->imposer->output_filename
$imposer_options{"outfile"} $self->imposer->output_filename
Truth table66%2/3
A ||= B
A= $imposer_options{"outfile"}B= $self->imposer->output_filename
ABresult
1X1
011
000
303 28 my $crop_output = $imposer_options{file} = File::Spec->catfile($tmpdir, 'with-crop-marks.pdf');
304  
305 # pass it to cropmarks
306 my %crop_args = (
307 title => $self->job_name,
308 input => $self->file,
309 output => $crop_output,
310 paper => $cropmark_paper,
311 cover => $imposer_options{cover},
312 28 signature => $self->imposer->computed_signature,
313 paper_thickness => $self->paper_thickness,
314 cropmark_offset => $self->cropmark_offset,
315 cropmark_length => $self->cropmark_length,
316 font_size => $self->font_size,
317 $self->imposer->cropmarks_options,
318 );
319 28 print Dumper(\%crop_args) if DEBUG;
320  
321 # rebuild the imposer, which should free the memory as well
322 28 $self->_set_imposer($self->_schema_class->new(%imposer_options));
323  
324 28 my $cropper = PDF::Cropmarks->new(%crop_args);
325 28 $cropper->add_cropmarks;
326 24 print "# cropping output in $crop_output\n" if DEBUG;
327 24 print Dumper($self->imposer) if DEBUG;
328 }
329 # in any case impose
330 64 return $self->imposer->impose;
331 }
332  
333 =head2 available_schemas
334  
335 Called on the class (not on the object returned by C<new>) will report
336 the list of available schema.
337  
338 E.g.
339  
340 PDF::Imposition->available_schemas;
341  
342 =cut
343  
344 13 ▶sub available_schemas {
Subroutine available_schemas: called CC=1 SCAR=0.0
Pod documented
345 13 return qw/2up 2down 2side 2x4x2 1x4x2cutfoldbind
346 2x4x1
347 duplex2up
348 4up 1repeat2top 1repeat2side 1repeat4 ea4x4
349 1x8x2 1x1/
350 }
351  
352 =head1 INTERNALS
353  
354 =over 4
355  
356 =item BUILDARGS
357  
358 =item imposer
359  
360 =item DEBUG
361  
362 Constant picked from AMW_DEBUG environment.
363  
364 =back
365  
366 =head1 AUTHOR
367  
368 Marco Pessotto, C<< <melmothx at gmail.com> >>
369  
370 =head1 BUGS
371  
372 Please report any bugs or feature requests to the author's email. If
373 you find a bug, please provide a minimal example file which reproduces
374 the problem (so I can add it to the test suite).
375  
376 Or, at your discretion, feel free to use the CPAN's RT.
377  
378 =head1 SUPPORT
379  
380 You can find documentation for this module with the perldoc command.
381  
382 perldoc PDF::Imposition
383  
384 =head1 REPOSITORY
385  
386 L<https://github.com/melmothx/pdf-imposition-perl>
387  
388 =head1 SEE ALSO
389  
390 =over 4
391  
392 =item psutils
393  
394 L<http://knackered.org/angus/psutils/> (shipped by any decent
395 GNU/Linux distro and in TeXlive!). If you don't bother the
396 PDF->PS->PDF route, it's a great and useful tool which just aged well.
397  
398 =item pdfpages
399  
400 L<http://www.ctan.org/pkg/pdfpages>
401  
402 =item pdfjam
403  
404 L<http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic-research/firth/software/pdfjam/>
405 (buil on the top of pdfpages)
406  
407 =item ConTeXt
408  
409 L<http://wiki.contextgarden.net/Imposition>
410  
411 The names of schemas are taken straight from the ConTeXt ones (if
412 existing), as described in the book I<Layouts in context>, by Willi
413 Egger, Hans Hagen and Taco Hoekwater, 2011.
414  
415 =back
416  
417 =head1 TODO
418  
419 The idea is to provide a wide range of imposition schemas (at least
420 the same provided by ConTeXt). This could require some time. If you
421 want to contribute, feel free to fork the repository and send a pull
422 request or a patch (please include documentation and at some tests).
423  
424 =head1 LICENSE
425  
426 This program is free software; you can redistribute it and/or modify
427 it under the terms of either: the GNU General Public License as
428 published by the Free Software Foundation; or the Artistic License.
429  
430 =cut
431  
432 10 1; # End of PDF::Imposition
« lib/PDF/Imposition/Schema2side.pm ↑ Summary script/pdf-impose.pl »