0

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
Camilo
  • 335
  • 5
  • 7
  • 1
    The string `r"c\temp"` is an invalid path name (colon missing after c). Second `namedata` is an XML file. You cannot output XML files via `.save` or `.tobytes` - only PDF. Why don't you use `pathlib.Path` to directly read just **any** file as binary data that you then can insert as embedded file: `content = pathlib.Path("any.file").read_bytes()`. Then do `doc.embfile_add("any.file", content)`. – Jorj McKie Jun 08 '23 at 21:15

1 Answers1

0

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()
Camilo
  • 335
  • 5
  • 7