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.ebooks.oeb.output import OEBOutput
|
||||||
from calibre.ptempfile import TemporaryDirectory
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
from calibre.ebooks.pdf.writer import PDFWriter, PDFMetadata
|
from calibre.ebooks.pdf.writer import PDFWriter, PDFMetadata
|
||||||
from calibre.ebooks.pdf.pageoptions import UNITS, unit, PAPER_SIZES, \
|
from calibre.ebooks.pdf.pageoptions import UNITS, PAPER_SIZES, \
|
||||||
paper_size, ORIENTATIONS, orientation, PageOptions
|
ORIENTATIONS
|
||||||
|
|
||||||
class PDFOutput(OutputFormatPlugin):
|
class PDFOutput(OutputFormatPlugin):
|
||||||
|
|
||||||
@ -29,53 +29,43 @@ class PDFOutput(OutputFormatPlugin):
|
|||||||
|
|
||||||
options = set([
|
options = set([
|
||||||
OptionRecommendation(name='margin_top', recommended_value='1',
|
OptionRecommendation(name='margin_top', recommended_value='1',
|
||||||
level=OptionRecommendation.LOW, long_switch='margin_top',
|
level=OptionRecommendation.LOW,
|
||||||
help=_('The top margin around the document.')),
|
help=_('The top margin around the document.')),
|
||||||
OptionRecommendation(name='margin_bottom', recommended_value='1',
|
OptionRecommendation(name='margin_bottom', recommended_value='1',
|
||||||
level=OptionRecommendation.LOW, long_switch='margin_bottom',
|
level=OptionRecommendation.LOW,
|
||||||
help=_('The bottom margin around the document.')),
|
help=_('The bottom margin around the document.')),
|
||||||
OptionRecommendation(name='margin_left', recommended_value='1',
|
OptionRecommendation(name='margin_left', recommended_value='1',
|
||||||
level=OptionRecommendation.LOW, long_switch='margin_left',
|
level=OptionRecommendation.LOW,
|
||||||
help=_('The left margin around the document.')),
|
help=_('The left margin around the document.')),
|
||||||
OptionRecommendation(name='margin_right', recommended_value='1',
|
OptionRecommendation(name='margin_right', recommended_value='1',
|
||||||
level=OptionRecommendation.LOW, long_switch='margin_right',
|
level=OptionRecommendation.LOW,
|
||||||
help=_('The right margin around the document.')),
|
help=_('The right margin around the document.')),
|
||||||
|
|
||||||
OptionRecommendation(name='unit', recommended_value='inch',
|
OptionRecommendation(name='unit', recommended_value='inch',
|
||||||
level=OptionRecommendation.LOW, short_switch='u',
|
level=OptionRecommendation.LOW, short_switch='u', choices=UNITS.keys(),
|
||||||
long_switch='unit', choices=UNITS.keys(),
|
|
||||||
help=_('The unit of measure. Default is inch. Choices '
|
help=_('The unit of measure. Default is inch. Choices '
|
||||||
'are %s' % UNITS.keys())),
|
'are %s' % UNITS.keys())),
|
||||||
OptionRecommendation(name='paper_size', recommended_value='letter',
|
OptionRecommendation(name='paper_size', recommended_value='letter',
|
||||||
level=OptionRecommendation.LOW,
|
level=OptionRecommendation.LOW, choices=PAPER_SIZES.keys(),
|
||||||
long_switch='paper_size', choices=PAPER_SIZES.keys(),
|
|
||||||
help=_('The size of the paper. Default is letter. Choices '
|
help=_('The size of the paper. Default is letter. Choices '
|
||||||
'are %s' % PAPER_SIZES.keys())),
|
'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',
|
OptionRecommendation(name='orientation', recommended_value='portrait',
|
||||||
level=OptionRecommendation.LOW,
|
level=OptionRecommendation.LOW, choices=ORIENTATIONS.keys(),
|
||||||
long_switch='orientation', choices=ORIENTATIONS.keys(),
|
|
||||||
help=_('The orientation of the page. Default is portrait. Choices '
|
help=_('The orientation of the page. Default is portrait. Choices '
|
||||||
'are %s' % ORIENTATIONS.keys())),
|
'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):
|
||||||
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:
|
with TemporaryDirectory('_pdf_out') as oebdir:
|
||||||
OEBOutput(None).convert(oeb_book, oebdir, input_plugin, opts, log)
|
OEBOutput(None).convert(oeb_book, oebdir, input_plugin, opts, log)
|
||||||
|
|
||||||
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
|
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
|
||||||
|
|
||||||
writer = PDFWriter(log, popts, opts.output_profile)
|
writer = PDFWriter(log, opts)
|
||||||
|
|
||||||
close = False
|
close = False
|
||||||
if not hasattr(output_path, 'write'):
|
if not hasattr(output_path, 'write'):
|
||||||
|
@ -63,36 +63,8 @@ ORIENTATIONS = {
|
|||||||
def orientation(orientation):
|
def orientation(orientation):
|
||||||
return ORIENTATIONS.get(orientation, QPrinter.Portrait)
|
return ORIENTATIONS.get(orientation, QPrinter.Portrait)
|
||||||
|
|
||||||
|
def size(size):
|
||||||
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:
|
try:
|
||||||
self.margin_top = int(size)
|
return int(size)
|
||||||
except:
|
except:
|
||||||
self.margin_top = 1
|
return 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
|
|
||||||
|
@ -13,13 +13,14 @@ import os, shutil, sys
|
|||||||
|
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
from calibre.customize.profiles import OutputProfile
|
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 import authors_to_string
|
||||||
from calibre.ebooks.metadata.opf2 import OPF
|
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, \
|
||||||
QMetaObject, QSizeF, Qt
|
QApplication, QPrinter, QMetaObject, QSizeF, Qt
|
||||||
from PyQt4.QtWebKit import QWebView
|
from PyQt4.QtWebKit import QWebView
|
||||||
|
|
||||||
from pyPdf import PdfFileWriter, PdfFileReader
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
@ -37,7 +38,7 @@ class PDFMetadata(object):
|
|||||||
|
|
||||||
|
|
||||||
class PDFWriter(QObject):
|
class PDFWriter(QObject):
|
||||||
def __init__(self, log, popts=PageOptions(), profile=OutputProfile(None)):
|
def __init__(self, log, opts):
|
||||||
if QApplication.instance() is None:
|
if QApplication.instance() is None:
|
||||||
QApplication([])
|
QApplication([])
|
||||||
QObject.__init__(self)
|
QObject.__init__(self)
|
||||||
@ -49,9 +50,20 @@ class PDFWriter(QObject):
|
|||||||
self.connect(self.view, SIGNAL('loadFinished(bool)'), self._render_html)
|
self.connect(self.view, SIGNAL('loadFinished(bool)'), self._render_html)
|
||||||
self.render_queue = []
|
self.render_queue = []
|
||||||
self.combine_queue = []
|
self.combine_queue = []
|
||||||
self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts')
|
self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts')
|
||||||
self.popts = popts
|
|
||||||
self.profile = profile
|
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):
|
def dump(self, opfpath, out_stream, pdf_metadata):
|
||||||
self.metadata = pdf_metadata
|
self.metadata = pdf_metadata
|
||||||
@ -88,14 +100,16 @@ class PDFWriter(QObject):
|
|||||||
|
|
||||||
printer = QPrinter(QPrinter.HighResolution)
|
printer = QPrinter(QPrinter.HighResolution)
|
||||||
|
|
||||||
if self.profile.short_name == 'default':
|
if self.opts.output_profile.short_name == 'default':
|
||||||
printer.setPaperSize(self.popts.paper_size)
|
if self.custom_size == None:
|
||||||
|
printer.setPaperSize(paper_size(self.opts.paper_size))
|
||||||
else:
|
else:
|
||||||
#printer.setResolution(self.profile.dpi)
|
printer.setPaperSize(QSizeF(self.custom_size[0], self.custom_size[1]), unit(self.opts.unit))
|
||||||
printer.setPaperSize(QSizeF(self.profile.width / self.profile.dpi, self.profile.height / self.profile.dpi), QPrinter.Inch)
|
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.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(self.popts.orientation)
|
printer.setOrientation(orientation(self.opts.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)
|
||||||
@ -104,7 +118,7 @@ class PDFWriter(QObject):
|
|||||||
def _delete_tmpdir(self):
|
def _delete_tmpdir(self):
|
||||||
if os.path.exists(self.tmp_path):
|
if os.path.exists(self.tmp_path):
|
||||||
shutil.rmtree(self.tmp_path, True)
|
shutil.rmtree(self.tmp_path, True)
|
||||||
self.tmp_path = PersistentTemporaryDirectory('_pdf_out_parts')
|
self.tmp_path = PersistentTemporaryDirectory('_pdf_output_parts')
|
||||||
|
|
||||||
def _write(self):
|
def _write(self):
|
||||||
self.logger.info('Combining individual PDF parts...')
|
self.logger.info('Combining individual PDF parts...')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user