mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Report progress in PDF text output plugin
This commit is contained in:
parent
9f87cd7f99
commit
089789dd19
@ -236,5 +236,5 @@ class PDFOutput(OutputFormatPlugin):
|
|||||||
opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0]
|
opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0]
|
||||||
convert(
|
convert(
|
||||||
opfpath, self.opts, metadata=self.metadata, output_path=self.output_path,
|
opfpath, self.opts, metadata=self.metadata, output_path=self.output_path,
|
||||||
log=self.log, cover_data=self.cover_data
|
log=self.log, cover_data=self.cover_data, report_progress=self.report_progress
|
||||||
)
|
)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
# vim:fileencoding=utf-8
|
# vim:fileencoding=utf-8
|
||||||
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
# Imports {{{
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
import json
|
import json
|
||||||
@ -35,8 +36,10 @@ from polyglot.builtins import iteritems, range
|
|||||||
from polyglot.urllib import urlparse
|
from polyglot.urllib import urlparse
|
||||||
|
|
||||||
OK, KILL_SIGNAL = range(0, 2)
|
OK, KILL_SIGNAL = range(0, 2)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# Renderer {{{
|
||||||
class Container(ContainerBase):
|
class Container(ContainerBase):
|
||||||
|
|
||||||
tweak_mode = True
|
tweak_mode = True
|
||||||
@ -168,6 +171,30 @@ class RenderManager(QObject):
|
|||||||
QApplication.instance().exit(OK)
|
QApplication.instance().exit(OK)
|
||||||
|
|
||||||
|
|
||||||
|
def job_for_name(container, name, margins, page_layout):
|
||||||
|
index_file = container.name_to_abspath(name)
|
||||||
|
if margins:
|
||||||
|
page_layout = QPageLayout(page_layout)
|
||||||
|
page_layout.setUnits(QPageLayout.Point)
|
||||||
|
old_margins = page_layout.marginsPoints()
|
||||||
|
new_margins = QMarginsF(
|
||||||
|
margins.get('left', old_margins.left()),
|
||||||
|
margins.get('top', old_margins.top()),
|
||||||
|
margins.get('right', old_margins.right()),
|
||||||
|
margins.get('bottom', old_margins.bottom()))
|
||||||
|
page_layout.setMargins(new_margins)
|
||||||
|
return index_file, page_layout, name
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# Metadata {{{
|
||||||
|
def data_as_pdf_doc(data):
|
||||||
|
podofo = get_podofo()
|
||||||
|
ans = podofo.PDFDoc()
|
||||||
|
ans.load(data)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def update_metadata(pdf_doc, pdf_metadata):
|
def update_metadata(pdf_doc, pdf_metadata):
|
||||||
if pdf_metadata.mi:
|
if pdf_metadata.mi:
|
||||||
xmp_packet = metadata_to_xmp_packet(pdf_metadata.mi)
|
xmp_packet = metadata_to_xmp_packet(pdf_metadata.mi)
|
||||||
@ -176,13 +203,6 @@ def update_metadata(pdf_doc, pdf_metadata):
|
|||||||
pdf_metadata.mi.book_producer, pdf_metadata.mi.tags, xmp_packet)
|
pdf_metadata.mi.book_producer, pdf_metadata.mi.tags, xmp_packet)
|
||||||
|
|
||||||
|
|
||||||
def data_as_pdf_doc(data):
|
|
||||||
podofo = get_podofo()
|
|
||||||
ans = podofo.PDFDoc()
|
|
||||||
ans.load(data)
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
def add_cover(pdf_doc, cover_data, page_layout, opts):
|
def add_cover(pdf_doc, cover_data, page_layout, opts):
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
page_size = page_layout.fullRectPoints().size()
|
page_size = page_layout.fullRectPoints().size()
|
||||||
@ -193,8 +213,10 @@ def add_cover(pdf_doc, cover_data, page_layout, opts):
|
|||||||
writer.end()
|
writer.end()
|
||||||
cover_pdf_doc = data_as_pdf_doc(buf.getvalue())
|
cover_pdf_doc = data_as_pdf_doc(buf.getvalue())
|
||||||
pdf_doc.insert_existing_page(cover_pdf_doc)
|
pdf_doc.insert_existing_page(cover_pdf_doc)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# Margin groups {{{
|
||||||
def create_margin_groups(container):
|
def create_margin_groups(container):
|
||||||
|
|
||||||
def merge_group(group):
|
def merge_group(group):
|
||||||
@ -221,23 +243,10 @@ def create_margin_groups(container):
|
|||||||
if current_group:
|
if current_group:
|
||||||
groups.append(merge_group(current_group))
|
groups.append(merge_group(current_group))
|
||||||
return groups
|
return groups
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
def job_for_name(container, name, margins, page_layout):
|
# Link handling {{{
|
||||||
index_file = container.name_to_abspath(name)
|
|
||||||
if margins:
|
|
||||||
page_layout = QPageLayout(page_layout)
|
|
||||||
page_layout.setUnits(QPageLayout.Point)
|
|
||||||
old_margins = page_layout.marginsPoints()
|
|
||||||
new_margins = QMarginsF(
|
|
||||||
margins.get('left', old_margins.left()),
|
|
||||||
margins.get('top', old_margins.top()),
|
|
||||||
margins.get('right', old_margins.right()),
|
|
||||||
margins.get('bottom', old_margins.bottom()))
|
|
||||||
page_layout.setMargins(new_margins)
|
|
||||||
return index_file, page_layout, name
|
|
||||||
|
|
||||||
|
|
||||||
def add_anchors_markup(root, uuid, anchors):
|
def add_anchors_markup(root, uuid, anchors):
|
||||||
body = root[-1]
|
body = root[-1]
|
||||||
div = body.makeelement(XHTML('div'), id=uuid, style='page-break-before: always')
|
div = body.makeelement(XHTML('div'), id=uuid, style='page-break-before: always')
|
||||||
@ -358,8 +367,10 @@ def fix_links(pdf_doc, anchor_locations, name_anchor_map, mark_links, log):
|
|||||||
return loc
|
return loc
|
||||||
|
|
||||||
pdf_doc.alter_links(replace_link, mark_links)
|
pdf_doc.alter_links(replace_link, mark_links)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# Outline creation {{{
|
||||||
class PDFOutlineRoot(object):
|
class PDFOutlineRoot(object):
|
||||||
|
|
||||||
def __init__(self, pdf_doc):
|
def __init__(self, pdf_doc):
|
||||||
@ -388,15 +399,18 @@ def add_toc(pdf_parent, toc_parent, anchor_locations, name_anchor_map, log):
|
|||||||
pdf_child = pdf_parent.create(title, loc.pagenum, True, loc.left, loc.top, loc.zoom)
|
pdf_child = pdf_parent.create(title, loc.pagenum, True, loc.left, loc.top, loc.zoom)
|
||||||
if len(child):
|
if len(child):
|
||||||
add_toc(pdf_child, child, anchor_locations, name_anchor_map, log)
|
add_toc(pdf_child, child, anchor_locations, name_anchor_map, log)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
def convert(opf_path, opts, metadata=None, output_path=None, log=default_log, cover_data=None):
|
def convert(opf_path, opts, metadata=None, output_path=None, log=default_log, cover_data=None, report_progress=lambda x, y: None):
|
||||||
container = Container(opf_path, log)
|
container = Container(opf_path, log)
|
||||||
|
report_progress(0.05, _('Parsed all content for markup transformation'))
|
||||||
margin_groups = create_margin_groups(container)
|
margin_groups = create_margin_groups(container)
|
||||||
name_anchor_map = make_anchors_unique(container)
|
name_anchor_map = make_anchors_unique(container)
|
||||||
toc = get_toc(container, verify_destinations=False)
|
toc = get_toc(container, verify_destinations=False)
|
||||||
links_page_uuid = add_all_links(container, margin_groups)
|
links_page_uuid = add_all_links(container, margin_groups)
|
||||||
container.commit()
|
container.commit()
|
||||||
|
report_progress(0.1, _('Completed markup transformation'))
|
||||||
|
|
||||||
manager = RenderManager(opts)
|
manager = RenderManager(opts)
|
||||||
page_layout = get_page_layout(opts)
|
page_layout = get_page_layout(opts)
|
||||||
@ -421,20 +435,23 @@ def convert(opf_path, opts, metadata=None, output_path=None, log=default_log, co
|
|||||||
pdf_doc = doc
|
pdf_doc = doc
|
||||||
else:
|
else:
|
||||||
pdf_doc.append(doc)
|
pdf_doc.append(doc)
|
||||||
|
report_progress(0.7, _('Rendered all HTML as PDF'))
|
||||||
|
|
||||||
fix_links(pdf_doc, anchor_locations, name_anchor_map, opts.pdf_mark_links, log)
|
fix_links(pdf_doc, anchor_locations, name_anchor_map, opts.pdf_mark_links, log)
|
||||||
if toc and len(toc):
|
if toc and len(toc):
|
||||||
add_toc(PDFOutlineRoot(pdf_doc), toc, anchor_locations, name_anchor_map, log)
|
add_toc(PDFOutlineRoot(pdf_doc), toc, anchor_locations, name_anchor_map, log)
|
||||||
|
report_progress(0.75, _('Added links to PDF content'))
|
||||||
|
|
||||||
|
# TODO: Remove unused fonts
|
||||||
|
# TODO: Remove duplicate fonts
|
||||||
|
# TODO: Subset and embed fonts before rendering PDF
|
||||||
|
|
||||||
if cover_data:
|
if cover_data:
|
||||||
add_cover(pdf_doc, cover_data, page_layout, opts)
|
add_cover(pdf_doc, cover_data, page_layout, opts)
|
||||||
|
|
||||||
if metadata is not None:
|
if metadata is not None:
|
||||||
update_metadata(pdf_doc, PDFMetadata(metadata))
|
update_metadata(pdf_doc, PDFMetadata(metadata))
|
||||||
|
report_progress(1, _('Updated metadata in PDF'))
|
||||||
# TODO: Remove unused fonts
|
|
||||||
# TODO: Remove duplicate fonts
|
|
||||||
# TODO: Subset and embed fonts before rendering PDF
|
|
||||||
|
|
||||||
pdf_data = pdf_doc.write()
|
pdf_data = pdf_doc.write()
|
||||||
if output_path is None:
|
if output_path is None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user