def merge_pdfs(self, final_pdf_path, actions, append_blank_page=True):
"""
Merge pdf files in only one PDF
:param final_pdf_path: file path to save pdf
:param actions: list of tuples, each tuple containing a PDF file path and the degrees of counterclockwise
rotation to perform on the PDF document.
:param append_blank_page: append a blank page between documents if True.
:return:
"""
""" Merge all pdf of a folder in one single file '.pdf'. """
output = PdfFileWriter()
docs_to_close = []
for num_doc, (pdf_file_path, rotation) in enumerate(actions):
if pdf_file_path == final_pdf_path:
continue
if not pdf_file_path:
continue
logging.info(u"Parse '%s'" % pdf_file_path)
try:
document_file = open(pdf_file_path, 'rb')
document = PdfFileReader(document_file, strict=False)
num_pages = document.getNumPages()
except Exception as exc:
logging.exception("Error merging pdf %s: %s" % (pdf_file_path, str(exc)))
raise DocumentClipperError
# Rotation must be performed per page, not per document
for num_page in range(num_pages):
page = document.getPage(num_page)
page = page.rotateCounterClockwise(rotation)
output.addPage(page)
if append_blank_page:
output.addBlankPage()
docs_to_close.append(document_file)
self._write_to_pdf(output, final_pdf_path)
self._close_files(docs_to_close)
评论列表
文章目录