What are the advantages of generating a PDF from HTML using tools like dompdf?
Generating a PDF from HTML using tools like dompdf allows for easy conversion of web content into a printable format. This can be useful for creating reports, invoices, or other documents that need to be shared or printed. dompdf handles the conversion process, ensuring that the layout and styling of the HTML content are preserved in the resulting PDF.
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use Dompdf\Options;
// Create a new Dompdf instance
$dompdf = new Dompdf();
// Load HTML content from a file or string
$html = '<h1>Hello, world!</h1>';
$dompdf->loadHtml($html);
// Set paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML content to PDF
$dompdf->render();
// Output the generated PDF
$dompdf->stream('document.pdf', array('Attachment' => 0));