19 my $defaultcrop = '0.9';
21 Getopt::Long::Configure("bundling");
24 my $usage = <<ENDUSAGE;
25 pdfstitch - Copyright (C) 2017 by Jakob Haufe <sur5r\@sur5r.net>
27 Usage: $0 [-hgpcs] [--genmeta] [--defaultcrop=<factor>] [--preview] [--crop] [--stitch] {PDF file|.stitch file}
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
35 -c, --crop Generate cropped PDF according to given .stitch
36 -s, --stitch Generate stitched PDF
37 (default when called with a .stitch file)
39 pdfstitch is free software under the GNU AGPL version 3. See LICENSE for details.
45 'g|genmeta' => \$genmeta,
46 'd|defaultcrop=s' => \$defaultcrop,
47 'p|preview' => \$preview,
49 's|stitch' => \$stitch
50 ) or die "Call with --help to see available options.\n";
54 die "--genmeta can only be combined with --defaultcrop!\n" if($genmeta and ($preview or $crop or $stitch));
56 die "--defaultcrop can only be combined with --genmeta!\n" if($defaultcrop and ($preview or $crop or $stitch));
58 die "No input file specified!\n" unless $ARGV[0];
60 my $infile = $ARGV[0];
62 die "$infile does not exist!\n" unless -e $infile;
63 die "$infile is not readable!\n" unless -r $infile;
65 if(not ($genmeta or $preview or $crop or $stitch))
67 my $magic = File::LibMagic->new();
70 if($magic->can('info_from_filename'))
72 $mime_type = $magic->info_from_filename($infile)->{mime_type}
76 # Fallback for File::Libmagic below 1.06
77 $mime_type = $magic->checktype_filename($infile);
79 if($mime_type =~ "^application/pdf")
81 print "Detected PDF, turning on --genmeta\n";
84 elsif ($mime_type =~ "^text/plain")
86 YAML::LoadFile($infile) or die "Failed to parse $infile as YAML!\n";
87 print "Detected YAML, turning on --stitch\n";
92 die "$infile has unsupported type: $mime_type\n";
98 print "Generating meta file for " . basename($infile) . ".\n";
99 my $outfile = basename($infile) . ".stitch";
101 die "$outfile exists, aborting!\n" if -e $outfile;
104 my $pdf = PDF::API2->open($infile);
106 my $page = $pdf->openpage(1);
107 my ($llx, $lly, $urx, $ury) = $page->get_mediabox;
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)],
120 foreach $page (1..$pdf->pages)
122 $meta->{pageoffsets}->{$page}->{x} = 0;
123 $meta->{pageoffsets}->{$page}->{y} = 0;
126 YAML::Bless($meta)->keys(['input','x','y','width','height','columns','rows', 'pageorder','pageoffsets']);
127 YAML::DumpFile($outfile,$meta);
131 my $meta = YAML::LoadFile($infile);
132 my $inpdf = PDF::API2->open($meta->{input});
134 if($preview or $crop)
136 my $previewpdf = PDF::API2->new() if $preview;
137 my $croppedpdf = PDF::API2->new() if $crop;
142 $transparency = $previewpdf->egstate();
143 $transparency->transparency(0.8);
146 foreach my $pagenr (@{$meta->{pageorder}})
148 next if $pagenr eq "blank";
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};
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();
166 my $croppage = $croppedpdf->import_page($inpdf, $pagenr, 0);
167 $croppage->cropbox($llx, $lly, $llx + $urx, $lly + $ury);
171 $previewpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-preview.pdf') if $preview;
172 $croppedpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-cropped.pdf') if $crop;
177 my $width = $meta->{width} * $meta->{columns};
178 my $height = $meta->{height} * $meta->{rows};
180 my $stitchedpdf = PDF::API2->new();
182 my $page = $stitchedpdf->page();
183 $page->mediabox($width, $height);
185 my $content = $page->gfx();
189 foreach my $pagenr (@{$meta->{pageorder}})
191 if($pagenr ne "blank")
193 my $xo = $stitchedpdf->importPageIntoForm($inpdf, $pagenr);
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};
200 $xo->bbox($llx, $lly, $urx, $ury);
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);
207 if($column == $meta->{columns})
214 $stitchedpdf->saveas(basename($infile, ('.pdf.stitch','.stitch')) . '-stitched.pdf');