mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
beginnings of working comic to pdf output.
This commit is contained in:
parent
ed13e58801
commit
ee777c539a
@ -17,7 +17,7 @@ from calibre.customize.conversion import OutputFormatPlugin, \
|
||||
OptionRecommendation
|
||||
from calibre.ebooks.oeb.output import OEBOutput
|
||||
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, \
|
||||
ORIENTATIONS
|
||||
|
||||
@ -49,36 +49,42 @@ class PDFOutput(OutputFormatPlugin):
|
||||
|
||||
def convert(self, oeb_book, output_path, 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:
|
||||
self.convert_images(input_plugin.get_images(), output_path)
|
||||
self.convert_images(input_plugin.get_images())
|
||||
else:
|
||||
self.convert_text(oeb_book, output_path)
|
||||
self.convert_text(oeb_book)
|
||||
|
||||
def convert_images(self, images, output_path):
|
||||
raise NotImplementedError()
|
||||
def convert_images(self, images):
|
||||
# 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:
|
||||
OEBOutput(None).convert(oeb_book, oebdir, self.input_plugin, self.opts, self.log)
|
||||
|
||||
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])
|
||||
|
||||
close = False
|
||||
if not hasattr(output_path, 'write'):
|
||||
close = True
|
||||
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
|
||||
os.makedirs(os.path.dirname(output_path))
|
||||
out_stream = open(output_path, 'wb')
|
||||
else:
|
||||
out_stream = output_path
|
||||
def write(self, Writer, items):
|
||||
writer = Writer(self.opts, self.log)
|
||||
|
||||
out_stream.seek(0)
|
||||
out_stream.truncate()
|
||||
writer.dump(opf, out_stream, PDFMetadata(oeb_book.metadata))
|
||||
close = False
|
||||
if not hasattr(self.output_path, 'write'):
|
||||
close = True
|
||||
if not os.path.exists(os.path.dirname(self.output_path)) and os.path.dirname(self.output_path) != '':
|
||||
os.makedirs(os.path.dirname(self.output_path))
|
||||
out_stream = open(self.output_path, 'wb')
|
||||
else:
|
||||
out_stream = self.output_path
|
||||
|
||||
if close:
|
||||
out_stream.close()
|
||||
out_stream.seek(0)
|
||||
out_stream.truncate()
|
||||
writer.dump(items, out_stream, PDFMetadata(self.metadata))
|
||||
|
||||
if close:
|
||||
out_stream.close()
|
||||
|
||||
|
@ -64,12 +64,11 @@ class PDFWriter(QObject):
|
||||
|
||||
self.opts = opts
|
||||
|
||||
def dump(self, opfpath, out_stream, pdf_metadata):
|
||||
def dump(self, items, out_stream, pdf_metadata):
|
||||
self.metadata = pdf_metadata
|
||||
self._delete_tmpdir()
|
||||
|
||||
opf = OPF(opfpath, os.path.dirname(opfpath))
|
||||
self.render_queue = [i.path for i in opf.spine]
|
||||
self.render_queue = items
|
||||
self.combine_queue = []
|
||||
self.out_stream = out_stream
|
||||
|
||||
@ -87,7 +86,7 @@ class PDFWriter(QObject):
|
||||
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.info('Processing %s...' % item)
|
||||
self.logger.debug('Processing %s...' % item)
|
||||
|
||||
self.view.load(QUrl(item))
|
||||
|
||||
@ -120,7 +119,7 @@ class PDFWriter(QObject):
|
||||
self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts')
|
||||
|
||||
def _write(self):
|
||||
self.logger.info('Combining individual PDF parts...')
|
||||
self.logger.debug('Combining individual PDF parts...')
|
||||
|
||||
try:
|
||||
outPDF = PdfFileWriter(title=self.metadata.title, author=self.metadata.author)
|
||||
@ -134,8 +133,16 @@ class PDFWriter(QObject):
|
||||
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user