PDF Output: Fix a rare failure when the input document has a ToC item pointing to the last page. Fixes #1942012 [Private bug](https://bugs.launchpad.net/calibre/+bug/1942012)

This commit is contained in:
Kovid Goyal 2021-09-04 10:14:56 +05:30
parent 074dd7141d
commit 2dcc4fce2b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -619,12 +619,20 @@ def annotate_toc(toc, anchor_locations, name_anchor_map, log):
child.pdf_loc = loc child.pdf_loc = loc
def add_toc(pdf_parent, toc_parent): def add_toc(pdf_parent, toc_parent, log, pdf_doc):
for child in toc_parent: for child in toc_parent:
title, loc = child.title, child.pdf_loc title, loc = child.title, child.pdf_loc
try:
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)
except ValueError:
if loc.pagenum > 1:
log.warn(f'TOC node: {title} at page: {loc.pagenum} is beyond end of file, moving it to last page')
pdf_child = pdf_parent.create(title, pdf_doc.page_count(), True, loc.left, loc.top, loc.zoom)
else:
log.warn(f'Ignoring TOC node: {title} at page: {loc.pagenum}')
continue
if len(child): if len(child):
add_toc(pdf_child, child) add_toc(pdf_child, child, log, pdf_doc)
def get_page_number_display_map(render_manager, opts, num_pages, log): def get_page_number_display_map(render_manager, opts, num_pages, log):
@ -1078,7 +1086,7 @@ def convert(opf_path, opts, metadata=None, output_path=None, log=default_log, co
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) add_toc(PDFOutlineRoot(pdf_doc), toc, log, pdf_doc)
report_progress(0.75, _('Added links to PDF content')) report_progress(0.75, _('Added links to PDF content'))
pdf_metadata = PDFMetadata(metadata) pdf_metadata = PDFMetadata(metadata)