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

Dynamic HTML Page

achristmann wrote on 2016-02-23:
I have a page which uses JS to generate dynamic content based on what the user inputs. Once complete, I would like to save it to PDF. I couldn't find a whole lot of documentation on this so I'm using PHP Object Buffering to save the completed HTML to the server, then pdfcrowd to generate a PDF from the recently saved file. I'm not sure if this is proper but it works WHEN I use two separate buttons, one to generate the HTML file and another to call the PHP code like below.

<form action="" method="post">
      <input type="submit" value="Save HTML" />
      <input type="hidden" name="button_pressed" value="1" />
</form>
<form action="savepdf.php" method="post">
      <input type="submit" value="Save PDF" />
      <input type="hidden" name="button_pressed" value="1" />
</form>
<?php
try
if (isset($_POST['button_pressed']))
{
		file_put_contents('newpage2.html', ob_get_contents());
}
?> 


The following set of code does not work. It will save the PDF but I cannot open it with any PDF viewer.

<?php
require 'inc/pdfcrowd.php';
ob_start();
?>
<form action="" method="post">
      <input type="submit" value="Save 2 PDF" />
      <input type="hidden" name="button_pressed" value="1" />
</form>
<?php
try
{  
	if (isset($_POST['button_pressed']))
	{
		file_put_contents('newpage2.html', ob_get_contents()); 
		
		$client = new Pdfcrowd("***", "***");
	
		$pdf = $client->convertFile("newpage2.html");
	
		header("Content-Type: application/pdf");
		header("Cache-Control: no-cache");
		header("Accept-Ranges: none");
		header("Content-Disposition: inline; filename=\"newPDF2.pdf\"");
	
		echo $pdf;
	}
}
catch(PdfcrowdException $why)
{
    echo "Pdfcrowd Error: " . $why;
}
?>


Any help is much appreciated.
support wrote on 2016-02-26:
Hello,

If I understand your scenario correctly I would go about it like this:

1/ use JS to generate dynamic content from the user's input
2/ POST the generated content to your server
3/ on the server, post the content to our API and receive PDF
4/ send the generated PDF back to the browser

Hope this helps.
lsaulapoll wrote on 2016-09-20:
@achristmann Did you ever find a resolution to this? I also had a similar issue.

I was able to generate the PDF by passing the html value via POST, then writing that value to a static file. First I grabbed the html via jQuery, then set a hidden input value to the html that was just grabbed. Then once the value is set I passed it to a PHP file, which would take the variable and write that to a static text file. The PDF crowd API is then able to handle creating the PDF from that static text file.

The problem I run into comes after that. I can open the PDF in the browser - Firefox and Chrome seem to accept the PDF just fine. However I can't open the PDF in Acrobat or Photoshop.

Unfortunately, that's exactly what I need to do with it. It needs to be added to another PDF, but if I can't open it I can't edit it.

Anyway, I think it has something to do with the headers, but everything is setup the way the PDFCrowd API documentation shows. So I don't know.

If anybody has found a solution to this it would be very helpful if you could share it here. Thanks!