20 Getopt::Long::Configure("bundling");
23 my $usage = <<ENDUSAGE;
24 pdfstitch - Copyright (C) 2017 by Jakob Haufe <sur5r\@sur5r.net>
26 Usage: $0 [-hgpcs] [--genmeta] [--preview] [--crop] [--stitch] {PDF file|.stitch file}
28 -h, --help Display this message
29 -g, --genmeta Generate .stitch file for stitching based on given PDF
30 (default when called with a PDF)
31 -p, --preview Generate preview PDF containing overlays to analyze
33 -c, --crop Generate cropped PDF according to given .stitch
34 -s, --stitch Generate stitched PDF
35 (default when called with a .stitch file)
37 pdfstitch is free software under the GNU AGPL version 3. See LICENSE for details.
43 'g|genmeta' => \$genmeta,
44 'p|preview' => \$preview,
46 's|stitch' => \$stitch
47 ) or die "Call with --help to see available options.\n";
51 die "--genmeta can not be combined with other actions!\n" if($genmeta and ($preview or $crop or $stitch));
53 die "No input file specified!\n" unless $ARGV[0];
55 my $infile = $ARGV[0];
57 die "$infile does not exist!\n" unless -e $infile;
58 die "$infile is not readable!\n" unless -r $infile;
60 if(not ($genmeta or $preview or $crop or $stitch))
62 my $magic = File::LibMagic->new();
65 if($magic->can('info_from_filename'))
67 $mime_type = $magic->info_from_filename($infile)->{mime_type}
71 # Fallback for File::Libmagic below 1.06
72 $mime_type = $magic->checktype_filename($infile);
74 if($mime_type =~ "^application/pdf")
76 print "Detected PDF, turning on --genmeta\n";
79 elsif ($mime_type =~ "^text/plain")
81 YAML::LoadFile($infile) or die "Failed to parse $infile as YAML!\n";
82 print "Detected YAML, turning on --stitch\n";
87 die "$infile has unsupported type: $mime_type\n";
93 print "Generating meta file for " . basename($infile) . ".\n";
94 my $outfile = basename($infile) . ".stitch";
96 die "$outfile exists, aborting!\n" if -e $outfile;
99 my $pdf = PDF::API2->open($infile);
101 my $page = $pdf->openpage(1);
102 my ($llx, $lly, $urx, $ury) = $page->get_mediabox;
105 input => basename($infile),
106 x => (($urx - $llx)*0.1)/2,
107 y => (($ury - $lly)*0.1)/2,
108 width => ($urx - $llx)*0.9,
109 height => ($ury - $lly)*0.9,
110 columns => int(sqrt($pdf->pages)),
111 rows => int(sqrt($pdf->pages)),
112 pageorder => [(1 .. $pdf->pages)],
115 foreach $page (1..$pdf->pages)
117 $meta->{pageoffsets}->{$page}->{x} = 0;
118 $meta->{pageoffsets}->{$page}->{y} = 0;
121 YAML::Bless($meta)->keys(['input','x','y','width','height','columns','rows', 'pageorder','pageoffsets']);
122 YAML::DumpFile($outfile,$meta);
126 my $meta = YAML::LoadFile($infile);
127 my $inpdf = PDF::API2->open($meta->{input});
129 if($preview or $crop)
131 my $previewpdf = PDF::API2->new() if $preview;
132 my $croppedpdf = PDF::API2->new() if $crop;
137 $transparency = $previewpdf->egstate();
138 $transparency->transparency(0.8);
141 foreach my $pagenr (@{$meta->{pageorder}})
143 next if $pagenr eq "blank";
145 my $llx = $meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x};
146 my $lly = $meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y};
147 my $urx = $meta->{width};
148 my $ury = $meta->{height};
152 my $previewpage = $previewpdf->import_page($inpdf, $pagenr, 0);
153 my $previewcontent = $previewpage->gfx();
154 $previewcontent->egstate($transparency);
155 $previewcontent->rect($llx, $lly, $urx, $ury);
156 $previewcontent->fillcolor('%F000');
157 $previewcontent->fill();
161 my $croppage = $croppedpdf->import_page($inpdf, $pagenr, 0);
162 $croppage->cropbox($llx, $lly, $llx + $urx, $lly + $ury);
166 $previewpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-preview.pdf') if $preview;
167 $croppedpdf->saveas(basename($infile, ('.pdf.stitch', '.stitch')) . '-cropped.pdf') if $crop;
172 my $width = $meta->{width} * $meta->{columns};
173 my $height = $meta->{height} * $meta->{rows};
175 my $stitchedpdf = PDF::API2->new();
177 my $page = $stitchedpdf->page();
178 $page->mediabox($width + 100, $height + 100);
180 my $content = $page->gfx();
184 foreach my $pagenr (@{$meta->{pageorder}})
186 if($pagenr ne "blank")
188 my $xo = $stitchedpdf->importPageIntoForm($inpdf, $pagenr);
190 my $llx = $meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x};
191 my $lly = $meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y};
192 my $urx = $llx + $meta->{width};
193 my $ury = $lly + $meta->{height};
195 $xo->bbox($llx, $lly, $urx, $ury);
197 my $xpos = ($column - 1) * $meta->{width};
198 my $ypos = $height - ($row * $meta->{height});
199 $content->formimage($xo, $xpos, $ypos);
202 if($column > $meta->{columns})
209 $stitchedpdf->saveas(basename($infile, ('.pdf.stitch','.stitch')) . '-stitched.pdf');