Search code examples
python-3.xpdfpdf-generationpypdfpdfrw

How to merge pages of a pdf file into a single vertically combined page with python


I have tried merge_page method in pypdf and pdfrw but they stact one page over other, how do I proceed? below code which i tried, similar with both modules

from pdfrw import PdfReader, PdfWriter, PageMerge

def merge_pdf_pages(input_file='input.pdf'):
    # Read the input PDF file
    reader = PdfReader(input_file)
    writer = PdfWriter()
    pages = PageMerge()
    for page in reader.pages:
        pages.add(page)
    writer.addpage(pages.render())
    with open("output.pdf",'wb') as file:
        writer.write(file)
# Example usage
merge_pdf_pages('input.pdf')

Solution

  • Blending multiple PDF into a single sheet is usually done by trained Graphics Artist, who understand the programming problems related to computerised PDF design.

    Compositors or Imposers will fine tune an "Imposition" or "Composition" using more than one simple method, but in effect it is simply reprint at a different scale (or same scale)

    Here I can graphically adjust the most basic of such "N-UP" methods in a Browser PDF reader, but the complexity will be similar using a single programmed command line.

    Most complex might be 40 pages as Ten Plates where matrix calculations would be used for "Booklet" production in LaTeX using folds, slits and bleed boxes

    enter image description here

    When designing programs for Desk Top "Publications" the maths can be complex, yet simply repetitive using boilerplates. https://tex.stackexchange.com/a/490408

    Since you have given few clues in the question? I have to presume from "one over the other" that means 2 landscape on 1 portrait enter image description here

    The difference between a Program and a Human Composer can be seen here. The Programmatic result is on the Left to Right However the Human Viewer will set those Four sheets via the R2L settings.

    enter image description here

    From comments it was stated the desire is to emulate, output same as continuous view.
    Here on the left we see pages as normally spaced out in the viewer, where in the middle view, the PDF viewer can use "continuous page mode" (with seamless spacing, but still 4 controllable pages) and on the right the output from imposing infinite pages vertically.

    enter image description here

    In this case the command used was

    cpdf -impose-xy "1 0" fourL.pdf -o Output.pdf
    

    where cpdf is a cross platform application for binary command line applications or I believe with a JavaScript API/SDK interface and one for Python (pycpdflib). See https://github.com/coherentgraphics

    specifically (link may become outdated, but currently) https://www.coherentpdf.com/cpdfmanual/cpdfmanualch9.html#x13-860009.2