mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
IGN:...
This commit is contained in:
commit
a22a758caf
@ -125,6 +125,8 @@ class CYBOOKG3(USBMS):
|
||||
# Delete the ebook auxiliary file
|
||||
if os.path.exists(filepath + '.mbp'):
|
||||
os.unlink(filepath + '.mbp')
|
||||
if os.path.exists(filepath + '.dat'):
|
||||
os.unlink(filepath + '.dat')
|
||||
|
||||
# Delete the thumbnails file auto generated for the ebook
|
||||
if os.path.exists(filepath + '_6090.t2b'):
|
||||
|
@ -48,6 +48,8 @@ class HTMLPreProcessor(object):
|
||||
|
||||
# Fix pdftohtml markup
|
||||
PDFTOHTML = [
|
||||
# Remove page links
|
||||
(re.compile(r'<a name=\d+></a>', re.IGNORECASE), lambda match: ''),
|
||||
# Remove <hr> tags
|
||||
(re.compile(r'<hr.*?>', re.IGNORECASE), lambda match: '<br />'),
|
||||
# Remove page numbers
|
||||
@ -69,6 +71,12 @@ class HTMLPreProcessor(object):
|
||||
|
||||
# Have paragraphs show better
|
||||
(re.compile(r'<br.*?>'), lambda match : '<p>'),
|
||||
|
||||
# Un wrap lines
|
||||
(re.compile(r'(?<=\w)\s*</i>\s*<p.*?>\s*<i>\s*(?=\w)'), lambda match: ' '),
|
||||
(re.compile(r'(?<=\w)\s*<p.*?>\s*(?=\w)', re.UNICODE), lambda match: ' '),
|
||||
# Clean up spaces
|
||||
(re.compile(u'(?<=\.|,|:|;|\?|!|”|"|\')[\s^ ]*(?=<)'), lambda match: ' '),
|
||||
]
|
||||
|
||||
# Fix Book Designer markup
|
||||
|
@ -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)
|
||||
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):
|
||||
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
|
||||
def size(size):
|
||||
try:
|
||||
return int(size)
|
||||
except:
|
||||
return 1
|
||||
|
@ -12,13 +12,15 @@ Write content to PDF.
|
||||
import os, shutil, sys
|
||||
|
||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||
from calibre.ebooks.pdf.pageoptions import PageOptions
|
||||
from calibre.customize.profiles import OutputProfile
|
||||
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, Qt
|
||||
from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, \
|
||||
QApplication, QPrinter, QMetaObject, QSizeF, Qt
|
||||
from PyQt4.QtWebKit import QWebView
|
||||
|
||||
from pyPdf import PdfFileWriter, PdfFileReader
|
||||
@ -36,7 +38,7 @@ class PDFMetadata(object):
|
||||
|
||||
|
||||
class PDFWriter(QObject):
|
||||
def __init__(self, log, popts=PageOptions()):
|
||||
def __init__(self, log, opts):
|
||||
if QApplication.instance() is None:
|
||||
QApplication([])
|
||||
QObject.__init__(self)
|
||||
@ -48,8 +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.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
|
||||
@ -85,9 +99,17 @@ 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)
|
||||
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)
|
||||
|
||||
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(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)
|
||||
@ -96,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