mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
PDFOutput: Custom document size, clean up pageoptions.
This commit is contained in:
parent
1d19b2611e
commit
41dd6ddf9f
@ -18,8 +18,8 @@ from calibre.customize.conversion import OutputFormatPlugin, \
|
||||
from calibre.ebooks.oeb.output import OEBOutput
|
||||
from calibre.ptempfile import TemporaryDirectory
|
||||
from calibre.ebooks.pdf.writer import PDFWriter, PDFMetadata
|
||||
from calibre.ebooks.pdf.pageoptions import UNITS, unit, PAPER_SIZES, \
|
||||
paper_size, ORIENTATIONS, orientation, PageOptions
|
||||
from calibre.ebooks.pdf.pageoptions import UNITS, PAPER_SIZES, \
|
||||
ORIENTATIONS
|
||||
|
||||
class PDFOutput(OutputFormatPlugin):
|
||||
|
||||
@ -29,53 +29,43 @@ class PDFOutput(OutputFormatPlugin):
|
||||
|
||||
options = set([
|
||||
OptionRecommendation(name='margin_top', recommended_value='1',
|
||||
level=OptionRecommendation.LOW, long_switch='margin_top',
|
||||
level=OptionRecommendation.LOW,
|
||||
help=_('The top margin around the document.')),
|
||||
OptionRecommendation(name='margin_bottom', recommended_value='1',
|
||||
level=OptionRecommendation.LOW, long_switch='margin_bottom',
|
||||
level=OptionRecommendation.LOW,
|
||||
help=_('The bottom margin around the document.')),
|
||||
OptionRecommendation(name='margin_left', recommended_value='1',
|
||||
level=OptionRecommendation.LOW, long_switch='margin_left',
|
||||
level=OptionRecommendation.LOW,
|
||||
help=_('The left margin around the document.')),
|
||||
OptionRecommendation(name='margin_right', recommended_value='1',
|
||||
level=OptionRecommendation.LOW, long_switch='margin_right',
|
||||
level=OptionRecommendation.LOW,
|
||||
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(),
|
||||
level=OptionRecommendation.LOW, short_switch='u', 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(),
|
||||
level=OptionRecommendation.LOW, choices=PAPER_SIZES.keys(),
|
||||
help=_('The size of the paper. Default is letter. Choices '
|
||||
'are %s' % PAPER_SIZES.keys())),
|
||||
OptionRecommendation(name='custom_size', recommended_value=None,
|
||||
help=_('Custom size of the document. Use the form widthxheight '
|
||||
'EG. `123x321` to specify the width and height. '
|
||||
'This overrides any specified paper-size.')),
|
||||
OptionRecommendation(name='orientation', recommended_value='portrait',
|
||||
level=OptionRecommendation.LOW,
|
||||
long_switch='orientation', choices=ORIENTATIONS.keys(),
|
||||
level=OptionRecommendation.LOW, 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):
|
||||
popts = PageOptions()
|
||||
|
||||
popts.set_margin_top(opts.margin_top)
|
||||
popts.set_margin_bottom(opts.margin_bottom)
|
||||
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)
|
||||
|
||||
with TemporaryDirectory('_pdf_out') 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, opts.output_profile)
|
||||
writer = PDFWriter(log, opts)
|
||||
|
||||
close = False
|
||||
if not hasattr(output_path, 'write'):
|
||||
|
@ -63,36 +63,8 @@ ORIENTATIONS = {
|
||||
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):
|
||||
def size(size):
|
||||
try:
|
||||
self.margin_top = int(size)
|
||||
return 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
|
||||
return 1
|
||||
|
@ -13,13 +13,14 @@ import os, shutil, sys
|
||||
|
||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||
from calibre.customize.profiles import OutputProfile
|
||||
from calibre.ebooks.pdf.pageoptions import PageOptions
|
||||
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, QApplication, QPrinter, \
|
||||
QMetaObject, QSizeF, Qt
|
||||
from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, \
|
||||
QApplication, QPrinter, QMetaObject, QSizeF, Qt
|
||||
from PyQt4.QtWebKit import QWebView
|
||||
|
||||
from pyPdf import PdfFileWriter, PdfFileReader
|
||||
@ -37,7 +38,7 @@ class PDFMetadata(object):
|
||||
|
||||
|
||||
class PDFWriter(QObject):
|
||||
def __init__(self, log, popts=PageOptions(), profile=OutputProfile(None)):
|
||||
def __init__(self, log, opts):
|
||||
if QApplication.instance() is None:
|
||||
QApplication([])
|
||||
QObject.__init__(self)
|
||||
@ -49,9 +50,20 @@ class PDFWriter(QObject):
|
||||
self.connect(self.view, SIGNAL('loadFinished(bool)'), self._render_html)
|
||||
self.render_queue = []
|
||||
self.combine_queue = []
|
||||
self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts')
|
||||
self.popts = popts
|
||||
self.profile = profile
|
||||
self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts')
|
||||
|
||||
self.custom_size = None
|
||||
if opts.custom_size != None:
|
||||
width, sep, height = opts.custom_size.partition('x')
|
||||
if height != '':
|
||||
try:
|
||||
width = int(width)
|
||||
height = int(height)
|
||||
self.custom_size = (width, height)
|
||||
except:
|
||||
self.custom_size = None
|
||||
|
||||
self.opts = opts
|
||||
|
||||
def dump(self, opfpath, out_stream, pdf_metadata):
|
||||
self.metadata = pdf_metadata
|
||||
@ -88,14 +100,16 @@ class PDFWriter(QObject):
|
||||
|
||||
printer = QPrinter(QPrinter.HighResolution)
|
||||
|
||||
if self.profile.short_name == 'default':
|
||||
printer.setPaperSize(self.popts.paper_size)
|
||||
if self.opts.output_profile.short_name == 'default':
|
||||
if self.custom_size == None:
|
||||
printer.setPaperSize(paper_size(self.opts.paper_size))
|
||||
else:
|
||||
#printer.setResolution(self.profile.dpi)
|
||||
printer.setPaperSize(QSizeF(self.profile.width / self.profile.dpi, self.profile.height / self.profile.dpi), QPrinter.Inch)
|
||||
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(self.popts.margin_left, self.popts.margin_top, self.popts.margin_right, self.popts.margin_bottom, self.popts.unit)
|
||||
printer.setOrientation(self.popts.orientation)
|
||||
printer.setPageMargins(size(self.opts.margin_left), size(self.opts.margin_top), size(self.opts.margin_right), size(self.opts.margin_bottom), unit(self.opts.unit))
|
||||
printer.setOrientation(orientation(self.opts.orientation))
|
||||
printer.setOutputFormat(QPrinter.PdfFormat)
|
||||
printer.setOutputFileName(item_path)
|
||||
self.view.print_(printer)
|
||||
@ -104,7 +118,7 @@ class PDFWriter(QObject):
|
||||
def _delete_tmpdir(self):
|
||||
if os.path.exists(self.tmp_path):
|
||||
shutil.rmtree(self.tmp_path, True)
|
||||
self.tmp_path = PersistentTemporaryDirectory('_pdf_out_parts')
|
||||
self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts')
|
||||
|
||||
def _write(self):
|
||||
self.logger.info('Combining individual PDF parts...')
|
||||
|
Loading…
x
Reference in New Issue
Block a user