More robust pdf output

This commit is contained in:
John Schember 2009-04-01 07:39:41 -04:00
parent 7973c41073
commit 596e3f7138
3 changed files with 135 additions and 18 deletions

View File

@ -13,7 +13,9 @@ import os
from calibre.customize.conversion import OutputFormatPlugin, \ from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation OptionRecommendation
from calibre.ebooks.pdf.writer import PDFWriter, PDFMargins from calibre.ebooks.pdf.writer import PDFWriter
from calibre.ebooks.pdf.pageoptions import UNITS, unit, PAPER_SIZES, \
paper_size, ORIENTATIONS, orientation, PageOptions
class PDFOutput(OutputFormatPlugin): class PDFOutput(OutputFormatPlugin):
@ -34,16 +36,37 @@ class PDFOutput(OutputFormatPlugin):
OptionRecommendation(name='margin_right', recommended_value='1', OptionRecommendation(name='margin_right', recommended_value='1',
level=OptionRecommendation.LOW, long_switch='margin_right', level=OptionRecommendation.LOW, long_switch='margin_right',
help=_('The right margin around the document.')), help=_('The right margin around the document.')),
OptionRecommendation(name='unit', recommended_value='inch',
level=OptionRecommendation.LOW, short_switch='u',
long_switch='unit', choices=UNITS.keys(),
help=_('The unit of measure. Default is inch. Choices '
'are %s' % UNITS.keys())),
OptionRecommendation(name='paper_size', recommended_value='letter',
level=OptionRecommendation.LOW,
long_switch='paper_size', choices=PAPER_SIZES.keys(),
help=_('The size of the paper. Default is letter. Choices '
'are %s' % PAPER_SIZES.keys())),
OptionRecommendation(name='orientation', recommended_value='portrait',
level=OptionRecommendation.LOW,
long_switch='orientation', choices=ORIENTATIONS.keys(),
help=_('The orientation of the page. Default is portrait. Choices '
'are %s' % ORIENTATIONS.keys())),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
margins = PDFMargins() popts = PageOptions()
margins.top = opts.margin_top
margins.bottom = opts.margin_bottom popts.set_margin_top(opts.margin_top)
margins.left = opts.margin_left popts.set_margin_bottom(opts.margin_bottom)
margins.right = opts.margin_right popts.set_margin_left(opts.margin_left)
popts.set_margin_right(opts.margin_right)
popts.unit = unit(opts.unit)
popts.paper_size = paper_size(opts.paper_size)
popts.orientation = orientation(opts.orientation)
writer = PDFWriter(log, margins) writer = PDFWriter(log, popts)
close = False close = False
if not hasattr(output_path, 'write'): if not hasattr(output_path, 'write'):

View File

@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
from PyQt4.Qt import QPrinter
UNITS = {
'millimeter' : QPrinter.Millimeter,
'point' : QPrinter.Point,
'inch' : QPrinter.Inch,
'pica' : QPrinter.Pica,
'didot' : QPrinter.Didot,
'cicero' : QPrinter.Cicero,
'devicepixel' : QPrinter.DevicePixel,
}
def unit(unit):
return UNITS.get(unit, QPrinter.Inch)
PAPER_SIZES = {
'a0' : QPrinter.A0, # 841 x 1189 mm
'a1' : QPrinter.A1, # 594 x 841 mm
'a2' : QPrinter.A2, # 420 x 594 mm
'a3' : QPrinter.A3, # 297 x 420 mm
'a4' : QPrinter.A4, # 210 x 297 mm, 8.26 x 11.69 inches
'a5' : QPrinter.A5, # 148 x 210 mm
'a6' : QPrinter.A6, # 105 x 148 mm
'a7' : QPrinter.A7, # 74 x 105 mm
'a8' : QPrinter.A8, # 52 x 74 mm
'a9' : QPrinter.A9, # 37 x 52 mm
'b0' : QPrinter.B0, # 1030 x 1456 mm
'b1' : QPrinter.B1, # 728 x 1030 mm
'b2' : QPrinter.B2, # 515 x 728 mm
'b3' : QPrinter.B3, # 364 x 515 mm
'b4' : QPrinter.B4, # 257 x 364 mm
'b5' : QPrinter.B5, # 182 x 257 mm, 7.17 x 10.13 inches
'b6' : QPrinter.B6, # 128 x 182 mm
'b7' : QPrinter.B7, # 91 x 128 mm
'b8' : QPrinter.B8, # 64 x 91 mm
'b9' : QPrinter.B9, # 45 x 64 mm
'b10' : QPrinter.B10, # 32 x 45 mm
'c5e' : QPrinter.C5E, # 163 x 229 mm
'comm10e' : QPrinter.Comm10E, # 105 x 241 mm, U.S. Common 10 Envelope
'dle' : QPrinter.DLE, # 110 x 220 mm
'executive' : QPrinter.Executive, # 7.5 x 10 inches, 191 x 254 mm
'folio' : QPrinter.Folio, # 210 x 330 mm
'ledger' : QPrinter.Ledger, # 432 x 279 mm
'legal' : QPrinter.Legal, # 8.5 x 14 inches, 216 x 356 mm
'letter' : QPrinter.Letter, # 8.5 x 11 inches, 216 x 279 mm
'tabloid' : QPrinter.Tabloid, # 279 x 432 mm
#'custom' : QPrinter.Custom, # Unknown, or a user defined size.
}
def paper_size(size):
return PAPER_SIZES.get(size, QPrinter.Letter)
ORIENTATIONS = {
'portrait' : QPrinter.Portrait,
'landscape' : QPrinter.Landscape,
}
def orientation(orientation):
return ORIENTATIONS.get(orientation, QPrinter.Portrait)
class PageOptions(object):
margin_top = 1
margin_bottom = 1
margin_left = 1
margin_right = 1
unit = QPrinter.Inch
paper_size = QPrinter.Letter
orientation = QPrinter.Portrait
def set_margin_top(self, size):
try:
self.margin_top = int(size)
except:
self.margin_top = 1
def set_margin_bottom(self, size):
try:
self.margin_bottom = int(size)
except:
self.margin_bottom = 1
def set_margin_left(self, size):
try:
self.margin_left = int(size)
except:
self.margin_left = 1
def set_margin_right(self, size):
try:
self.margin_right = int(size)
except:
self.margin_right = 1

View File

@ -12,23 +12,17 @@ Write content to PDF.
import os, shutil, sys import os, shutil, sys
from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.ebooks.pdf.pageoptions import PageOptions
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, \
QMetaObject, Qt QMetaObject, Qt
from PyQt4.QtWebKit import QWebView from PyQt4.QtWebKit import QWebView
from pyPdf import PdfFileWriter, PdfFileReader from pyPdf import PdfFileWriter, PdfFileReader
class PDFMargins:
def __init__(self, margin=1):
self.top = margin
self.bottom = margin
self.left = margin
self.right = margin
class PDFWriter(QObject): class PDFWriter(QObject):
def __init__(self, log, margins=PDFMargins()): def __init__(self, log, popts=PageOptions()):
if QApplication.instance() is None: if QApplication.instance() is None:
QApplication([]) QApplication([])
QObject.__init__(self) QObject.__init__(self)
@ -41,7 +35,7 @@ class PDFWriter(QObject):
self.render_queue = [] self.render_queue = []
self.combine_queue = [] self.combine_queue = []
self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts') self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts')
self.margins = margins self.popts = popts
def dump(self, spine, out_stream): def dump(self, spine, out_stream):
self._delete_tmpdir() self._delete_tmpdir()
@ -75,7 +69,9 @@ class PDFWriter(QObject):
self.logger.debug('\tRendering item as %s' % item_path) self.logger.debug('\tRendering item as %s' % item_path)
printer = QPrinter(QPrinter.HighResolution) printer = QPrinter(QPrinter.HighResolution)
printer.setPageMargins(self.margins.left, self.margins.top, self.margins.right, self.margins.bottom, QPrinter.Inch) printer.setPageMargins(self.popts.margin_left, self.popts.margin_top, self.popts.margin_right, self.popts.margin_bottom, self.popts.unit)
printer.setPaperSize(self.popts.paper_size)
printer.setOrientation(self.popts.orientation)
printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(item_path) printer.setOutputFileName(item_path)
self.view.print_(printer) self.view.print_(printer)