Convert single-page landscape pdf into rescaled double-page portrait pdf

conversionimage manipulationimagemagickpdfprinting

I'm having trouble using convert to do the following:

I have a bunch of single-page pdf files whose geometry corresponds to a landscape A4 sheet. The actual content is visually split in 2 pages. What I've been trying to do with each of these files, is basically: resize to A3, actually split vertically in the middle to get two pages (I've tried convert's crop unsuccesfully), then re-assemble as a 2-page pdf file made of 2 A4 portrait-oriented pages.

The final content should be the original content resized by a factor sqrt(2)

[  ] -> [    ]  ->  [ | ]
        [    ]      [ | ]
 A4       A3         2xA4
lands.   lands.     portrait

The whole purpose of this is to be able to print the resized content as 2 portrait A4 sheets instead of 1 landscape A4, but actually creating new pdf files would be better than printing directly, as they can then be reprinted anytime and shared with other people who'll then be able to print them directly as intended as well.

Best Answer

Here's a variant of un2up, which uses Python with the pyPdf library. Note that you need at least version 1.13 (prior versions did not support scaling). Untested.

#!/usr/bin/env python
import copy, math, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
    p.scaleBy(math.sqrt(2))
    q = copy.copy(p)
    (w, h) = p.mediaBox.upperRight
    p.mediaBox.upperRight = (w/2, h)
    q.mediaBox.upperLeft = (w/2, h)
    output.addPage(p)
    output.addPage(q)
output.write(sys.stdout)
Related Question