Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active May 3, 2024 11:17
Show Gist options
  • Save daveh/88ff8c5e2f99f0a11e9c7ef8e16c3888 to your computer and use it in GitHub Desktop.
Save daveh/88ff8c5e2f99f0a11e9c7ef8e16c3888 to your computer and use it in GitHub Desktop.
Generate a PDF with PHP (code to accompany https://youtu.be/XGS732DLtDc)
<!DOCTYPE html>
<html>
<head>
<title>PDF Example</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
<h1>Example</h1>
<form method="post" action="generate-pdf.php">
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="quantity">Quantity</label>
<input type="text" name="quantity" id="quantity">
<button>Generate PDF</button>
</form>
</body>
</html>
<?php
require __DIR__ . "/vendor/autoload.php";
use Dompdf\Dompdf;
use Dompdf\Options;
$name = $_POST["name"];
$quantity = $_POST["quantity"];
//$html = '<h1 style="color: green">Example</h1>';
//$html .= "Hello <em>$name</em>";
//$html .= '<img src="example.png">';
//$html .= "Quantity: $quantity";
/**
* Set the Dompdf options
*/
$options = new Options;
$options->setChroot(__DIR__);
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
/**
* Set the paper size and orientation
*/
$dompdf->setPaper("A4", "landscape");
/**
* Load the HTML and replace placeholders with values from the form
*/
$html = file_get_contents("template.html");
$html = str_replace(["{{ name }}", "{{ quantity }}"], [$name, $quantity], $html);
$dompdf->loadHtml($html);
//$dompdf->loadHtmlFile("template.html");
/**
* Create the PDF and set attributes
*/
$dompdf->render();
$dompdf->addInfo("Title", "An Example PDF"); // "add_info" in earlier versions of Dompdf
/**
* Send the PDF to the browser
*/
$dompdf->stream("invoice.pdf", ["Attachment" => 0]);
/**
* Save the PDF file locally
*/
$output = $dompdf->output();
file_put_contents("file.pdf", $output);
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/gutenberg-css@0.6">
<style>
table {
width: 100%;
}
footer {
text-align: center;
font-style: italic;
}
</style>
</head>
<body>
<img src="example.png">
<h1>Invoice</h1>
<p>Name: {{ name }}</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td>A sample product</td>
<td style="text-align: right">{{ quantity }}</td>
</tr>
</tbody>
</table>
<footer>
This is an example
</footer>
</body>
</html>
@ahmed-rabah
Copy link

very useful thank you very much

@chinnawut14158
Copy link

hi How to display results in Thailand fonts plz help me

@daveh
Copy link
Author

daveh commented Jan 11, 2023

What happens when you try it? What output do you get?

@rolandart
Copy link

Hi Dave,
Thanks for the video, I thought I messed something up, but the gutenberg doesn't work with the above code either, any idea what the cause might be?
thanks, Roland

@daveh
Copy link
Author

daveh commented Feb 10, 2023

@rolandart I just tried it and it didn't work with the unpkg.com CSS file for some reason - I changed it to this one and it worked:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gutenberg-css@0.6.1/dist/gutenberg.min.css">

@rolandart
Copy link

@rolandart I just tried it and it didn't work with the unpkg.com CSS file for some reason - I changed it to this one and it worked:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gutenberg-css@0.6.1/dist/gutenberg.min.css">

Thank you Dave!
Temporarily solved with a local file.

@yassinbenmansour
Copy link

great a clear and easy to understand video thank you so much. Would love to see a more complex form with lots of boxes and lines and shading.

@Maazter
Copy link

Maazter commented Mar 11, 2023

Hi Dave, This is a very intuitive video and is helping me understand a lot more things about generating a PDF, I do have one question though...All of my data is being processed into a .php file, so is it possible to pass my template.php through to the file_get_contents?

Regards,

Mark.

@daveh
Copy link
Author

daveh commented Jun 27, 2023

@Maazter Sorry Mark, only just saw this notification - no, you can't use file_get_contents to get the output from a PHP script, as it won't execute it. You can use output buffering with require though, something like this:

ob_start();
require 'path/to/file.php';
$output = ob_get_clean();

@Maazter
Copy link

Maazter commented Jun 27, 2023

Hi Dave,

Thnx for the reply, no worries for it being late :-), I solved this by creating a html page on the fly, then sending that to the file_get_contents, I suppose it's a bit of work around, but as I am using this functionality already to keep some of my PHP separate from my html I decided to re-use it.

Anyway thank you again for a most excellent video.

Mark.

@loocmm
Copy link

loocmm commented Aug 31, 2023

idont get thoes lines, even if i copypase your hole code. i also downloaded gutenberg and linked it localy and no change.
just wondering why

@rwsons
Copy link

rwsons commented Sep 3, 2023

Hi Daveh,
Could you please tell me the version of Dompdf you used in this project?

@daveh
Copy link
Author

daveh commented Sep 3, 2023

@rwsons Of course - version 1.2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment