title: Editing PDF metadata fields with Python3 and pdfrw
body: <p>I'm trying to edit the metadata <code>Title</code> field of PDFs, to include the ASCII equivalents when possible. I'm using Python3 and the module <a href="https://pypi.org/project/pdfrw/" rel="nofollow noreferrer"><code>pdfrw</code></a>.</p>
  
  <p>How can I do string operations that replace the metadata fields?</p>
  
  <p>My test code is here: </p>
  
  <pre><code>from pdfrw import PdfReader, PdfWriter, PdfString
  import unicodedata
  
  def edit_title_metadata(inpdf):
  
      trailer = PdfReader(inpdf)
  
      # this statement is breaking pdfrw
      trailer.Info.Title = unicode_normalize(trailer.Info.Title)
  
      # also have tried:
      #trailer.Info.Title = PdfString(unicode_normalize(trailer.Info.Title))
  
      PdfWriter("test.pdf", trailer=trailer).write()
      return
  
  def unicode_normalize(s):
      return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')
  
  if __name__ == "__main__":
  
      edit_title_metadata('Anadon-2011-Scientific Opinion on the safety e.pdf')
  </code></pre>
  
  <p>And the traceback is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "get_metadata.py", line 68, in &lt;module&gt;
      main()
    File "get_metadata.py", line 54, in main
      edit_title_metadata(pdf)
    File "get_metadata.py", line 11, in edit_title_metadata
      trailer.Info.Title = PdfString(unicode_normalize(trailer.Info.Title))
    File "get_metadata.py", line 18, in unicode_normalize
      return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')
    File "/path_to_python/python3.7/site-packages/pdfrw/objects/pdfstring.py", line 550, in encode
      if isinstance(source, uni_type):
  TypeError: isinstance() arg 2 must be a type or tuple of types
  </code></pre>
  
  <hr>
  
  <p>Notes:</p>
  
  <ul>
  <li><p><a href="https://github.com/JoshData/pdf-redactor/issues/11" rel="nofollow noreferrer">This issue</a> at GitHub may be related.</p></li>
  <li><p>FWIW, Also getting same error with Python3.6</p></li>
  <li><p>I've shared the pdf (which has non-ascii hyphens, unicode char \u2010)</p></li>
  </ul>
  
  <p>.</p>
  
  <pre><code> wget https://gist.github.com/philshem/71507d4e8ecfabad252fbdf4d9f8bdd2/raw/cce346ab39dd6ecb3a718ad3f92c9f546761e87b/Anadon-2011-Scientific%2520Opinion%2520on%2520the%2520safety%2520e.pdf
  </code></pre>