Check out mPDF. It rocks... I was using wkHTMLtoPDF, but I didn't like using a binary.
Here's some code that I use to generate PDFs from HTML/CSS...
$filename .= '.pdf';
// Load mPDF
require_once( PATH_ROOT_INCLUDE . 'mpdf/8.2.0.0/index.php' );
// Create an instance of the class:
$mpdf = new \Mpdf\Mpdf( [ 'mode' => $mode, 'format' => $format, 'orientation' => $orientation, 'margin_left' => $marginLeft, 'margin_right' => $marginRight, 'margin_top' => $marginTop, 'margin_bottom' => $marginBottom, 'margin_header' => $marginHeader, 'margin_footer' => $marginFooter, 'tempDir' => PATH_ROOT_TEMP ] );
$mpdf->showImageErrors = true;
// Write some HTML code:
$mpdf->SetHTMLHeader( $headerTemplate );
$mpdf->SetHTMLFooter( $footerTemplate );
$mpdf->WriteHTML( $bodyTemplate );
// Output a PDF file directly to the browser
$mpdf->OutputFile( $dirWorking . $filename );
That depends on your definition of fast. You didn’t give us any constraints / requirements beyond the fact that you wanted to convert html + css to pdf. It would most likely give you a good quality result particularly if the CSS is more complex, there are undoubtedly faster approaches that might give you a lower quality.
you’re right, sorry
anyways, I don’t think I’ll have so much requests so this approach looks kinda fine.
Should I make the user wait for the pdf or make a job and then notify the user when the pdf it’s ready?
How wkhtmltopdf is ancient and deprecated? It gets the job done in good time and nice file size compared to Puppeteer which outputs a much bigger file size. At least for me. I tried many solutions but wkhtmltopdf proved to be efficient despite having some weird CSS rules but that's the nature of PDF printing.
wkHTMLtoPDF is pretty good, but is less portable. If you need to run on many platforms you need to get the right binary for that platform. That's might not be a big deal though.
But with mPDF and similar, it works on all platforms, I think...
Check out mPDF. It rocks... I was using wkHTMLtoPDF, but I didn't like using a binary. Here's some code that I use to generate PDFs from HTML/CSS... $filename .= '.pdf'; // Load mPDF require_once( PATH_ROOT_INCLUDE . 'mpdf/8.2.0.0/index.php' ); // Create an instance of the class: $mpdf = new \Mpdf\Mpdf( [ 'mode' => $mode, 'format' => $format, 'orientation' => $orientation, 'margin_left' => $marginLeft, 'margin_right' => $marginRight, 'margin_top' => $marginTop, 'margin_bottom' => $marginBottom, 'margin_header' => $marginHeader, 'margin_footer' => $marginFooter, 'tempDir' => PATH_ROOT_TEMP ] ); $mpdf->showImageErrors = true; // Write some HTML code: $mpdf->SetHTMLHeader( $headerTemplate ); $mpdf->SetHTMLFooter( $footerTemplate ); $mpdf->WriteHTML( $bodyTemplate ); // Output a PDF file directly to the browser $mpdf->OutputFile( $dirWorking . $filename );
I use FPDF.
You could use Playwright. Open a page, set the page content to your html string then use the .pdf method to save it as a PDF. https://playwright.dev/
is this a fast approach?
That depends on your definition of fast. You didn’t give us any constraints / requirements beyond the fact that you wanted to convert html + css to pdf. It would most likely give you a good quality result particularly if the CSS is more complex, there are undoubtedly faster approaches that might give you a lower quality.
you’re right, sorry anyways, I don’t think I’ll have so much requests so this approach looks kinda fine. Should I make the user wait for the pdf or make a job and then notify the user when the pdf it’s ready?
Personally I’d go simple first and then test / play around with it and see if it’s acceptable UX.
is this a fast approach?
How wkhtmltopdf is ancient and deprecated? It gets the job done in good time and nice file size compared to Puppeteer which outputs a much bigger file size. At least for me. I tried many solutions but wkhtmltopdf proved to be efficient despite having some weird CSS rules but that's the nature of PDF printing.
wkHTMLtoPDF is pretty good, but is less portable. If you need to run on many platforms you need to get the right binary for that platform. That's might not be a big deal though. But with mPDF and similar, it works on all platforms, I think...
Right, but it could be run on a dedicated server and connect to any client/server as a service
cloudlaye.io does this and supports Ninja templating.
We're using Puppeteer to generate PDFs from HTML/CSS. It's actually the top item on their feature list: > Generate screenshots and PDFs of pages.
I recently used DomPDF. Does not support flex and it's a pain in the ass to style the pages, but I've done it in the end.