Last active
April 25, 2025 12:06
-
-
Save daveh/88ff8c5e2f99f0a11e9c7ef8e16c3888 to your computer and use it in GitHub Desktop.
Generate a PDF with PHP (code to accompany https://youtu.be/XGS732DLtDc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<link rel="stylesheet" href="https://unpkg.com/[email protected]"> | |
<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> |
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
Hi Daveh,
Could you please tell me the version of Dompdf you used in this project?
@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
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.