title: Check whether a PDF-File is valid with Python
body: <p><strong>I get a File via a HTTP-Upload and need to be sure its a pdf-file.</strong> Programing Language is Python, but this should not matter.</p>
  <p>I thought of the following solutions:</p>
  <ol>
  <li><p>Check if the first bytes of the string are &quot;%PDF&quot;. <em>This is not a good check but prevents the use from uploading other files accidentally.</em></p>
  </li>
  <li><p>Try the libmagic (the &quot;file&quot; command on the bash uses it). <em>This does exactly the same check as in (1)</em></p>
  </li>
  <li><p>Take a lib and try to read the page-count out of the file. <em>If the lib is able to read a pagecount it should be a valid pdf. Problem: I dont know a lib for python which can do this</em></p>
  </li>
  </ol>
  <p>So anybody got any solutions for a lib or another trick?</p>