I've got a bunch of PDF files that have been produced two "real" pages to a single PDF page; I'd like to chop these in half and put each half on a separate page. Essentially, I need something thatdoes the exact opposite of pdfnup (or psnup). Google and apt-cache search are giving me no love.

Platform is Linux, open source preferred; as I've got a great pile of these to do something that can be scripted (as opposed to a GUI) would be nice, so I can just give it a list of them and have it chew away.

A pre-existing script isn't the only option, either; if there's sample code to manipulate PDFs in similar ways with a third-party library, I can probably hack it into doing what I want.

link|improve this question
feedback

4 Answers

up vote 4 down vote accepted

You can solve this with the help of Ghostscript. pdftk alone cannot do that (to the best of my knowledge). I'll give you the commandline steps to do this manually. It will be easy to script this as a procedure, also with different parameters for page sizes and page numbers. But you said that you can do that yourself ;-)

How to solve this with the help of Ghostscript...

...and for the fun of it, I've recently done it not with an input file featuring "double-up" pages, but one with "treble-ups". You can read the answer for this case here.

Your case is even simpler. You seem to have something similar to this:

+------------+------------+   ^
|            |            |   |
|      1     |      2     |   |
|            |            | 595 pt
|            |            |   |
|            |            |   |
|            |            |   |
+------------+------------+   v
             ^
            fold
             v
+------------+------------+   ^
|            |            |   |
|      3     |      4     |   |
|            |            | 595 pt
|            |            |   |
|            |            |   |
|            |            |   |
+------------+------------+   v
<---------- 842 pt -------->

You want to create 1 PDF with 4 pages, each of which has the size of 421 pt x 595 pt.

First Step

Let's first extract the left sections from each of the input pages:

gs \
    -o left-sections.pdf \
    -sDEVICE=pdfwrite \
    -g4210x5950 \
    -c "<</PageOffset [0 0]>> setpagedevice" \
    -f double-page-input.pdf

What did these parameters do?

First, know that in PDF 1 inch == 72 points. Then the rest is:

  • -o ...............: Names output file. Implicitely also uses -dBATCH -dNOPAUSE -dSAFER.
  • -sDEVICE=pdfwrite : we want PDF as output format.
  • -g................: sets output media size in pixels. pdfwrite's default resolution is 720 dpi. Hence multiply by 10 to get a match for PageOffset.
  • -c "..............: asks Ghostscript to process the given PostScript code snippet just before the main input file (which needs to follow with -f).
  • <</PageOffset ....: sets shifting of page image on the medium. (Of course, for left pages the shift by [0 0] has no real effect.)
  • -f ...............: process this input file.

Which result did the last command achieve?

This one:

Output file: left-sections.pdf, page 1
+------------+  ^
|            |  |
|     1      |  |
|            |595 pt
|            |  |
|            |  |
|            |  |
+------------+  v

Output file: left-sections.pdf, page 2
+------------+  ^
|            |  |
|     3      |  |
|            |595 pt
|            |  |
|            |  |
|            |  |
+------------+  v
<-- 421 pt -->

Second Step

Next, the right sections:

gs \
    -o right-sections.pdf \
    -sDEVICE=pdfwrite \
    -g4210x5950 \
    -c "<</PageOffset [421 0]>> setpagedevice" \
    -f double-page-input.pdf

Result:

Output file: right-sections.pdf, page 1
+------------+  ^
|            |  |
|     2      |  |
|            |595 pt
|            |  |
|            |  |
|            |  |
+------------+  v

Output file: right-sections.pdf, page 2
+------------+  ^
|            |  |
|     4      |  |
|            |595 pt
|            |  |
|            |  |
|            |  |
+------------+  v
<-- 421 pt -->

Last Step

Now we combine the pages into one file. We could do that with ghostscript as well, but we'll use pdftk instead, because it's faster for this job:

pdftk \
  A=right-sections.pdf \
  B=left-sections.pdf \
  cat A1 B1 A2 B2 \
  output single-pages-output.pdf
  verbose

Done. Here is the desired result. 4 different pages, sized 421x595 pt.

Result:

