How to convert html to Pdf using Dompdf in PHP
DOMPDF is convert HTML to PDF. You can download the latest version of Dompdf from GitHub.
Step 1: Reference the namespace of Dompdf
1 |
namespace Dompdf; |
Step 2: include Dompdf library
1 |
require_once 'dompdf/autoload.inc.php'; |
Step 3: Initiate the Dompdf class
1 |
$dompdf = new Dompdf(); |
Basic Parameters
Load HTML content
1 |
$dompdf->loadHtml('<h1>PHPGURUKUL | Programming Blog </h1>'); |
loadhtml() use to generate PDF and render on browser.
Set the paper size. This is optional.
1 |
$dompdf->setPaper('A4', 'landscape'); |
setPaper()–
$size(string)— ‘legal’,’letter’,’A4′
$orientation(string) — ‘portrait’ or ‘landscape’.
Render to HTML to PDF
1 |
$dompdf->render(); |
For view the PDF
1 |
$dompdf->stream("",array("Attachment" => false)); |
For download the PDF
1 |
$dompdf->stream("sample.pdf"); |