PHP output PDF Print HTML5+CSS3 print format control

Source: Internet
Author: User
Tags print format wkhtmltopdf
Looking for a good printing method is an urgent need of all walks of life, after several toss, channelled through output pdf,php fetch the target HTML code, and then output to the specified parser, and then generate the final printing format, become the ultimate choice for people.
ERP system came to an end, a variety of delivery orders, return orders, contracts, statements and so on a series of things lined up to print the format of the team. HTML5+CSS3 has been so popular, but at present, based on CSS3 printing control, incredibly still in the original tribal stage, Chrome, Firefox, Edge and so on even simple @page support is not perfect. After a toss, finally found a good solution, that is, channelled through output pdf,php fetch the target HTML code, and then output to the specified parser, and then generate the final printing format.

In fact, in this mode:

HTML5+CSS3, just the adjustment of the output content, you need to ensure that the full feature of the CSS3 @page support is correct. In particular long articles (contracts) are paginated, and long tables are automatically paginated (preferably thead and tfoot can be paged repeatedly).
When you preview a PDF, it's actually a pre-print preview, and then you print it out directly.
PDF allows users to download cost files for backup.
HTML content (PDF content), can be cached through the Cache class library, self-regulation cycle, compressed storage.
So the printing problem solved.

This article recommends two available class libraries: Prince and Wkhtmltopdf. These two bits are cross-platform system support, and do not need to be embedded in PHP, there is no need to worry about. Both goods need to download the installation on the official website, if it is windows, remember to install the time do not put on the C drive, otherwise it will not be executed.

PHP Calls:

Prince: Download His official library of this kind: Prince for PHP. Incidentally, Prince's class library supports. NET, Java, Python, Prel, and so on.

Snappy: This is a foreign developer based on wkhtmltopdf PHP composer extension, directly in your project execution: Composer require knplabs/knp-snappy, you can. Snappy GitHub address.

Prince and Wkhtmltopdf, in fact, are a HTML5+CSS3 parser and a feature that fully supports the printing features of the CSS3 @page section. and has the ability to generate PDFs. Where Prince is charged, but he provides free use of license, in the ointment is to generate a PDF preview, the first page in the upper right corner will have a P-word icon, but will disappear when printing.

In comparison, Prince support for Chinese style is better (perhaps wkhtmltopdf because I have a problem with my CSS, not for @media print), to modify the Chinese font, wkhtmltopdf or will use the default song output, And Prince is the font you specify.

Both of the calling codes are simple.

Prince section:

<?php require '.  /bootstrap.php ';  $prince = new \xerp\helper\prince (' D:\\prince\\prince-10r7-win64\\bin\\prince.exe ');  Header (' content-type:application/pdf ');  Header (' Content-disposition:inline; filename= ' foo.pdf '); $prince->convert_file_to_passthru (' http://www.163.com ');

Snappy section:

<?php require '.  /bootstrap.php ';  Use knp\snappy\pdf;  $snappy = new Pdf (' D:\xampp\wkhtmltopdf\bin\\wkhtmltopdf.exe ');  Header (' content-type:application/pdf ');  Header (' content-disposition:attachment; filename= ' file.pdf '); Print $snappy->getoutput (' http://www.163.com ');

is so sour, no longer afraid of customers to change the format of printing, casually change it, pro.

No picture no truth, come on a good.

Like this page, is actually based on HTML generation, debugging only need to debug Html+css, as follows:

With Html+css to print, basically a format, up to 1 hours, basically can be tuned out. So late, tube you have how many sub-statement, I unified with http://erp/print/registration type/Report Id.ext url,.html is to request the normal HTML output,. PDF is the request HTML output PDF, really how fast how fast!

  • 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.