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

7,165

Solution 1

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)

Solution 2

I managed to solve my original problem using a gs solution based on the answer to this question on superuser.com, found following a link within another Q/A refered to in @Stone's answer to the present question.

Here's a way it can be done, put into a script:

#!/bin/bash
################################################################
# Make two portrait A4 sheets out of one landscape A4 sheet, by
# splitting and resizing adequately
################################################################
# solution based on: 
# https://superuser.com/questions/235074/freeware-to-split-a-pdfs-pages-down-the-middle/235401#235401
################################################################
IN=$1
OUT=$2
TMPLEFT=/tmp/left.pdf
TMPRIGHT=/tmp/right.pdf

# left side
gs -o $TMPLEFT -sDEVICE=pdfwrite -g5955x8420 -c "<</PageOffset [0 0]>>" setpagedevice -f $IN
# right side
gs -o $TMPRIGHT -sDEVICE=pdfwrite -g5955x8420 -c "<</PageOffset [-595.5 0]>>" setpagedevice -f $IN
# join
pdfunite $TMPLEFT $TMPRIGHT $OUT

I still think there should be an easier way, but this works and avoids ImageMagick convert's shortcomings.

Solution 3

Use PDFjam to do this.

See: https://superuser.com/questions/246092/how-to-convert-a-1-page-pdf-to-a-2-page-per-sheet-pdf

Share:
7,165

Related videos on Youtube

Dalker
Author by

Dalker

Updated on September 18, 2022

Comments

  • Dalker
    Dalker over 1 year

    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.

    • Admin
      Admin almost 12 years
      Do not use the imagemagick tools like convert. They rasterize the output and degrade the quality.
    • Admin
      Admin over 11 years
      @Marco Thanks for the tip! Do you have a suggestion for an alternative?
  • Dalker
    Dalker over 11 years
    From the man page and --help of pdfjam, I understand how to join two pdf files/pages/sheets into one. But I want to do the opposite: split one page into two. Is that actually possible?
  • Dalker
    Dalker over 11 years
    This worked when commenting out p.scaleBy(math.sqrt(2)), which for some reason produced a blank output pdf. Fortunately, evince is smart enough to print the resulting split pdf in full A4 anyway, so this did answer my question and avoids using any explicit geometry, which is a really good thing. Note on ArchLinux: first line must be explicitly #!/usr/bin/env python2 as python3 is the default.