Skip to content

Instantly share code, notes, and snippets.

@HelgeSverre
Last active November 15, 2016 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelgeSverre/29b8228b61cd42cecf42737f3ad397e0 to your computer and use it in GitHub Desktop.
Save HelgeSverre/29b8228b61cd42cecf42737f3ad397e0 to your computer and use it in GitHub Desktop.
<?php
/*
Assuming that all the data is the same "form", we can simply grab the data from the request
and render it as HTML, then convert that HTML doument to a PDF using the DomPDF library.
*/
// Grab the request data
$request = $_POST;
// Build our template with the data from the request
// Note: when doing html to PDF type stuff, table layouts are usually the way to go.
$template = <<<EOT
<h1>Super Important Form</h1>
<table border="1">
<tr>
<th>Your Name</th>
<th>{$request['invoiceId']}</th>
</tr>
<tr>
<td>Your favorite color</td>
<td>{$request['favoriteColor']}</td>
</tr>
<tr>
<td>Your Age</td>
<td>{$request['age']}</td>
</tr>
</table>
EOT;
// Create our instance of DomPDF
$dompdf = new Dompdf\Dompdf();
// Load the template we built
$dompdf->loadHtml($template);
// Set the size of the pdf document and the orientation
$dompdf->setPaper("A4", "portrait");
// "Builds" the pdf internally
$dompdf->render();
// Now we can either SAVE it by doing this:
file_put_contents("/some/directory/my-file.pdf", $dompdf->output());
// OR
// stream it to the browser (trigger a download dialog)
$dompdf->stream("my-file.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment