Sometimes, we want to add HTML And CSS into PDF with PHP.
In this article, we’ll look at how to add HTML And CSS into PDF with PHP.
How to add HTML And CSS into PDF with PHP?
To add HTML And CSS into PDF with PHP, we can use mPDF.
To install it, we run
composer require mpdf/mpdf
Then we use it by writing
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();
We create an Mpdf
object and call WriteHTML
on it to add the HTML we want as a string.
We can add CSS within style tags and style attributes of elements.
Conclusion
To add HTML And CSS into PDF with PHP, we can use mPDF.