pdfrw icon indicating copy to clipboard operation
pdfrw copied to clipboard

Writing XMP metadata

Open kskyten opened this issue 6 years ago • 2 comments

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?

kskyten avatar Jun 15 '18 08:06 kskyten

Not sure I understand the question. trailer.Root.Metadata.stream = xmp should set the stream and the stream length.

pmaupin avatar Jun 15 '18 19:06 pmaupin

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?

adrianschlatter avatar Apr 11 '21 20:04 adrianschlatter