beginnings of working comic to pdf output.

This commit is contained in:
John Schember 2009-04-28 21:33:35 -04:00
parent ed13e58801
commit ee777c539a
2 changed files with 42 additions and 29 deletions

View File

@ -17,7 +17,7 @@ from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation OptionRecommendation
from calibre.ebooks.oeb.output import OEBOutput from calibre.ebooks.oeb.output import OEBOutput
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.ebooks.pdf.writer import PDFWriter, PDFMetadata from calibre.ebooks.pdf.writer import PDFWriter, ImagePDFWriter, PDFMetadata
from calibre.ebooks.pdf.pageoptions import UNITS, PAPER_SIZES, \ from calibre.ebooks.pdf.pageoptions import UNITS, PAPER_SIZES, \
ORIENTATIONS ORIENTATIONS
@ -49,35 +49,41 @@ class PDFOutput(OutputFormatPlugin):
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
self.input_plugin, self.opts, self.log = input_plugin, opts, log self.input_plugin, self.opts, self.log = input_plugin, opts, log
self.output_path = output_path
self.metadata = oeb_book.metadata
if input_plugin.is_image_collection: if input_plugin.is_image_collection:
self.convert_images(input_plugin.get_images(), output_path) self.convert_images(input_plugin.get_images())
else: else:
self.convert_text(oeb_book, output_path) self.convert_text(oeb_book)
def convert_images(self, images, output_path): def convert_images(self, images):
raise NotImplementedError() # process images to document size
self.write(ImagePDFWriter, images)
def convert_text(self, oeb_book, output_path): def convert_text(self, oeb_book):
with TemporaryDirectory('_pdf_out') as oebdir: with TemporaryDirectory('_pdf_out') as oebdir:
OEBOutput(None).convert(oeb_book, oebdir, self.input_plugin, self.opts, self.log) OEBOutput(None).convert(oeb_book, oebdir, self.input_plugin, self.opts, self.log)
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0] opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
writer = PDFWriter(self.opts, self.log) self.write(PDFWriter, [s.path for s in opf.spine])
def write(self, Writer, items):
writer = Writer(self.opts, self.log)
close = False close = False
if not hasattr(output_path, 'write'): if not hasattr(self.output_path, 'write'):
close = True close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '': if not os.path.exists(os.path.dirname(self.output_path)) and os.path.dirname(self.output_path) != '':
os.makedirs(os.path.dirname(output_path)) os.makedirs(os.path.dirname(self.output_path))
out_stream = open(output_path, 'wb') out_stream = open(self.output_path, 'wb')
else: else:
out_stream = output_path out_stream = self.output_path
out_stream.seek(0) out_stream.seek(0)
out_stream.truncate() out_stream.truncate()
writer.dump(opf, out_stream, PDFMetadata(oeb_book.metadata)) writer.dump(items, out_stream, PDFMetadata(self.metadata))
if close: if close:
out_stream.close() out_stream.close()

View File

@ -64,12 +64,11 @@ class PDFWriter(QObject):
self.opts = opts self.opts = opts
def dump(self, opfpath, out_stream, pdf_metadata): def dump(self, items, out_stream, pdf_metadata):
self.metadata = pdf_metadata self.metadata = pdf_metadata
self._delete_tmpdir() self._delete_tmpdir()
opf = OPF(opfpath, os.path.dirname(opfpath)) self.render_queue = items
self.render_queue = [i.path for i in opf.spine]
self.combine_queue = [] self.combine_queue = []
self.out_stream = out_stream self.out_stream = out_stream
@ -87,7 +86,7 @@ class PDFWriter(QObject):
item = str(self.render_queue.pop(0)) item = str(self.render_queue.pop(0))
self.combine_queue.append(os.path.join(self.tmp_path, '%i.pdf' % (len(self.combine_queue) + 1))) self.combine_queue.append(os.path.join(self.tmp_path, '%i.pdf' % (len(self.combine_queue) + 1)))
self.logger.info('Processing %s...' % item) self.logger.debug('Processing %s...' % item)
self.view.load(QUrl(item)) self.view.load(QUrl(item))
@ -120,7 +119,7 @@ class PDFWriter(QObject):
self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts') self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts')
def _write(self): def _write(self):
self.logger.info('Combining individual PDF parts...') self.logger.debug('Combining individual PDF parts...')
try: try:
outPDF = PdfFileWriter(title=self.metadata.title, author=self.metadata.author) outPDF = PdfFileWriter(title=self.metadata.title, author=self.metadata.author)
@ -134,8 +133,16 @@ class PDFWriter(QObject):
self.loop.exit(0) self.loop.exit(0)
class ImagePDFWriter(object): class ImagePDFWriter(PDFWriter):
def _render_next(self):
item = str(self.render_queue.pop(0))
self.combine_queue.append(os.path.join(self.tmp_path, '%i.pdf' % (len(self.combine_queue) + 1)))
self.logger.debug('Processing %s...' % item)
html = '<html><body><img src="%s" style="display: block; margin-left: auto; margin-right: auto; padding: 0px;" /></body></html>' % item
self.view.setHtml(html)
def __init__(self, opts, log):
self.opts, self.log = opts, log