]> git.sur5r.net Git - pdfstitch/blob - pdfstitch
Order page offsets numerically in YAML file
[pdfstitch] / pdfstitch
1 #!/usr/bin/env perl
2
3 use 5.20.0;
4 use strict;
5 use utf8;
6 use warnings qw(all);
7
8 use File::Basename;
9 use File::LibMagic;
10 use Getopt::Long;
11 use PDF::API2;
12 use YAML;
13
14 my $help = '';
15 my $genmeta = '';
16 my $preview = '';
17 my $crop = '';
18 my $stitch = '';
19 my $defaultcrop = '0.9';
20
21 Getopt::Long::Configure("bundling");
22
23
24 my $usage = <<ENDUSAGE;
25 pdfstitch - Copyright (C) 2017 by Jakob Haufe <sur5r\@sur5r.net>
26
27 Usage: $0 [-hgpcs] [--genmeta] [--defaultcrop=<factor>] [--preview] [--crop] [--stitch] {PDF file|.stitch file}
28
29  -h, --help             Display this message
30  -g, --genmeta          Generate .stitch file for stitching based on given PDF
31                         (default when called with a PDF)
32  -d, --defaultcrop=0.9  Set default crop factor for genmeta (defaults to 0.9)
33  -p, --preview          Generate preview PDF containing overlays to analyze
34                         cropping
35  -c, --crop             Generate cropped PDF according to given .stitch
36  -s, --stitch           Generate stitched PDF
37                         (default when called with a .stitch file)
38
39 pdfstitch is free software under the GNU AGPL version 3. See LICENSE for details.
40 ENDUSAGE
41
42 GetOptions
43 (
44     'h|help' => \$help,
45     'g|genmeta' => \$genmeta,
46     'd|defaultcrop=s' => \$defaultcrop,
47     'p|preview' => \$preview,
48     'c|crop' => \$crop,
49     's|stitch' => \$stitch
50 ) or die "Call with --help to see available options.\n";
51
52 die $usage if $help;
53
54 die "--genmeta can only be combined with --defaultcrop!\n" if($genmeta and ($preview or $crop or $stitch));
55
56 die "--defaultcrop can only be combined with --genmeta!\n" if($defaultcrop and ($preview or $crop or $stitch));
57
58 die "No input file specified!\n" unless $ARGV[0];
59
60 my $infile = $ARGV[0];
61
62 die "$infile does not exist!\n" unless -e $infile;
63 die "$infile is not readable!\n" unless -r $infile;
64
65 if(not ($genmeta or $preview or $crop or $stitch))
66 {
67     my $magic = File::LibMagic->new();
68     my $mime_type;
69
70     if($magic->can('info_from_filename'))
71     {
72         $mime_type = $magic->info_from_filename($infile)->{mime_type}
73     }
74     else
75     {
76         # Fallback for File::Libmagic below 1.06
77         $mime_type = $magic->checktype_filename($infile);
78     }
79     if($mime_type =~ "^application/pdf")
80     {
81         print "Detected PDF, turning on --genmeta\n";
82         $genmeta = 1;
83     }
84     elsif ($mime_type =~  "^text/plain")
85     {
86         YAML::LoadFile($infile) or die "Failed to parse $infile as YAML!\n";
87         print "Detected YAML, turning on --stitch\n";
88         $stitch = 1;
89     }
90     else
91     {
92         die "$infile has unsupported type: $mime_type\n";
93     }
94 }
95
96 if($genmeta)
97 {
98     print "Generating meta file for " . basename($infile) . ".\n";
99     my $outfile = basename($infile) . ".stitch";
100
101     die "$outfile exists, aborting!\n" if -e $outfile;
102
103
104     my $pdf = PDF::API2->open($infile);
105
106     my $page = $pdf->openpage(1);
107     my ($llx, $lly, $urx, $ury) = $page->get_mediabox;
108
109     my $meta = {
110         input => basename($infile),
111         x => (($urx - $llx)*(1-$defaultcrop))/2,
112         y => (($ury - $lly)*(1-$defaultcrop))/2,
113         width => ($urx - $llx)*$defaultcrop,
114         height => ($ury - $lly)*$defaultcrop,
115         columns => int(sqrt($pdf->pages)),
116         rows => int(sqrt($pdf->pages)),
117         pageorder => [(1 .. $pdf->pages)],
118     };
119
120     foreach $page (1..$pdf->pages)
121     {
122         $meta->{pageoffsets}->{$page}->{x} = 0;
123         $meta->{pageoffsets}->{$page}->{y} = 0;
124     }
125
126     YAML::Bless($meta)->keys(['input','x','y','width','height','columns','rows', 'pageorder','pageoffsets']);
127     YAML::Bless($meta->{pageoffsets})->keys([1..$pdf->pages]);
128     YAML::DumpFile($outfile,$meta);
129 }
130 else
131 {
132     my $meta = YAML::LoadFile($infile);
133     my $inpdf = PDF::API2->open($meta->{input});
134
135     if($preview or $crop)
136     {
137         my $previewpdf = PDF::API2->new() if $preview;
138         my $croppedpdf = PDF::API2->new() if $crop;
139         my $transparency;
140
141         if($preview)
142         {
143             $transparency = $previewpdf->egstate();
144             $transparency->transparency(0.8);
145         }
146
147         foreach my $pagenr (@{$meta->{pageorder}})
148         {
149             next if $pagenr eq "blank";
150
151             my $llx = $meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x};
152             my $lly = $meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y};
153             my $urx = $meta->{width};
154             my $ury = $meta->{height};
155
156             if($preview)
157             {
158                 my $previewpage = $previewpdf->import_page($inpdf, $pagenr, 0);
159                 my $previewcontent = $previewpage->gfx();
160                 $previewcontent->egstate($transparency);
161                 $previewcontent->rect($llx, $lly, $urx, $ury);
162                 $previewcontent->fillcolor('%F000');
163                 $previewcontent->fill();
164             }
165             if($crop)
166             {
167                 my $croppage = $croppedpdf->import_page($inpdf, $pagenr, 0);
168                 $croppage->cropbox($llx, $lly, $llx + $urx, $lly + $ury);
169             }
170         }
171
172         $previewpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-preview.pdf') if $preview;
173         $croppedpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-cropped.pdf') if $crop;
174     }
175
176     if($stitch)
177     {
178         my $width = $meta->{width} * $meta->{columns};
179         my $height = $meta->{height} * $meta->{rows};
180
181         my $stitchedpdf = PDF::API2->new();
182
183         my $page = $stitchedpdf->page();
184         $page->mediabox($width, $height);
185
186         my $content = $page->gfx();
187         my $column = 0;
188         my $row = 0;
189
190         foreach my $pagenr (@{$meta->{pageorder}})
191         {
192             if($pagenr ne "blank")
193             {
194                 my $xo = $stitchedpdf->importPageIntoForm($inpdf, $pagenr);
195
196                 my $llx = $meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x};
197                 my $lly = $meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y};
198                 my $urx = $llx + $meta->{width};
199                 my $ury = $lly + $meta->{height};
200
201                 $xo->bbox($llx, $lly, $urx, $ury);
202
203                 my $xpos = ($column) * $meta->{width} - ($meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x});
204                 my $ypos = $height - (($row+1) * $meta->{height}) - ($meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y});
205                 $content->formimage($xo, $xpos, $ypos);
206             }
207             $column++;
208             if($column == $meta->{columns})
209             {
210                 $row++;
211                 $column=0;
212             }
213         }
214
215         $stitchedpdf->saveas(basename($infile, ('.pdf.stitch','.stitch')) . '-stitched.pdf');
216     }
217 }
218