Answers:
如果您愿意编写一些Python ,PyPdf库使这种事情变得容易。将下面的代码保存在一个名为pdf-cat-even
(或您喜欢的脚本)的脚本中,使其可执行(chmod +x pdf-cat-even
),然后作为过滤器(./pdf-cat-even a.pdf b.pdf >concatenated.pdf
)运行。该addBlankPage
方法需要pyPdf≥1.13 。
#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
output_page_number = 0
alignment = 2 # to align on even pages
for filename in sys.argv[1:]:
# This code is executed for every file in turn
input = PdfFileReader(open(filename))
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
# This code is executed for every input page in turn
output.addPage(p)
output_page_number += 1
while output_page_number % alignment != 0:
output.addBlankPage()
output_page_number += 1
output.write(sys.stdout)
./pdf-cat-even $(cat list-of-file-names.txt) >concatenated.pdf
<list-of-file-names.txt tr '\n' '\0' | xargs -0 ./pdf-cat-even >concatenated.pdf
第一步是产生一个空白页的pdf文件。您可以使用许多程序(LibreOffice / OpenOffice,inkscape,(La)TeX,scribus等)轻松完成此操作
然后仅在需要的地方包括这个空白页:
pdftk A.pdf empty_page.pdf B.pdf output result.pdf
如果要使用脚本自动执行此操作,则可以使用例如pdftk file.pdf dump_data | grep NumberOfPages | egrep -o '[0-9]*'
提取页数。
pdftk --version
返回pdftk 1.44。我记得我那些比较精打细算的朋友花了至少15分钟的时间尝试各种事情来完成这项工作,然后放弃了。
Gilles的回答对我有用,但是由于我必须合并许多文件,因此如果我可以从文本文件中读取它们的名称,则更加方便。我稍微修改了Gilles的代码来做到这一点,也许会对其他人有所帮助:
#!/usr/bin/env python
# requires PyPdf library, version 1.13 or above -
# its homepage is http://pybrary.net/pyPdf/
# running: ./this-script-name file-with-pdf-list > output.pdf
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
output_page_number = 0
# every new file should start on (n*alignment + 1)th page
# (with value 2 this means starting always on an odd page)
alignment = 2
listoffiles = open(sys.argv[1]).read().splitlines()
for filename in listoffiles:
# This code is executed for every file in turn
input = PdfFileReader(open(filename))
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
# This code is executed for every input page in turn
output.addPage(p)
output_page_number += 1
while output_page_number % alignment != 0:
output.addBlankPage()
output_page_number += 1
output.write(sys.stdout)
您也可以使用LaTeX进行此操作(尽管我知道这可能不是您想要的)。类似于以下内容的东西应该起作用:
\documentclass{book}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=-]{A}
\cleardoublepage % Make sure we clear to an odd page
\includepdf[pages=-]{B} % This inserts all pages. Or you can specify specific pages, a range, or `{}` for a blank page
\end{document}
请注意,\cleardoublepage
仅插入带有双面打印类的空白页(例如书本)
pdfpages
可以在CTAN上找到更多选项和信息。
\includepdf[pages=-]{...}
。
\cleardoublepage
如果您使用的是用于双面打印的类,则仅插入空白页。我正在使用无效的文章;我已修复它并更新了问题以反映这一点。
\includepdf
默认情况下仅包括第一页(并非所有页面)。\documentclass[twoside]{article}
也可以。
这是PyPDF2和python3的代码
#!/usr/bin/env python
# requires PyPdf2 library, version 1.26 or above -
# its homepage is https://pythonhosted.org/PyPDF2/index.html
# running: ./this-script-name output.pdf file-with-pdf-list
import copy, sys
from PyPDF2 import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
output_page_number = 0
# every new file should start on (n*alignment + 1)th page
# (with value 2 this means starting always on an odd page)
alignment = 2
for filename in sys.argv[2:]:
# This code is executed for every file in turn
input = PdfFileReader(open(filename, "rb"))
output.appendPagesFromReader(input)
output_page_number += input.getNumPages()
while output_page_number % alignment != 0:
output.addBlankPage()
output_page_number += 1
output.write(open(sys.argv[1], "wb"))