title: With PHP, how can I check if a PDF file has errors
body: <p>I have a DB system built in PHP/MySql. I'm fairly new at this.  The system allows the user to upload an invoice.  Others give permission to pay the invoice. The accounting person uploads the check.  After check is uploaded, it generates a PDF as a cover, then uses PDFTK (using Ben Squire's PDFTK-PHP-Library) to combine all of the files together and present the user with a single PDF to download.  </p>
  
  <p>Some users upload PDF files which cause PDFTK to hang indefinitely when it tries to combine the PDF with others (but most of the time it works fine). No returned error, just hangs. In order to get back onto the sytem, user must clear cache and re-log in. There are no error messages logged by the server, it just freezes. The only difference I can find in the files that do or do not work in looking at them with Acrobat is that the bad files are legal sized (8.5 x 14) ... but if I create my own legal sized file and try that, it works fine. </p>
  
  <p>Using Putty I've gone to command line and replicated the same problem, PDFTK can't read the file, it hangs on the command line as well. I tried using PDFMerge which uses FPDF to combine the files and get an error with the file as well (The error I get back from this is: <b>FPDF error: Unable to find object (4, 0) at expected location</b>).  On the command line I was able to use ImageMagick to convert PDF to JPG, but it gives me an error: "Warning: File has an invalid xref entry: 2. Rebuilding xref table." and then it converts it to a jpg but gives a few other less helpful warnings. </p>
  
  <p>If I could get PHP to check the PDF file to determine if is valid without hanging the system, I could use ImageMagick to convert the file and then convert it back to a PDF, but I don't want to do this to all files.  How can I get it to check the validity of the file when uploaded to see if it needs to be converted without causing the system to hang? </p>
  
  <p>Here is a link to a file that is causing problems: <a href="http://www.cssc-testing.org/accounting/school_9/20130604-a1atransportation-1.pdf" rel="nofollow">http://www.cssc-testing.org/accounting/school_9/20130604-a1atransportation-1.pdf</a></p>
  
  <p>Thanks in advance for any guidance you can offer! </p>
  
  <p>My Code (which I'm guessing is not very clean, as I'm new): </p>
  
  <pre><code>$pdftk = new pdftk();
  if($create_cover) { $pdftk-&gt;setInputFile(array("filename" =&gt; $cover_page['server'])); }
  
  // Load a list of attachments
  $sql = "SELECT * FROM actg_attachments WHERE trans_id = {$trans_id}";
  $attachments = Attachment::find_by_sql($sql);
  foreach($attachments as $attachment) {
      // Check if the file exists from the attachments
      $attachment-&gt;set_variables();
      $file = $attachment-&gt;abs_path . DS . $attachment-&gt;filename;
      if(file_exists($file)){
          // Use the pdftk tool to attach the documents to this PDF
          $pdftk-&gt;setInputFile(array("filename" =&gt; $file));
      }
  }
  
  $pdftk-&gt;setOutputFile($save_file);
  $pdftk-&gt;_renderPdf();
  </code></pre>
  
  <p>the $pdftk class it is calling is from: <a href="https://github.com/bensquire/php-pdtfk-toolkit" rel="nofollow">https://github.com/bensquire/php-pdtfk-toolkit</a></p>