+------------+ +------------+ +------------+ +------------+   ^
|            | |            | |            | |            |   |
|     1      | |     2      | |     3      | |     4      |   |
|            | |            | |            | |            |5595 pt
|            | |            | |            | |            |   |
|            | |            | |            | |            |   |
|            | |            | |            | |            |   |
+------------+ +------------+ +------------+ +------------+   v
<-- 421 pt --> <-- 421 pt --> <-- 421 pt --> <-- 421 pt -->
link|improve this answer
@Unknown: Thanks for the downvoting! Would you please care to write a comment indicating some reason for this? – pipitas May 30 '11 at 20:10
+1 for awesome use of ASCII art, and very clear instructions. Just cause i'm a CLI n00b, the \ s escape the lines so its easier to read, right? – Journeyman Geek Jun 28 '11 at 6:20
@Journeyman Geek: the ` tell the command line interpreter *"This is not the real end of the line+command -- command continues on the next line."* You can copy all the lines (including the ` line continuation signs) into your terminal window and it should work once you hit [enter]... – pipitas Jun 28 '11 at 9:56
feedback

So, after a lot more searching (it seems that "PDF cut pages" is a far better search), I found a little script called unpnup which uses poster, PDF/PS conversion, and pdftk to do exactly what I need. It's a bit of a long way around, but it's far superior to the other methods I found (such as using imagemagick) because it doesn't rasterise the pages before spitting them out.

Just in case mobileread goes away for some reason, the core of the script (licenced under the GPLv2 or later by Harald Hackenberg <hackenberggmx.at>) is as follows:

pdftk "$1" burst
for file in pg*.pdf;
do
    pdftops -eps $file
    poster -v -pA4 -mA5 -c0% `basename $file .pdf`.eps > `basename $file .pdf`.tps
    epstopdf `basename $file .pdf`.tps
done
pdftk pg*.pdf cat output ../`basename $1 .pdf`_unpnuped.pdf
link|improve this answer
1  
Gotta love it when people answer their own questions. However, if you needed to do it with a GUI, especially if the pages sizes weren't even or you wanted to further crop each side, check out Briss: briss.sourceforge.net – frabjous Sep 22 '10 at 20:10
You should be able to do what you want with PDFTK by itself, without all the conversions. – CarlF Sep 22 '10 at 20:51
@CarlF: I thought it'd be possible, but I can't see anything in the PDFTK man page to manipulate the contents of pages. Got any pointers for me? – womble Sep 23 '10 at 6:38
@frabjous: What's wrong with answering your own questions? – pipitas Sep 24 '10 at 12:07
@womble: your conversions go via PS/EPS. This is bound to lead to losses in quality (embedded fonts, transparencies, etc.). My suggestion avoids the risky PDF => EPS => PDF route and goes the safer PDF => PDF => PDF way. – pipitas Sep 24 '10 at 12:10
show 1 more comment
feedback

Based on piptas' answer above:

On windows, for splitting letter-size PDFs with a single cover image at start, the following worked great for me (note the use of [-612 0] in the second step, a positive value created blank pages because it pushed the wrong way.)

gswin32c -o left-sections.pdf -sDEVICE=pdfwrite -dFirstPage=2 -g6120x7920 -c "<</PageOffset [0 0]>> setpagedevice" -f input.pdf

Note the use of -dFirstPage=2 which instructs gs to begin processing on page 2.

gswin32c -o right-sections.pdf -sDEVICE=pdfwrite -dFirstPage=2 -g6120x7920 -c "<</PageOffset [-612 0]>> setpagedevice" -f input.pdf

This creates right-sections.pdf the same way. And now the cover image:

gswin32c -o cover.pdf -sDEVICE=pdfwrite -dLastPage=1 -g6120x7920 -c "<</PageOffset [0 0]>> setpagedevice" -f input.pdf

Next, since I didn't want to merge with pdftk using manual page input, I split the left and right sections into separate PDFs in a new directory.

mkdir input_file
copy cover.pdf input_file\0000.pdf
pdftk left-sections.pdf burst output input_file\%04d_A.pdf
pdftk right-sections.pdf burst output input_file\%04d_B.pdf

Then I join the PDFs in that directory, alphabetically (and luckily that means they're sorted in the right order!) and I also run the result through ghostscript again to fix "Warning: Generation number out of 0..65535 range, assuming 0." errors produced by pdftk which ghostscript called "itext-paulo-155 (itextpdf.sf.net-lawagie.com)" -- it also happened to cut file size in half in my usage. With a 4.5MB original, pdftk's result was 6.7MB and gswin32c's reprocessing reduced that to 3.2 MB.

pdftk input_file\*.pdf cat output input_temp.pdf
gswin32c -o final_output.pdf -sDEVICE=pdfwrite -f input_temp.pdf

And we're done! Feel free to delete the input_file folder, cover.pdf, input_temp.pdf, right_sections.pdf and left_sections.pdf. ;-)

link|improve this answer
feedback

There is a tool pdfposter which can be used to create PDFs with several pages for one input page (tiling or chopping the pages). It is similar to the tool poster, which does the same for PostScript files.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.