mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
a772d45227
commit
b2c8f6f8ee
@ -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)
|
||||
# }}}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user