Skip to content

Instantly share code, notes, and snippets.

@oleteacher
Created January 5, 2023 21:49
Show Gist options
  • Save oleteacher/5c1ab1ca54eb717837a09de5278784ac to your computer and use it in GitHub Desktop.
Save oleteacher/5c1ab1ca54eb717837a09de5278784ac to your computer and use it in GitHub Desktop.
dompdf example
<!DOCTYPE html>
<html>
<head>
<title>Convert HTML to PDF using DomPDF and PHP</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.2.3/morph/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Create PDF</h2>
<form action="dompdf_generate.php" method="post">
<div class="form-group">
<label>Your Name:</label>
<input type="text" name="yourname" class="form-control" placeholder="Your Name" required>
</div>
<div class="form-group">
<label>Your Email:</label>
<input type="email" name="youremail" class="form-control" placeholder="Your Email" required>
</div>
<div class="form-group">
<label>Your Website URL:</label>
<input type="url" name="yoururl" class="form-control" placeholder="https://Your URL" required>
</div>
<div class="form-group">
<label>About Your Website:</label>
<textarea name="yourtext" class="form-control" placeholder="Textarea"></textarea>
</div>
<div class="form-group">
<br><p><button class="btn btn-success">Generate the PDF</button></p>
</div>
</form>
</div>
</body>
</html>
====================================================================
<?php
/* include vendor autoloader */
require_once '../dompdf/vendor/autoload.php';
/* get the Dompdf namespace */
use Dompdf\Dompdf;
/* start and use the dompdf class */
$dompdf = new Dompdf();
$html = '<style>th {text-align: left;}</style><h1>All About '.$_POST['yourname'].'</h1>
<table align="left" class="table">
<tr>
<th>Name:</th>
<td>'.$_POST['yourname'].'</td>
</tr>
<tr>
<th>Email:</th>
<td>'.$_POST['youremail'].'</td>
</tr>
<tr>
<th>URL:</th>
<td>'.$_POST['yoururl'].'</td>
</tr>
<tr>
<th>About:</th>
<td>'.$_POST['yourtext'].'</td>
</tr>
</table>';
$dompdf->loadHtml($html);
/* Render HTML as PDF */
$dompdf->render();
/* Output the generated PDF to Browser */
/* Downloads a PDF */
//$dompdf->stream();
/* Downloads a PHP??? */
$dompdf->stream("thePDF", array("Attachment" => 0));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment