]> git.sur5r.net Git - pdfstitch/blob - pdfstitch
New parameter --defaultcrop to define genmeta crop factor
[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::DumpFile($outfile,$meta);
128 }
129 else
130 {
131     my $meta = YAML::LoadFile($infile);
132     my $inpdf = PDF::API2->open($meta->{input});
133
134     if($preview or $crop)
135     {
136         my $previewpdf = PDF::API2->new() if $preview;
137         my $croppedpdf = PDF::API2->new() if $crop;
138         my $transparency;
139
140         if($preview)
141         {
142             $transparency = $previewpdf->egstate();
143             $transparency->transparency(0.8);
144         }
145
146         foreach my $pagenr (@{$meta->{pageorder}})
147         {
148             next if $pagenr eq "blank";
149
150             my $llx = $meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x};
151             my $lly = $meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y};
152             my $urx = $meta->{width};
153             my $ury = $meta->{height};
154
155             if($preview)
156             {
157                 my $previewpage = $previewpdf->import_page($inpdf, $pagenr, 0);
158                 my $previewcontent = $previewpage->gfx();
159                 $previewcontent->egstate($transparency);
160                 $previewcontent->rect($llx, $lly, $urx, $ury);
161                 $previewcontent->fillcolor('%F000');
162                 $previewcontent->fill();
163             }
164             if($crop)
165             {
166                 my $croppage = $croppedpdf->import_page($inpdf, $pagenr, 0);
167                 $croppage->cropbox($llx, $lly, $llx + $urx, $lly + $ury);
168             }
169         }
170
171         $previewpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-preview.pdf') if $preview;
172         $croppedpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-cropped.pdf') if $crop;
173     }
174
175     if($stitch)
176     {
177         my $width = $meta->{width} * $meta->{columns};
178         my $height = $meta->{height} * $meta->{rows};
179
180         my $stitchedpdf = PDF::API2->new();
181
182         my $page = $stitchedpdf->page();
183         $page->mediabox($width, $height);
184
185         my $content = $page->gfx();
186         my $column = 0;
187         my $row = 0;
188
189         foreach my $pagenr (@{$meta->{pageorder}})
190         {
191             if($pagenr ne "blank")
192             {
193                 my $xo = $stitchedpdf->importPageIntoForm($inpdf, $pagenr);
194
195                 my $llx = $meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x};
196                 my $lly = $meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y};
197                 my $urx = $llx + $meta->{width};
198                 my $ury = $lly + $meta->{height};
199
200                 $xo->bbox($llx, $lly, $urx, $ury);
201
202                 my $xpos = ($column) * $meta->{width} - ($meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x});
203                 my $ypos = $height - (($row+1) * $meta->{height}) - ($meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y});
204                 $content->formimage($xo, $xpos, $ypos);
205             }
206             $column++;
207             if($column == $meta->{columns})
208             {
209                 $row++;
210                 $column=0;
211             }
212         }
213
214         $stitchedpdf->saveas(basename($infile, ('.pdf.stitch','.stitch')) . '-stitched.pdf');
215     }
216 }
217