This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Unable to open PDF in Acrobat or PS

lsaulapoll wrote on 2016-09-20:
Hi,
First off - thanks for the excellent service you all are providing. I was very happy to be able to generate a PDF so easily, especially one that reads CSS styling.

I was excited to finally get my app working to generate PDFs, but was disappointed that I was unable to open the PDF in the software we use at the office - namely Photoshop and Acrobat. I've attached a screenshot of the error and a copy of the PDF.

Below you will see the code that I am using to call the output.

 function printPDF(){
         

        try
        {   
             
            // create an API client instance
            $client = new Pdfcrowd("lsaul", "9425dc76600b63a829ddb12ad715f8c5");

            // convert a web page and store the generated PDF into a $pdf variable
            $pdf = $client->convertURI('http://example.com/dev.charts/pages/testfile.html');

            // set HTTP response headers
            header("Content-Type: application/pdf");
            header("Cache-Control: no-cache");  
            header("Accept-Ranges: none");
            header("Content-Disposition: attachment; filename=\"apoll_table.pdf\"");

            // send the generated PDF 
            echo $pdf;
        }
        catch(PdfcrowdException $why)
        {
            echo "Pdfcrowd Error: " . $why;
        }
      }


I came across another forum somewhere that seemed to indicate an issue with the 'PDF header' would cause something like that. I previously tried using:

header("Cache-Control: max-age=0");


Though that doesn't work either.

Can you please help me figure out what I need to change in my code to generate a PDF without errors?

Thanks!
support wrote on 2016-09-21:
Hello,

We analyzed the PDF file and It looks like your app echoes some HTML code before and after the $pdf variable is echoed. This causes that the PDF file is invalid. Please ensure that there is no output other than "echo $pdf"
lsaulapoll wrote on 2016-09-23:
Thanks for pointing that out. I'll have to look at my code and figure out how that could be happening. I'll post it here as well for the sake of review:

I know I'll have to change something, but looking over it initially I don't see why that would be an issue. I write html prior to sending anything to the PDFCrowd API. As you can see below:

<?php 
 
      
      $print = $_POST['outputpdf'];
      $table_content = $_POST['pdf_table'];
 
?>
  <!DOCTYPE html>
  <html>
  <head>
    <title></title>
  </head>
  <body> 
   <?php 
      echo "<h3 style='color: #0275d8;' class='container'>Click on 'Print PDF' to create a PDF of this table, or click 'Back' to restart.</h3>";
      echo $table_content;

      $myfile = fopen("testfile.html", "w");
      $txt = "<!DOCTYPE html><html><head><title>Table Output</title></head><body> <link href='../bower_components/bootstrap/dist/css/bootstrap.min.css' rel='stylesheet'>";
      fwrite($myfile, $txt);

      $txt = $table_content;
      fwrite($myfile, $txt);

      $txt = "</body></html>";
      fwrite($myfile, $txt);
      fclose($myfile);
     ?>

     <div class="container">
       <button class="btn btn-danger" onclick="history.go(-1);" style="display:inline-block; float: left;">Back</button>

     <form id="output" method="post" action="http://www.example.com/dev.charts/pdf-print.php">
       <label><img id="loadingimg" style="display: none;" src="http://dev.cloudcell.co.uk/bin/loading.gif"/>   </label>
       <input id="printButton" class="btn btn-success" type="Submit" value="Print PDF" name='outputpdf'>
     </form>
     </div>

  </body>
  </html>



As you can see the html file with the dynamic table is created. Then once that is available, the form posts to another file which calls the 'printPDF' function and runs the PDFCrowd API.

So there must be a problem with the content in 'testfile.html'.Just noticing the additional '<html>'...will have to remove that and report back.

Thanks!