1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<META NAME="generator" CONTENT="lgazmail v1.4G.h">
<TITLE>The Answer Gang 91: Combining multiple PDFs into one</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<!--endcut ==============================================-->
<!-- begin 1 -->
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
height="50" width="60" alt="(?) " border="0"
>Combining multiple PDFs into one</H3>
<p><strong>From Faber Fedor
</strong></p>
<p></strong></p>
<p align="right"><strong>Answered By Ben Okopnik, Yann Vernier
</strong></p>
<blockquote><code><font color="#000033"><br>From the chaos of creation
<br>just the final form survives
<br>-- The World Inside The Crystal, Steve Savitsky
</font></code></blockquote>
<blockquote><em><font color="#000066">We could have just posted the finished script in 2c tips. but there's
juicy perl bits to learn from the crafting. Enjoy.
-- Heather</font></em></blockquote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
Hey Gang,
</STRONG></P>
<P><STRONG>
I was playing with my new scanner last night (under a legacy OS
unfortunately) when I realized a shortcoming: I wanted all of the
scanned pages to be in one PDF file, not in separate ones. Well, to that
end, I threw together this quick and dirty Perl script to do just that.
</STRONG></P>
<P><STRONG>
The script assumes you have Ghostscript and pdf2ps installed. It takes
two arguments: the name of the output file and a directory name that
contains all of the PDFs (which have .pdf extensions) to be combined,
e.g.
</STRONG></P>
<pre><strong> ./combine-pdf.pl test.pdf test/
</strong></pre>
<P><STRONG>
I'm sure you can point out many flaws with the script (like how I grab
the command line arguments and clean up after myself), but that's why
it's "quick and dirty". If/when I clean it up, I'll repost it.
</STRONG></P>
<p align="center">See attached <tt><a href="../misc/tag/combine-pdf-faber1,pl.txt">combine-pdf-faber1,pl.txt</a></tt></p>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
If you don't mind, I'll toss in some ideas.
<IMG SRC="../../gx/dennis/smily.gif" ALT=":)"
height="24" width="20" align="middle"> See my version at the end.
</blockQuote>
<blockquote><pre>#!/usr/bin/perl -w
use strict;
</pre></blockquote>
<blockQuote>
Good idea on both.
</blockQuote>
<blockquote><pre># n21pdf.pl: A quick and dirty little program to convert multiple PDFs
# to one PDF requires pdf2ps and Ghostscript
# written by Faber Fedor (faber@linuxnj.com) 2003-05-27
if (scalar(@ARGV) != 2 ) {
</pre></blockquote>
<blockQuote>
You don't need 'scalar'. Scalar behavior (which is defined by the
comparison operator) would cause the list to return the number of its
members, so "if ( @ARGV != 2 )" works fine.
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
Okay. I was trying to get ptkdbi (my fave Perl debugger) to show me the
scalar value of @ARGV and the only way was with <TT> scalar()</TT>. That's also
what I found in the Perl Bookshelf.
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
This is the same as "$foo = @foo". $foo is going to contain the number
of elements in @foo.
</blockQuote>
<blockquote><pre>my $PDFFILE = shift ;
my $PDFDIR = shift;
</pre></blockquote>
<blockQuote>
You could also just do
</blockQuote>
<blockQuote>
my ( $PDFFILE, $PDFDIR ) = @ARGV;
</blockQuote>
<blockQuote>
Combining declaration and assignment is perfectly valid.
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
Cute. I'll have to remember that.
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
</blockQuote>
<blockquote><pre>chomp($PDFDIR);
</pre></blockquote>
<blockQuote>
No need; the "\n" isn't part of @ARGV.
</blockQuote>
<blockquote><pre>$PDFDIR = $PDFDIR . '/' if substr($PDFDIR, length($PDFDIR)-1) ne '/';
</pre></blockquote>
<blockQuote>
Yikes! You could just say "$PDFDIR .= '<TT>/</TT>'"; an extra slash doesn't hurt
anything (part of the POSIX standard, as it turns out).
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
I know, but I <EM>really</EM> don't like seeing "a_dir//a_file". I always
expect it to fail (although it never does).
<IMG SRC="../../gx/dennis/smily.gif" ALT=":-)"
height="24" width="20" align="middle">
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Yonn]
I'm no Perlist myself, but my first choice would be:
$foo =~ s%/*$%/%;
</blockQuote>
<blockQuote>
Which simply ensures that the string ends with exactly one <TT>/.</TT>
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
<IMG SRC="../../gx/dennis/smily.gif" ALT="<grin>"
height="24" width="20" align="middle"> That's one of the ten most common "Perl newbie" mistakes that
CLPM wizards listed: "Using s/// where tr/// is more appropriate." When
you're substituting strings, think "s///"; for characters, go with
"tr///".
</blockQuote>
<blockquote><pre>tr#/##s
</pre></blockquote>
<blockQuote>
Better yet, just ignore it; multiple slashes work just fine.
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Yonn]
I did say I'm no perlist. Tr to me would be the translation tool, for
replacing characters, including deletion.
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Yonn]
Yep; that's exactly what it does. However, even the standard utils "tr"
can <em> _compress</em> strings - which is exactly what was needed here (note the
"s"queeze modifier at the end.)
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Yonn]
Thank you. It's a modifier I had not learned but should have noticed in
your mail. The script would have to tack a <TT>/</TT> onto the end of the string
before doing that tr.
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
You're welcome. Yep, either that or use the globbing mechanism the way I
did; it eliminates all the hassle.
</blockQuote>
<blockquote><pre>for ( <$dir/*pdf> ){
=head
Here's the beef, Granny! :)
All you get here are the specified files as returned by "sh".
You could also use the actual "glob" keyword which is an alias for the
internal function that implements <shell_expansion> mechanism.
=cut
# Mung individual PDF to heart's content
...
}
</pre></blockquote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Yonn]
I don't know how to apply it
to the <EM>end</EM> of the string, which is very easy given a regular
expression as the substitute command uses. I'm more used to dealing with
sed. Remember, the input data may well look like "<TT>/foo/bar/</TT>" and not
just "bar/".
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
You can't apply it to the end of the string, but then I'd imagine Faber
would be just as unhappy with <TT>////foo/////bar////.</TT> "tr", as above, will
regularize all of that.
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
</blockQuote>
<blockquote><pre>opendir(DIR, $PDFDIR) or die "Can't open directory $PDFDIR: $! \n" ;
</pre></blockquote>
<blockQuote>
Take a look at "perldoc -f glob" or read up on the globbing operator
<*.whatever> in "I/O Operators" in perlop. "opendir" is a little clunky
for things like this.
</blockQuote>
<blockquote><pre> `$PDF2PS $file $outfile` ;
</pre></blockquote>
<blockQuote>
Don't use backticks unless you want the STDOUT output from the command
you invoke. "system" is much better for stuff like this and lets you
check the exit status.
</blockQuote>
<blockQuote>
Note - the following is untested but should work.
</blockQuote>
<p align="center">See attached <tt><a href="../misc/tag/combine-pdf-ben1.pl.txt">combine-pdf-ben1.pl.txt</a></tt></p>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
Thanks, I've cleaned it up and attached it. there's one thing that I
couldn't make work, but first...
</STRONG></P>
<P><STRONG>
(now looking inside Ben's version)
</STRONG></P>
<pre><strong>die "Usage: ", $0 =~ /([^\/]+)$/, " <outfile.pdf> <directory_of_pdf_files>\n"
unless @ARGV == 2;
</strong></pre>
<P><STRONG>
Uh, that regex there. Take $_, match one or more characters that aren't
a <TT>/</TT> up to the end of line and remember it and place it in $0? Huh?
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
Nope - it's exactly the behavior that Jason was talking about. "print"
takes a list - that's why the members are separated by commas. The
"match" operator, =~, says to look in whatever comes before it; "$_"
doesn't require it.
</blockQuote>
<blockquote><pre>print if /gzotz/; # Print $_ if $_ contains "gzotz"
print if $foo =~ /gzotz/; # Print $_ if $foo contains "gzotz"
print $foo if /gzotz/; # Print $foo if $_ contains "gzotz"
</pre></blockquote>
<blockQuote>
So, what I'm doing is looking at what's in "$0", and capturing/returning
the part in the parens as per standard list behavior. It's a cute little
trick.
</blockQuote>
<blockQuote>
I guess I <EM>will</EM> have to do this one soon in my One-Liner articles; it's
a useful little idiom.
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
I had to move a few things around to get it to work. I did have one
problem though
</STRONG></P>
<pre><strong>#convert ps files to a pdf file
system $GS, $GS_ARGS, $filelist
and die "Problem combining files!\n";
</strong></pre>
<P><STRONG>
This did not work no way, no how. I kept getting "<TT>/undefinedfilename</TT>"
from GS no matter how I quoted it (and I used every method I found in
the Perl Bookshelf).
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
Hm. I didn't try it, but -
</blockQuote>
<blockquote><pre>perl -we'$a="ls"; $b="-l"; $c="Docs"; system $a, $b, $c and die "Fooey!\n"'
</pre></blockquote>
<blockQuote>
That works fine. I wonder what "gs"s hangup was. Oh, well - you got it
going, anyway. I guess there's not much of a security issue in handing
it to "sh -c" instead of execvp()ing it in this case: the perms will
take care of all that.
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
To get it to finally work, I did:
</STRONG></P>
<pre><strong>#convert ps files to a pdf file
my $cmd_string = $GS . $GS_ARGS . $filelist ;
system $cmd_string
and die "Problem combining files!\n";
</strong></pre>
<P><STRONG>
<shrug>
</STRONG></P>
<P><STRONG>
Anywho, here's the final (?) working copy:
</STRONG></P>
<p align="center">See attached <tt><a href="../misc/tag/combine-pdf-faber2.pl.txt">combine-pdf-faber2.pl.txt</a></tt></p>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
Cool! Glad I could help.
</blockQuote>
<!-- end 1 -->
<!-- *** BEGIN copyright *** -->
<hr>
<CENTER><SMALL><STRONG>
<h5>
<br>Copyright © 2003
<br>Copying license <A HREF="">http://www.linuxgazette.com/copying.html</A>
<BR>Published in Issue 91 of <i>Linux Gazette</i>, June 2003</H5>
</STRONG></SMALL></CENTER>
<!-- *** END copyright *** -->
<SMALL><CENTER><H6 ALIGN="center">HTML script maintained by
<A HREF="mailto:star@starshine.org">Heather Stern</a> of
Starshine Technical Services,
<A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
</H6></SMALL></CENTER>
<HR>
<!--startcut ======================================================= -->
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0"
><A HREF="../index.html"
><IMG SRC="../../gx/navbar/toc.jpg" align="middle"
ALT="[ Table Of Contents ]" border="0"></A
><A HREF="../lg_answer.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A>
<A HREF="../../tag/bios.html">Meet the Gang</A>
<A HREF="1.html">1</A>
<A HREF="2.html">2</A>
<A HREF="3.html">3</A>
<A HREF="4.html">4</A>
</td>
<td align="left" valign="center"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A
><IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<!--endcut ========================================================= -->
<P> <hr>
<!--startcut ======================================================= -->
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<!--endcut ========================================================= -->
<!--startcut ======================================================= -->
</BODY></HTML>
<!--endcut ========================================================= -->
|