PHP output PDF printing HTML5 + CSS3 printing format control

Source: Internet
Author: User
Tags print format wkhtmltopdf
Looking for a good way to print is an urgent need from all walks of life. after several twists and turns, the author outputs PDF files and PHP captures the HTML code of the target, and then outputs the HTML code to the specified parser, then generate the final print format... looking for a good way to print is an urgent need from all walks of life. after several twists and turns, the author outputs PDF files and PHP captures the HTML code of the target, and then outputs the HTML code to the specified parser, the ultimate choice is to generate the final print format.
The ERP system is coming to an end, and a series of headers, such as delivery orders, return orders, contracts, and statements, are arranged for printing. HTML5 + CSS3 has become so popular. However, at the moment, the printing control based on CSS3 is still in the original tribe stage. Chrome, Firefox, Edge, and other simple @ pages are not supported completely. After some hard work, I finally found a good solution, that is, to output PDF, PHP captures the HTML code of the target, then output it to the specified parser, and then generate the final print format.

In fact, in this mode:

HTML5 + CSS3, only the adjustment of the output content, must ensure that the full feature support of CSS3 @ page is correct. In particular, the long article (contract) page, and long table automatic page (preferably thead and tfoot can be repeatedly displayed on pages ).
When previewing a PDF file, you can preview the layout before printing, and then directly print the output.
PDF files can be downloaded and backed up by users.
HTML content (PDF content) can be cached through the cache class library. you can adjust the cycle and compress the storage.
The problem was solved.

We recommend two available class libraries: Prince and wkhtmltopdf. Both are supported by cross-platform systems and are not embedded in php. Both products must be downloaded and installed on the official website. for windows, remember not to install them on the C drive. Otherwise, the installation will fail.

Php call:

Prince: download his official class library Prince for php. By the way, the Prince class library supports. net, Java, Python, Prel, and so on.

Snappy: this is a php composer extension developed by a foreign developer based on wkhtmltopdf. you can directly execute composer require knplabs/knp-snappy in your project. Snappy github address.

Prince and wkhtmltopdf are actually an html5 + css3 parser and fully support the printing feature of css3 @ page. And has the ability to generate PDF files. Among them, Prince is charged, but it provides free use of License. the disadvantage of the United States is that when a PDF preview is generated, there will be a P icon in the upper right corner of the first page, however, it disappears during printing.

Comparing the two, Prince provides better support for Chinese styles (maybe wkhtmltopdf is because my CSS style is incorrect and it is not targeted at @ media print) and modifies the Chinese font, wkhtmltopdf still uses the default output, while Prince is the font you specified.

The call code for both is simple.

Prince part:

 convert_file_to_passthru('http://www.163.com');

Snappy part:

 getOutput('http://www.163.com');

It's so cool that you don't have to worry about changing the print format any more.

No picture, no truth.

You can use HTML + CSS to print the file. the file is basically in the last format and can be completed in at most one hour. In this way, how many reports do you have? I use http: // erp/print/Register type/report id.ext's url,.html?request common html output; response is how fast the request outputs html pdf!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.