pythonxmlpdfpymupdf

Adding data object XML to PDF using PyMuPDF


I am struggling to add a data object to a PDF using PyMuPDF. I am successful adding a PDF as an embedded file but I can not add an XML file. I am trying using the following function : embfile_add.

The embedded XML file will be used to get data into a PDF form dynamically.

This is the code I am trying :

import fitz
import os
path = r"c\temp"
namedoc = "document.pdf"
pathnamedoc = os.path.join(path,namedoc)
print(pathnamedoc)

doc = fitz.open(pathnamedoc) # open main document
count = doc.embfile_count()
print("number of embedded file:", count)     # shows number of embedded files
namedata = "data.xml"
pathnamedata = os.path.join(path,namedata)
print(pathnamedata)

embedded_doc = fitz.open(pathnamedata) # open document you want to embed
embedded_data = embedded_doc.tobytes() # get the document byte data as a buffer
doc.embfile_add("data.xml", embedded_data)
doc.saveIncr()

but I keep having the following error:

RuntimeError: is no PDF

Solution

  • This is the code above corrected

    import fitz
    import os
    import pathlib
    path = r"C:\temp"
    namedoc = "document.pdf"
    pathnamedoc = os.path.join(path,namedoc)
    print(pathnamedoc)
    
    doc = fitz.open(pathnamedoc) # open main document
    count = doc.embfile_count()
    print("number of embedded file:", count)     # shows number of embedded files
    namedata = "data.xml"
    pathnamedata = os.path.join(path,namedata)
    print(pathnamedata)
    
    embedded_doc = fitz.open(pathnamedata) # open document you want to embed
    embedded_data = pathlib.Path(pathnamedata).read_bytes() # get the document byte data as a buffer
    doc.embfile_add("data.xml", embedded_data)
    doc.saveIncr()