Fix pdf_add_stream metadata error by clpo13 · Pull Request #3112 · pymupdf/PyMuPDF (original) (raw)
I ran into a problem with the recode_pdf tool from archive-pdf-tools, which uses PyMuPDF. This error occurs with versions of PyMuPDF from 1.23.9 and on:
$ recode_pdf --from-imagestack test001.tif --hocr-file test001.hocr --dpi 400 -o test001.pdf Traceback (most recent call last): File "/home/cody/ocrtest/venv/bin/recode_pdf", line 301, in res = recode(args.from_pdf, args.from_imagestack, args.dpi, args.hocr_file, File "/home/cody/ocrtest/venv/lib/python3.10/site-packages/internetarchivepdf/recode.py", line 749, in recode write_metadata(in_pdf, outdoc, extra_metadata=extra_metadata) File "/home/cody/ocrtest/venv/lib/python3.10/site-packages/internetarchivepdf/pdfhacks.py", line 516, in write_metadata to_pdf.set_xml_metadata(stream) File "/home/cody/ocrtest/venv/lib/python3.10/site-packages/fitz/init.py", line 5580, in set_xml_metadata xml = mupdf.pdf_add_stream( pdf, res, None, 0) File "/home/cody/ocrtest/venv/lib/python3.10/site-packages/fitz/mupdf.py", line 45040, in pdf_add_stream return _mupdf.pdf_add_stream(doc, buf, obj, compressed) ValueError: invalid null reference in method 'pdf_add_stream', argument 3 of type 'mupdf::PdfObj const &'
The change in this PR follows the pattern of the other calls to mupdf.pdf_add_stream in src/__init__.py, but if there's a better way to do it, let me know.