PDF output complete

This commit is contained in:
John Schember 2009-04-02 06:38:55 -04:00
parent dc3a6a1f61
commit 1d669699ba
2 changed files with 34 additions and 23 deletions

View File

@ -1,4 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import with_statement
__license__ = 'GPL 3' __license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>' __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
@ -9,10 +11,12 @@ Convert OEB ebook format to PDF.
#unit, papersize, orientation, custom_size, profile #unit, papersize, orientation, custom_size, profile
import os import os, glob
from calibre.customize.conversion import OutputFormatPlugin, \ from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation OptionRecommendation
from calibre.ebooks.oeb.output import OEBOutput
from calibre.ptempfile import TemporaryDirectory
from calibre.ebooks.pdf.writer import PDFWriter from calibre.ebooks.pdf.writer import PDFWriter
from calibre.ebooks.pdf.pageoptions import UNITS, unit, PAPER_SIZES, \ from calibre.ebooks.pdf.pageoptions import UNITS, unit, PAPER_SIZES, \
paper_size, ORIENTATIONS, orientation, PageOptions paper_size, ORIENTATIONS, orientation, PageOptions
@ -65,21 +69,26 @@ class PDFOutput(OutputFormatPlugin):
popts.unit = unit(opts.unit) popts.unit = unit(opts.unit)
popts.paper_size = paper_size(opts.paper_size) popts.paper_size = paper_size(opts.paper_size)
popts.orientation = orientation(opts.orientation) popts.orientation = orientation(opts.orientation)
writer = PDFWriter(log, popts) with TemporaryDirectory('_any2pdf') as oebdir:
OEBOutput(None).convert(oeb_book, oebdir, input_plugin, opts, log)
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
writer = PDFWriter(log, popts)
close = False close = False
if not hasattr(output_path, 'write'): if not hasattr(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(output_path)) and os.path.dirname(output_path) != '':
os.makedirs(os.path.dirname(output_path)) os.makedirs(os.path.dirname(output_path))
out_stream = open(output_path, 'wb') out_stream = open(output_path, 'wb')
else: else:
out_stream = output_path out_stream = output_path
out_stream.seek(0) out_stream.seek(0)
out_stream.truncate() out_stream.truncate()
writer.dump(oeb_book.spine, out_stream) writer.dump(opf, out_stream)
if close: if close:
out_stream.close() out_stream.close()

View File

@ -13,6 +13,7 @@ import os, shutil, sys
from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.ebooks.pdf.pageoptions import PageOptions from calibre.ebooks.pdf.pageoptions import PageOptions
from calibre.ebooks.metadata.opf2 import OPF
from PyQt4 import QtCore from PyQt4 import QtCore
from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, QApplication, QPrinter, \ from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, QApplication, QPrinter, \
@ -37,10 +38,11 @@ class PDFWriter(QObject):
self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts') self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts')
self.popts = popts self.popts = popts
def dump(self, spine, out_stream): def dump(self, opfpath, out_stream):
self._delete_tmpdir() self._delete_tmpdir()
self.render_queue = spine[:] opf = OPF(opfpath, os.path.dirname(opfpath))
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
@ -56,7 +58,7 @@ class PDFWriter(QObject):
def _render_next(self): def _render_next(self):
item = str(self.render_queue.pop(0)) item = str(self.render_queue.pop(0))
self.combine_queue.append(os.path.join(self.tmp_path, '%s_%i.pdf' % (os.path.basename(item), len(self.combine_queue)))) 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.info('Processing %s...' % item)
@ -64,9 +66,9 @@ class PDFWriter(QObject):
def _render_html(self, ok): def _render_html(self, ok):
if ok: if ok:
item_path = os.path.join(self.tmp_path, '%s_%i.pdf' % (os.path.basename(str(self.view.url().toLocalFile())), len(self.combine_queue) - 1)) item_path = os.path.join(self.tmp_path, '%i.pdf' % len(self.combine_queue))
self.logger.debug('\tRendering item as %s' % item_path) self.logger.debug('\tRendering item %s as %i' % (os.path.basename(str(self.view.url().toLocalFile())), len(self.combine_queue)))
printer = QPrinter(QPrinter.HighResolution) printer = QPrinter(QPrinter.HighResolution)
printer.setPageMargins(self.popts.margin_left, self.popts.margin_top, self.popts.margin_right, self.popts.margin_bottom, self.popts.unit) printer.setPageMargins(self.popts.margin_left, self.popts.margin_top, self.popts.margin_right, self.popts.margin_bottom, self.popts.unit)