PHP implements HTML to generate PDF file program_PHP tutorial

WBOY
Release: 2016-07-13 10:48:09
Original
766 people have browsed it

This article will introduce to you how to use HTML2FPDF and wkhtmltoimage to directly generate pdf files from web page html in Linux. I hope this method will be helpful to you.

Finally found an open source program developed based on FPDF and HTML2FPDF source code. The author is very awesome. It basically solves the problem of garbled characters in Chinese (as well as Japanese, Korean, Southeast Asian and global languages). It can be tested in Windows/Linux development environment and does not require the installation of other component support. It is good news for website developers who do not have VPS and independent servers.

Without further ado, the source code name is MPDF, and the official address is: http://www.mpdf1.com/ It has been updated to version 5.6.

Go to the official website to download, unzip it to the website directory, and use it.

$mpdf->Output('ss.pdf'); //$mpdf->Output('tmp.pdf',true);
The code is as follows
 代码如下 复制代码

include('mpdf.php');
$mpdf=new mPDF('UTF-8','A4','','',15,15,44,15);
$mpdf->useAdobeCJK = true;
$mpdf->SetAutoFont(AUTOFONT_ALL);
$mpdf->SetDisplayMode('fullpage');
//$mpdf->watermark_font = 'GB';
//$mpdf->SetWatermarkText('中国水印',0.1);
$url = 'http://www.你的域名.com/';
$strContent = file_get_contents($url);
//print_r($strContent);die;
$mpdf->showWatermarkText = true;
$mpdf->SetAutoFont();
//$mpdf->SetHTMLHeader( '头部' );
//$mpdf->SetHTMLFooter( '底部' );
$mpdf->WriteHTML($strContent);
$mpdf->Output('ss.pdf');
//$mpdf->Output('tmp.pdf',true);
//$mpdf->Output('tmp.pdf','d');
//$mpdf->Output();
exit;
?>

Copy code

include('mpdf.php');

$mpdf=new mPDF('UTF-8','A4','','',15,15,44,15);

$mpdf->useAdobeCJK = true;

$mpdf->SetAutoFont(AUTOFONT_ALL);
$mpdf->SetDisplayMode('fullpage');
//$mpdf->watermark_font = 'GB';
//$mpdf->SetWatermarkText('China Watermark',0.1);
$url = 'http://www.yourdomain.com/';
$strContent = file_get_contents($url);
//print_r($strContent);die;
$mpdf->showWatermarkText = true;
$mpdf->SetAutoFont();
//$mpdf->SetHTMLHeader( 'Header' );
//$mpdf->SetHTMLFooter( 'Bottom' );

$mpdf->WriteHTML($strContent);
//$mpdf->Output('tmp.pdf','d'); //$mpdf->Output();

exit;

?> PASS: It should be noted that the PHP file must be UTF-8. Don’t use the code posted by Mood Sky. If the squares are garbled when you write it yourself, you can change it to true in config.php $this->useAdobeCJK = false; or you must mark: $mpdf->useAdobeCJK = true; there is official documentation , you can explore it yourself. With this imperfect method, MPDF can only parse relatively simple css (it cannot parse tags such as ul li in js and css). Now that we have used the wkhtmltoimage extension, we don't care about the use of wkhtmltopdf. Similarly, wkhtmltoimage 0.11 will cause errors and you need to download version 0.10.0_rc2. 32-bit wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2
tar jxf wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2 cp wkhtmltoimage-i386 /usr/local/bin/wkhtmltopdf
64-bit
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2 mv wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2 wkhtmltoimage-0.10.0_rc2-static-amd64.tar tar -xvf wkhtmltopdf-0.10.0_rc2-static-amd64.tar mv wkhtmltoimage-amd64 /usr/bin/wkhtmltopdf test wkhtmltopdf http://www.yourdomain.com/ yourdomain.pdfphp shell_exec() execute shell_exec('/usr/local/bin/wkhtmltopdf http://www. Your domain name.com/ /usr/local/wwwroot/Your domain name.com/Your domain name.pdf'); Want to save your favorite articles? Link or create a Wumi account now? No more prompts! http://www.bkjia.com/PHPjc/632805.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632805.htmlTechArticleThis article will introduce to you how to use HTML2FPDF and wkhtmltoimage to directly generate pdf files from web page html in Linux. Hope this method is helpful to everyone. Finally found one in FP...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!