如何使用Python将PDF文件的页面合并为单个垂直组合页面

huangapple go评论91阅读模式
英文:

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

问题

我尝试了在pypdf和pdfrw中使用merge_page方法,但它们将一页叠加在另一页上,我该如何继续?
我尝试过的以下代码在这两个模块中都类似:

from pdfrw import PdfReader, PdfWriter, PageMerge

def merge_pdf_pages(input_file='input.pdf'):
    # 读取输入的PDF文件
    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)
# 示例用法
merge_pdf_pages('input.pdf')
英文:

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')

答案1

得分: 1

将多个PDF文件合并到一张单独的页面通常由受过训练的图形设计师完成,他们了解与计算机化PDF设计相关的编程问题。

合成器或排版师将使用多个简单方法微调“拼版”或“组合”,但实际上只是以不同的比例(或相同比例)重新打印。

在这里,我可以在浏览器的PDF阅读器中以最基本的“N-UP”方法进行图形调整,但使用单个编程命令的复杂性将类似。

最复杂的情况可能是40页,作为十个版式,将使用矩阵计算来进行LaTeX中的“小册子”制作,其中包括折页、裂缝和出血框。

在为桌面“出版物”设计程序时,数学可能会很复杂,但使用样板可以使其简单而重复。https://tex.stackexchange.com/a/490408

由于问题中提供了很少的线索?我必须从“一个在另一个上面”的意思推断出,这意味着2个横向放置在1个纵向上。

程序和人类作曲家之间的区别可以在这里看到。程序生成的结果是从左到右的,但是人类观众将通过R2L设置来设置这四张纸。

从评论中可以看出,希望模拟的输出与连续视图相同。
在左侧,我们看到页面通常在查看器中间隔排开,而在中间视图中,PDF查看器可以使用“连续页面模式”(具有无缝间距,但仍然有4个可控页面),在右侧,输出是通过垂直排列无限多个页面产生的。

在这种情况下,使用的命令是

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

其中cpdf是一个跨平台的二进制命令行应用程序,也可以使用JavaScript API/SDK接口和Python(pycpdflib)进行操作。请参阅https://github.com/coherentgraphics

具体信息(链接可能会过时,但目前可用)请参见https://www.coherentpdf.com/cpdfmanual/cpdfmanualch9.html#x13-860009.2

如何使用Python将PDF文件的页面合并为单个垂直组合页面
如何使用Python将PDF文件的页面合并为单个垂直组合页面
如何使用Python将PDF文件的页面合并为单个垂直组合页面
如何使用Python将PDF文件的页面合并为单个垂直组合页面

英文:

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

如何使用Python将PDF文件的页面合并为单个垂直组合页面

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
如何使用Python将PDF文件的页面合并为单个垂直组合页面

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.

如何使用Python将PDF文件的页面合并为单个垂直组合页面

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.

如何使用Python将PDF文件的页面合并为单个垂直组合页面

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

huangapple
  • 本文由 发表于 2023年7月28日 02:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定