简体   繁体   中英

What is the fastest way to convert html to pdf using JS or PHP?

I have a long table for about ~100.000 rows which is generated dynamically (technically it can be divs). It's needed to export this html data into pdf.

I have tried to use html2pdf.js . But when it is over 5000 rows it begins to work very slow and exported pdf has huge size. I think it is due to saving html as image and then inserting it into document.

Have tried html2pdf.php Also works slow from over 500 rows already has troubles, but document has very good size.

Have tried html-pdf with node js. Works much faster then previous 2, but there is one limitation. Node js script is on server and when there more then one client, performance is distributed between all client and performance decreases dramatically.

I am in search of library which will convert very long html table into pdf document for acceptable amount of time.

My needs can be described as:

1 ) The script can run in browser (Javascript) an should be fast enough in order not to make user wait for ages.

2 ) The script can run on server using PHP/Node js, but here should be considered that at the same time several users can request pdf.

3 ) May be there are some other solutions for examples on Java or Python which will run on Linux server that could be easily triggered from browser?

Options 1 or 2 are the most desirable.

Thanks!

One of the comments (which is deleted for some reason) suggested me to try AutoTable - Table plugin for jsPDF . Thank you! This plugin for js library html2pdf does an amazing job! Works with the speed of light.

I'd recommend DomPDF if you are using PHP because it's simplicity. Here is an example based on their quickstart example :

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world'); //complete page DOM goes here

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

You can find more information on how to install and other examples in their repository: https://github.com/dompdf/dompdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM