0

I use PyMuPDF's insert_link to add links to a PDF. But when I do it, I sometimes get the warning skipping bad link / annot item 0. When I highlight the same rect with add_highlight_annot the area is highlighted. There is just no link. This happens to some words, but I can't find a connection between them. What does the warning mean exactly?

Bellow is the code I use:

doc = fitz.open('test.pdf')
term = "hello"
for page in doc:
    rects = page.search_for(term)
    for rect in rects:
       page.add_highlight_annot(rect)
       l = {'kind': 2, 'from': rect, 'uri': link, 'id': ''}
       page.insert_link(l)
Mazze
  • 383
  • 3
  • 13
  • Maybe first use `print()` (and `print(type(...))`, `print(len(...))`, etc.) to see which part of code is executed and what you really have in variables. It is called `"print debuging"` and it helps to see what code is really doing. – furas Oct 20 '22 at 19:37
  • 1
    thanks for the tip. Turns out that the link text was of for some links and a `)` was missing. – Mazze Oct 20 '22 at 21:58

1 Answers1

1

This may happen if some text of the link is not UTF-8 encodedable. Internally in PyMuPDF's C code, PDF objects for defining the link are created from their representation as text source. The error occurs if the Python C-function PyUnicode_AsUTF8 fails. In your context most probably the URI text should be the problem.

Jorj McKie
  • 2,062
  • 1
  • 13
  • 17