PDF Output: Fix conversion failing if there are ToC entries pointing to removed content. Fixes #1960554 [Private bug](https://bugs.launchpad.net/calibre/+bug/1960554)

This commit is contained in:
Kovid Goyal 2022-03-03 15:33:33 +05:30
parent a772d45227
commit b2c8f6f8ee
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 3 deletions

View File

@ -569,6 +569,7 @@ def get_anchor_locations(name, pdf_doc, first_page_num, toc_uuid, log):
def fix_links(pdf_doc, anchor_locations, name_anchor_map, mark_links, log):
pc = pdf_doc.page_count()
def replace_link(url):
purl = urlparse(url)
@ -583,7 +584,12 @@ def fix_links(pdf_doc, anchor_locations, name_anchor_map, mark_links, log):
loc = anchor_locations.get(name_anchor_map.get(purl.fragment))
if loc is None:
log.warn(f'Anchor location for link to {purl.fragment} not found')
return None if loc is None else loc.as_tuple
if loc is None:
return None
if loc.pagenum > pc:
log.warn(f'Anchor location for link to {purl.fragment} is past the end of the document, moving it to last page')
loc.pagenum = pc
return loc.as_tuple
pdf_doc.alter_links(replace_link, mark_links)
# }}}

View File

@ -483,7 +483,7 @@ alter_link(PDFDoc *self, PdfDictionary &link, PyObject *alter_callback, bool mar
page = self->doc->GetPage(pagenum - 1);
} catch(const PdfError &err) {
(void)err;
PyErr_Format(PyExc_ValueError, "No page number %d in the PDF file", pagenum);
PyErr_Format(PyExc_ValueError, "No page number %d in the PDF file of %d pages", pagenum, self->doc->GetPageCount());
return ;
}
if (page) {
@ -525,7 +525,10 @@ PDFDoc_alter_links(PDFDoc *self, PyObject *args) {
}
for (auto const & ref: links) {
PdfObject *lo = self->doc->GetObjects().GetObject(ref);
if (lo) alter_link(self, lo->GetDictionary(), alter_callback, mark_links, border, link_color);
if (lo) {
alter_link(self, lo->GetDictionary(), alter_callback, mark_links, border, link_color);
if (PyErr_Occurred()) return NULL;
}
}
} catch(const PdfError & err) {
podofo_set_exception(err);