-
-
Save daveh/88ff8c5e2f99f0a11e9c7ef8e16c3888 to your computer and use it in GitHub Desktop.
<!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/[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> |
hi How to display results in Thailand fonts plz help me
What happens when you try it? What output do you get?
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
@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/[email protected]/dist/gutenberg.min.css">
@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/[email protected]/dist/gutenberg.min.css">
Thank you Dave!
Temporarily solved with a local file.
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.
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.
@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();
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.
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
very useful thank you very much