From cf823dff2658869aede781a8f8485236206ecf55 Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 24 Sep 2011 15:47:54 -0400 Subject: [PATCH] PDF output fixes: Raise an exception for input so corrupt that it cannot be rendered. Fix issue with enormous PDF's being created. Due to who PyPDF works this brings back the out of file decriptors bug. --- src/calibre/ebooks/pdf/writer.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/pdf/writer.py b/src/calibre/ebooks/pdf/writer.py index 05d874c9c3..632ccf230a 100644 --- a/src/calibre/ebooks/pdf/writer.py +++ b/src/calibre/ebooks/pdf/writer.py @@ -173,6 +173,10 @@ class PDFWriter(QObject): # {{{ printer.setOutputFormat(QPrinter.NativeFormat) self.view.print_(printer) printer.abort() + else: + # The document is so corrupt that we can't render the page. + self.loop.exit(0) + raise Exception('Document cannot be rendered.') self._render_book() def _delete_tmpdir(self): @@ -207,11 +211,14 @@ class PDFWriter(QObject): # {{{ try: outPDF = PdfFileWriter(title=self.metadata.title, author=self.metadata.author) for item in self.combine_queue: - with open(item, 'rb') as item_stream: - inputPDF = PdfFileReader(item_stream) - for page in inputPDF.pages: - outPDF.addPage(page) - outPDF.write(self.out_stream) + # The input PDF stream must remain open until the final PDF + # is written to disk. PyPDF references pages added to the + # final PDF from the input PDF on disk. It does not store + # the pages in memory so we can't close the input PDF. + inputPDF = PdfFileReader(open(item, 'rb')) + for page in inputPDF.pages: + outPDF.addPage(page) + outPDF.write(self.out_stream) finally: self._delete_tmpdir() self.loop.exit(0)