0

I want to add custom metadata to pdf file. This can be achieved by pypdf2 or pdrw library. I have referred Change metadata of pdf file with pypdf2 solution works fine, when there is no space between the two words of attribute.

In my case my metadata is like

meta = {'Page description' : 'description',
        'create time' : '123455' }

when I try to add above metadata as :

reader = PdfFileReader(filename)
writer = PdfFileWriter()
writer.appendPagesFromReader(reader)
writer.addMetadata(meta)
with open(filename, 'wb') as fout:
    writer.write(fout)

when we try to see the custom properties in document property of pdf, nothing is displayed.

The above solution works fine if attributes are without spaces

Nitesh Selkari
  • 67
  • 1
  • 2
  • 8

1 Answers1

0

I do not know about pypdfw, but for pdfrw, you should use PdfName('Page description') as your dictionary key. This will properly encode the space.

Disclaimer: I am the primary pdfrw author.

Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
  • I tried this solution, but no success.. Just to confirm, here is my dictionary look like now: ``` meta = {'PdfName(Page description)': 'Description', 'PdfName(create time)': '12345'} ``` – Nitesh Selkari Apr 30 '19 at 10:34
  • PdfName should not be inside the quotes. It is a function inside pdfrw. Also, you should consider using PdfDict instead of a real dict. pdfrw works better when you use its primitives that map to PDF primitives. – Patrick Maupin Apr 30 '19 at 16:43
  • The following code worked for me: trailer = PdfReader(filename) for k,v in dct.items(): trailer.Info[pdfrw.objects.pdfname.PdfName(k)] = v PdfWriter(filename, trailer=trailer).write() Here dct = dictionary object @Patrick Maupin : will try to use PdfDict as well. – Nitesh Selkari May 02 '19 at 09:26
  • when I am creating dictionary with PdfName, I am getting this error : Expected PDF /name object (line=14, col=14, token='00:00:00'). Can you help in this – Nitesh Selkari May 24 '19 at 10:06