PHP实现HTML生成PDF文件的方法,
本文实例讲述了在linux中利用HTML2FPDF与wkhtmltoimage把网页html直接生成pdf格式的文件方法,分享给大家供大家参考。具体实现方法如下:
找到一款在FPDF和HTML2FPDF源码基础上开发的一套开源程序,作者很给力。基本解决了中文(以及日语、韩语及东南亚和全球语言)乱码的问题,在Windows/Linux开发环境下测试可用,不需要安装别的组件支持,是没有VPS和独立服务器的网站开发者的福音。
不多说,源码名称是MPDF,官方地址是:http://www.mpdf1.com/ 已经更新到5.6版。
到官网下载,解压到网站目录下,使用。
复制代码 代码如下:
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;
?>
PASS:需要注意的是,PHP文件要是UTF-8。不用心情天空贴出的代码,自己写时出现方格乱码时,可以在config.php文件里的$this->useAdobeCJK = false;修改成true或者必须标注:$mpdf->useAdobeCJK = true; 官方有文档,可以自己摸索。
这个不完美方法,MPDF只能解析相对简单的css(不能解析js和css中ul li等标签)。现在既然使用了wkhtmltoimage扩展,也就不在乎wkhtmltopdf的使用了。
同样,wkhtmltoimage 0.11会出现错误,需要下载0.10.0_rc2版。
32位:
复制代码 代码如下:
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位
复制代码 代码如下:
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测试 wkhtmltopdf http://www.你的域名.com/ 你的域名.pdfphp shell_exec() 执行shell_exec('/usr/local/bin/wkhtmltopdf http://www.你的域名.com/ /usr/local/wwwroot/你的域名.com/你的域名.pdf');
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function