Skip to content

Commit

Permalink
merge docs:fix bug due to closing files before writting
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmadurga committed Dec 7, 2017
1 parent fc447fc commit 332a013
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions document_clipper/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def _write_to_pdf(self, output, path):
output.write(output_stream)
output_stream.close()

def _close_files(self, files_to_close):
for file in files_to_close:
file.close()

def image_to_pdf(self, img, pdf_path=None, **kwargs):
"""
Convert image to pdf.
Expand Down Expand Up @@ -214,6 +218,8 @@ def merge_pdfs(self, final_pdf_path, actions, append_blank_page=True):

output = PdfFileWriter()

docs_to_close = []

for num_doc, (pdf_file_path, rotation) in enumerate(actions):
if pdf_file_path == final_pdf_path:
continue
Expand All @@ -230,19 +236,23 @@ def merge_pdfs(self, final_pdf_path, actions, append_blank_page=True):
except Exception as exc:
logging.exception("Error merging pdf %s: %s" % (pdf_file_path, str(exc)))
raise DocumentClipperError
with document_file:
# 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)
# 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()

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)


def merge(self, final_pdf_path, actions, append_blank_page=False):
"""
Merge files (images and pdfs) in to one PDF
Expand Down

0 comments on commit 332a013

Please sign in to comment.