diff --git a/src/calibre/ebooks/conversion/plugins/pdf_output.py b/src/calibre/ebooks/conversion/plugins/pdf_output.py index 2a8f0bc603..e835adbe21 100644 --- a/src/calibre/ebooks/conversion/plugins/pdf_output.py +++ b/src/calibre/ebooks/conversion/plugins/pdf_output.py @@ -243,13 +243,10 @@ class PDFOutput(OutputFormatPlugin): self.oeb.container.write(path, nraw) def convert_text(self, oeb_book): - from calibre.ebooks.metadata.opf2 import OPF - from calibre.ebooks.pdf.render.from_html import PDFWriter - - self.log.debug('Serializing oeb input to disk for processing...') + from calibre.ebooks.pdf.html_writer import convert self.get_cover_data() - self.process_fonts() + if self.opts.pdf_use_document_margins and self.stored_page_margins: import json for href, margins in iteritems(self.stored_page_margins): @@ -263,39 +260,5 @@ class PDFOutput(OutputFormatPlugin): from calibre.customize.ui import plugin_for_output_format oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb_book, oeb_dir, self.input_plugin, self.opts, self.log) - opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0] - opf = OPF(opfpath, os.path.dirname(opfpath)) - - self.write(PDFWriter, [s.path for s in opf.spine], getattr(opf, - 'toc', None)) - - def write(self, Writer, items, toc): - writer = Writer(self.opts, self.log, cover_data=self.cover_data, - toc=toc) - writer.report_progress = self.report_progress - - close = False - if not hasattr(self.output_path, 'write'): - close = True - if not os.path.exists(os.path.dirname(self.output_path)) and os.path.dirname(self.output_path) != '': - os.makedirs(os.path.dirname(self.output_path)) - out_stream = open(self.output_path, 'wb') - else: - out_stream = self.output_path - - out_stream.seek(0) - out_stream.truncate() - self.log.debug('Rendering pages to PDF...') - import time - st = time.time() - if False: - import cProfile - cProfile.runctx('writer.dump(items, out_stream, PDFMetadata(self.metadata))', - globals(), locals(), '/tmp/profile') - else: - writer.dump(items, out_stream, PDFMetadata(self.metadata)) - self.log('Rendered PDF in %g seconds:'%(time.time()-st)) - - if close: - out_stream.close() + convert(opfpath, self.log, self.opts) diff --git a/src/calibre/ebooks/pdf/html_writer.py b/src/calibre/ebooks/pdf/html_writer.py new file mode 100644 index 0000000000..e89b37d43b --- /dev/null +++ b/src/calibre/ebooks/pdf/html_writer.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2019, Kovid Goyal + +from __future__ import absolute_import, division, print_function, unicode_literals + +import os + +from PyQt5.QtWebEngineWidgets import QWebEnginePage + +from calibre.ebooks.oeb.polish.container import Container as ContainerBase +from calibre.ebooks.oeb.polish.split import merge_html +from calibre.gui2.webengine import secure_webengine + + +class Container(ContainerBase): + + tweak_mode = True + is_dir = True + + def __init__(self, opf_path, log, root_dir=None): + ContainerBase.__init__(self, root_dir or os.path.dirname(opf_path), opf_path, log) + + +class Renderer(QWebEnginePage): + + def __init__(self, opts): + QWebEnginePage.__init__(self) + secure_webengine(self) + s = self.settings() + s.setAttribute(s.JavascriptEnabled, True) + s.setFontSize(s.DefaultFontSize, opts.pdf_default_font_size) + s.setFontSize(s.DefaultFixedFontSize, opts.pdf_mono_font_size) + s.setFontSize(s.MinimumLogicalFontSize, 8) + s.setFontSize(s.MinimumFontSize, 8) + std = {'serif':opts.pdf_serif_family, 'sans':opts.pdf_sans_family, + 'mono':opts.pdf_mono_family}.get(opts.pdf_standard_font, + opts.pdf_serif_family) + if std: + s.setFontFamily(s.StandardFont, std) + if opts.pdf_serif_family: + s.setFontFamily(s.SerifFont, opts.pdf_serif_family) + if opts.pdf_sans_family: + s.setFontFamily(s.SansSerifFont, opts.pdf_sans_family) + if opts.pdf_mono_family: + s.setFontFamily(s.FixedFont, opts.pdf_mono_family) + + +def convert(opf_path, log, opts): + container = Container(opf_path, log) + spine_names = [name for name in container.spine_names] + master = spine_names[0] + if len(spine_names) > 1: + merge_html(container, spine_names, master) + + container.commit() + index_file = container.name_to_abspath(master) + index_file