image to pdf output complete.

This commit is contained in:
John Schember 2009-04-29 19:19:24 -04:00
parent 73d203fa86
commit 3a2da56ccc
2 changed files with 29 additions and 54 deletions

View File

@ -16,6 +16,7 @@ import os, glob
from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation
from calibre.ebooks.oeb.output import OEBOutput
from calibre.ebooks.metadata.opf2 import OPF
from calibre.ptempfile import TemporaryDirectory
from calibre.ebooks.pdf.writer import PDFWriter, ImagePDFWriter, PDFMetadata
from calibre.ebooks.pdf.pageoptions import UNITS, PAPER_SIZES, \
@ -58,14 +59,14 @@ class PDFOutput(OutputFormatPlugin):
self.convert_text(oeb_book)
def convert_images(self, images):
# process images to document size
self.write(ImagePDFWriter, images)
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]
opfpath = glob.glob(os.path.join(oebdir, '*.opf'))[0]
opf = OPF(opfpath, os.path.dirname(opfpath))
self.write(PDFWriter, [s.path for s in opf.spine])

View File

@ -15,7 +15,6 @@ from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.ebooks.pdf.pageoptions import unit, paper_size, \
orientation, size
from calibre.ebooks.metadata import authors_to_string
from calibre.ebooks.metadata.opf2 import OPF
from PyQt4 import QtCore
from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, \
@ -64,6 +63,8 @@ class PDFWriter(QObject):
self.opts = opts
self.size = self._size()
def dump(self, items, out_stream, pdf_metadata):
self.metadata = pdf_metadata
self._delete_tmpdir()
@ -75,6 +76,28 @@ class PDFWriter(QObject):
QMetaObject.invokeMethod(self, "_render_book", Qt.QueuedConnection)
self.loop.exec_()
def _size(self):
'''
The size of a pdf page in cm.
'''
printer = QPrinter(QPrinter.HighResolution)
if self.opts.output_profile.short_name == 'default':
if self.custom_size == None:
printer.setPaperSize(paper_size(self.opts.paper_size))
else:
printer.setPaperSize(QSizeF(self.custom_size[0], self.custom_size[1]), unit(self.opts.unit))
else:
printer.setPaperSize(QSizeF(self.opts.output_profile.width / self.opts.output_profile.dpi, self.opts.output_profile.height / self.opts.output_profile.dpi), QPrinter.Inch)
printer.setPageMargins(0, 0, 0, 0, QPrinter.Point)
printer.setOrientation(orientation(self.opts.orientation))
printer.setOutputFormat(QPrinter.PdfFormat)
size = printer.paperSize(QPrinter.Millimeter)
return size.width() / 10, size.height() / 10
@QtCore.pyqtSignature('_render_book()')
def _render_book(self):
if len(self.render_queue) == 0:
@ -97,15 +120,7 @@ class PDFWriter(QObject):
self.logger.debug('\tRendering item %s as %i' % (os.path.basename(str(self.view.url().toLocalFile())), len(self.combine_queue)))
printer = QPrinter(QPrinter.HighResolution)
if self.opts.output_profile.short_name == 'default':
if self.custom_size == None:
printer.setPaperSize(paper_size(self.opts.paper_size))
else:
printer.setPaperSize(QSizeF(self.custom_size[0], self.custom_size[1]), unit(self.opts.unit))
else:
printer.setPaperSize(QSizeF(self.opts.output_profile.width / self.opts.output_profile.dpi, self.opts.output_profile.height / self.opts.output_profile.dpi), QPrinter.Inch)
printer.setPaperSize(QSizeF(self.size[0] * 10, self.size[1] * 10), QPrinter.Millimeter)
printer.setPageMargins(0, 0, 0, 0, QPrinter.Point)
printer.setOrientation(orientation(self.opts.orientation))
printer.setOutputFormat(QPrinter.PdfFormat)
@ -141,53 +156,12 @@ class ImagePDFWriter(PDFWriter):
self.logger.debug('Processing %s...' % item)
import Image
size = self._size()
#height = 'height: %ipx;' % size[1] if Image.open(item).size[1] > size[1] else ''
#height = 'height: %icm;' % size[1]
#height = 'height: %ipx;' % self.opts.output_profile.comic_screen_size[1]
### height = 'height: 594px;' # self.opts.output_profile.comic_screen_size[1] * .78
#print
#print size[1]
#print Image.open(item).size[1]
#print Image.open(item).size[1] / self.opts.output_profile.dpi
#print height
#height = 'height: %ipx;' % (self.opts.output_profile.comic_screen_size[1] - 160)
#height = 'height: %ipx;' % (self.opts.output_profile.comic_screen_size[1] * .78)
height = 'height: %fcm;' % (size[1] * 1.3)
#print height
height = 'height: %fcm;' % (self.size[1] * 1.3)
html = '<html><body style="margin: 0;"><img src="%s" style="%s display: block; margin-left: auto; margin-right: auto; padding: 0px;" /></body></html>' % (item, height)
self.view.setHtml(html)
def _render_html(self, ok):
if ok:
item_path = os.path.join(self.tmp_path, '%i.pdf' % len(self.combine_queue))
self.logger.debug('\tRendering item %s as %i' % (os.path.basename(str(self.view.url().toLocalFile())), len(self.combine_queue)))
printer = QPrinter(QPrinter.HighResolution)
if self.opts.output_profile.short_name == 'default':
if self.custom_size == None:
printer.setPaperSize(paper_size(self.opts.paper_size))
else:
printer.setPaperSize(QSizeF(self.custom_size[0], self.custom_size[1]), unit(self.opts.unit))
else:
printer.setPaperSize(QSizeF(self.opts.output_profile.comic_screen_size[0] / self.opts.output_profile.dpi, self.opts.output_profile.comic_screen_size[1] / self.opts.output_profile.dpi), QPrinter.Inch)
printer.setPageMargins(0, 0, 0, 0, QPrinter.Point)
printer.setOrientation(orientation(self.opts.orientation))
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(item_path)
self.view.print_(printer)
self._render_book()
def _size(self):
printer = QPrinter(QPrinter.HighResolution)