PDF Output: Do not crash when unable to find page for link destination, instead use previous page. Fixes #1687914 [Private bug](https://bugs.launchpad.net/calibre/+bug/1687914)

This commit is contained in:
Kovid Goyal 2017-05-04 11:27:58 +05:30
parent 8f8dd46e81
commit e54d347726
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 20 additions and 7 deletions

View File

@ -13,6 +13,7 @@ from datetime import datetime
from binascii import hexlify from binascii import hexlify
from calibre.constants import plugins, ispy3 from calibre.constants import plugins, ispy3
from calibre.utils.logging import default_log
pdf_float = plugins['speedup'][0].pdf_float pdf_float = plugins['speedup'][0].pdf_float
@ -238,3 +239,11 @@ class Reference(object):
return repr(self) return repr(self)
# }}} # }}}
def current_log(newlog=None):
if newlog:
current_log.ans = newlog
return current_log.ans or default_log
current_log.ans = None

View File

@ -11,17 +11,23 @@ import os
from urlparse import urlparse from urlparse import urlparse
from urllib2 import unquote from urllib2 import unquote
from calibre.ebooks.pdf.render.common import Array, Name, Dictionary, String, UTF16String from calibre.ebooks.pdf.render.common import Array, Name, Dictionary, String, UTF16String, current_log
class Destination(Array): class Destination(Array):
def __init__(self, start_page, pos, get_pageref): def __init__(self, start_page, pos, get_pageref):
pnum = start_page + max(0, pos['column']) pnum = start_page + max(0, pos['column'])
try: q = pnum
pref = get_pageref(pnum) while q > -1:
except IndexError: try:
pref = get_pageref(pnum-1) pref = get_pageref(q)
break
except IndexError:
pos['left'] = pos['top'] = 0
q -= 1
if q != pnum:
current_log().warn('Could not find page {} for link destination, using page {} instead'.format(pnum, q))
super(Destination, self).__init__([ super(Destination, self).__init__([
pref, Name('XYZ'), pos['left'], pos['top'], None pref, Name('XYZ'), pos['left'], pos['top'], None
]) ])
@ -139,5 +145,3 @@ class Links(object):
item = Dictionary({'Parent':parentref, 'Dest':dest, item = Dictionary({'Parent':parentref, 'Dest':dest,
'Title':UTF16String(toc.text or _('Unknown'))}) 'Title':UTF16String(toc.text or _('Unknown'))})
return self.pdf.objects.add(item) return self.pdf.objects.add(item)