pdfrw
pdfrw copied to clipboard
Writing XMP metadata
I'm able to read XMP metadata with
pdf = PdfReader('example.pdf')
metadata = pdf.Root.Metadata # {'/Length': '503', '/Subtype': '/XML', '/Type': '/Metadata'}
xmp = metadata.stream
but I haven't quite figured out how to write metadata yet. As far as I understand, I should do something like
trailer = PdfReader('nometadata.pdf')
trailer.Root.Metadata = IndirectPdfDict(...) # not sure of the arguments
PdfWriter('out.pdf', trailer=trailer).write()
How do I write into the .stream
attribute?
Not sure I understand the question. trailer.Root.Metadata.stream = xmp
should set the stream and the stream length.
Hi @pmaupin
This might not be the proper place to ask this question but I don't know where else: The above works for me with pdfs that already have xmp metadata (trailer.Root.Metadata.stream = xmp).
However, when trailer.Root.Metadata is None, it (obviously) fails. Is there a way to add the Metadata attribute if it does not exist yet?