官网URL:http://www.mpdf1.com/mpdf/index.php

github:https://github.com/mpdf/mpdf

官方开发手册,英文的:http://www.mpdfonline.com/repos/mpdfmanual.pdf

网页版 https://mpdf.github.io/

蛋疼,不想翻译600多页,只翻译了部分常用的部分

之前一直使用tcpdf,对于不是很复杂的合同生成还行,但是语法复杂的页面,对table的支持一塌糊涂,div+css支持很有限,而且必须是100%可以转换的闭合xml才行,不行直接报错,

然后花了1天左右尝试市面上基本所有的php关于pdf的插件,基本全军覆没,要么生成pdf过大,要么某个html属性支持不好,要么年久失修,多年未维护,非常蛋疼,最后在github找到mpdf

一看文件包52M,我擦,这么大,下载完成后发现字体占了70-80M,没想的是mpdf生成的pdf只有100多KB,大大降低我磁盘的消耗。而且生成速度还不错,中文兼容,图片兼容也还不错

要求

mPDF 7.0 requires PHP ^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0. PHP mbstring和gd扩展必须加载。

对于某些高级功能,可能需要其他扩展,例如用于压缩输出的zlib和用于生成条形码的bcmath或用于字符集转换和SVG处理的xml等嵌入式资源。

mPDF在使用单线程服务器(如php-s)获取外部HTTP资源时遇到一些问题。推荐使用适当的服务器,如nginx(php-fpm)或Apache。

Support us

Consider supporting development of mPDF with a donation of any value. Donation button can be found on the main page of the documentation.

安装

官方安装方法是通过composer and its packagist package mpdf/mpdf.

$ composer require mpdf/mpdf

使用

库的最简单的用法(从版本7.0开始)如下:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();

这将输出PDF作为应用程序/ PDF内容类型的PDF内联类型。

设置和配置

所有配置指令都可以通过构造函数的$ config参数来设置。

建议通过tempDir配置变量设置自己的临时目录。对于使用mPDF(通常为cli,webserver,fpm)的用户,该目录必须具有写权限(建议使用775模式)。

<?php

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/tmp']);

默认情况下,临时目录将位于vendor目录中,并且将具有来自post_install作曲家脚本的正确权限。

有关自定义临时目录的更多信息,请参阅手册中“安装和设置”一节中有关“临时文件的文件夹”的说明。

laravel的另一种安装方法

在composer.json加上

"autoload": {
"classmap": [
"database",
"app/Libarary/mpdf"//加上
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Tools.php",
"app/WeiXin.php"
]
},

吧mpdf放在目录 app/Libarary/下,我的版本是php5.6,so

在网站根目录运行

composer dumpautoload  

实例demo

注意:mpdf对复杂的table布局,基本可以完全兼容,简单的div+css也可以,但是建议table

use mPDF; //引入

$mpdf = new mPDF('utf-8', 'A4', 16, '', 10, 10, 15, 1);
$mpdf->SetDisplayMode('fullpage');
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;
//$stylesheet = file_get_contents('mpdfstyletables.css');
//$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
$file = 'images/seal/150.png';
// $mpdf->WriteHTML($contract_content, 2);
$mpdf->WriteHTML($html, 2);
$mpdf->Image($file, 140, 200); //追加盖章图片,貌似可以获取远程的,非必要请使用服务器本地的,以前tcpdf就是无法获取远程的,mpdf未测试远程图片
$mpdf->Output('mpdf.pdf', 'I'); //D是下载

下面有pdf实际效果,文件在100-200K,比较小,有盖章图片会大一点,请注意网页定义的像素和pdf显示效果有差异,建议生成之后打印看实际效果

需要注意的是,这个页面是找前端先把页面直接table布局先写出来,在慢慢调整出来,在ueditor上效果基本一样,可以随时修改文件,但是不能自由编辑,因为是table布局,如果不小心把布局删除了,就会变形

虽然不是特别自由,但是可以满足业务人员,自由修改文字,变更合同

html源代码

 <table style="border-collapse: collapse; width: 900px; height: 56px; font-family: 宋体; font-size: 18px; position: relative;">
<tbody>
<tr style="height:75px;text-align:center" class="firstRow">
<td style="font-weight: bold;font-size:40px;text-align:center" colspan="8">
购 销 合 同
</td>
</tr>
<tr style="height:75px;">
<td colspan="8">
&nbsp;
</td>
</tr>
<tr style="height:32px;text-align:left">
<td style="font-weight: normal;" colspan="4">
买方:{buyer_company_name}
</td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal; text-align: right; word-break: break-all;">
合同编号:
</td>
<td style="font-weight: normal;" colspan="2">
{order_key}
</td>
</tr>
<tr style="height:32px">
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;text-align:right">
签订地点:
</td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
</tr>
<tr style="height:32px">
<td style="font-weight: normal;" colspan="4">
卖方:{supply_company_name}
</td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal; text-align: right; word-break: break-all;">
签订时间:
</td>
<td style="font-weight: normal; word-break: break-all;" colspan="2">
{current_time}
</td>
</tr>
<tr style="height:32px">
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
<td style="font-weight: normal;"></td>
</tr>
<tr style="height:75px;">
<td colspan="8">
&nbsp;
</td>
</tr>
<tr style="height:32px;text-align:left">
<td style=" font-weight: normal;" colspan="8">
第一、订单号码、产品规格型号、数量、单价、金额;
</td>
</tr>
<tr style="height:32px;text-align:center;font-size:14px">
<td style="border: 1px solid #ccc;font-weight: normal; width: 13%;text-align:center;font-size:14px;">
货物名称
</td>
<td style="border: 1px solid #ccc;font-weight: normal; width: 16%;text-align:center;font-size:14px;">
品牌
</td>
<td style="border: 1px solid #ccc;font-weight: normal; width: 10%;text-align:center;font-size:14px;">
型号规格
</td>
<td style="border: 1px solid #ccc;font-weight: normal; width: 2%;text-align:center;font-size:14px;">
单位
</td>
<td style="border: 1px solid #ccc;font-weight: normal;width: 9%;text-align:center;font-size:14px;">
数量
</td>
<td style="border: 1px solid #ccc;font-weight: normal;width: 13%;text-align:center;font-size:14px;">
含税单价(元)
</td>
<td style="border: 1px solid #ccc;font-weight: normal;width: 12%;text-align:center;font-size:14px;">
金额(元)
</td>
<td style="border: 1px solid #ccc;font-weight: normal;width: 25%;text-align:center;font-size:14px;">
备注
</td>
</tr>
<tr style="height:32px;text-align:center;font-size:14px;text-align:center">
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{product_name}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{product_brand}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{product_type}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">

</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{product_number}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{order_amount}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{order_amount}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
{buyer_remark}
</td>
</tr>
<tr style="height:32px;text-align:left;font-size:14px">
<td style="border: 1px solid #ccc;font-weight: normal;font-size:14px;" colspan="5">
合计人民币金额(大写):{order_amount_capital}
</td>
<td style="border: 1px solid #ccc;font-weight: normal;font-size:14px;" colspan="3">
RMB {order_amount}
</td>
</tr>
<tr style="height:75px;">
<td colspan="8">
&nbsp;
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family:宋体">第二、交(提)货时间及地点、付款方式: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br/> &nbsp;交(提)货 方式:买方到卖方指定仓库自提,出库费用及货权移交后的仓储费用由买方负责。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br/> &nbsp;交(提)货时间和付款方式: 买方将全部货款于签订合同日16:30之前汇入供方指定账户,卖方收到货款后,买方可到卖方指定仓库提货。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style="font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第三条、 质量、包装标准:按《中华人民共和国国家标准》的规范GB/T 470-2008或按生产厂家提供的质量保证书为准。</span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第四条、合理损耗标准及计算方法:允许合理磅差±2‰,超出合理磅差范围部分由买卖双方协商解决,交货尾差±5%,卖方提供相对应的生产厂出厂磅码单,按磅码单抄码重量结算。 </span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第五条、检验标准、方法、地点及期限:按国家标准验收。如有异议,买方须在收货七天内提出,同时买方应保存异议货品留待卖方或第三方鉴定处理。 </span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第六条、结算方式及期限:款到发货,买方应于提货前以电汇/汇票等方式支付全部货款;卖方开具17%增值税发票。</span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第七条、违约责任:本合同受《中华人民共和国合同法》保护。买卖双方如有一方无法履行合同(不可抗力除外),由此造成的经济损失由违约方承担。遇不可抗力因素,不能履行本合同时,应及时通知对方,双方协商解决。</span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第八条、合同争议的解决方式:本合同在履行过程中发生的争议,由双方当事人协商解决;协商不成任何一方均可向合同签订所在地有管辖权的人民法院提起诉讼。</span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" font-weight: normal;" colspan="8">
<p style="margin:3px">
<span style="font-family: 宋体">第九条、其他约定事项:本合同以传真方式签订,经双方盖章后生效,传真件与原件具有同等法律效力;与本合同有关的货权转移凭据及其传真件同具法律效力;有效期:自买卖双方签订合同之日起,至货款、发票结清之日为止。</span>
</p>
</td>
</tr>
<tr style="height:75px;">
<td colspan="8">
&nbsp;
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; text-align:center; font-weight: normal;" colspan="5">
<p style="text-align:center">
<span style="font-family:宋体">买&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 方</span>
</p>
</td>
<td style=" border: 1px solid #ccc; text-align:center; font-weight: normal;" colspan="3">
<p style="text-align:center">
<span style="font-family:宋体">卖&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 方</span>
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">单位名称(章): </span>{buyer_company_name}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">单位名称(章): </span>{supply_company_name}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">单位地址:</span>{buyer_company_address}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">单位地址:</span>{buyer_company_address}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">法定代表人:</span>{buyer_representative}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">法定代表人:</span>{supply_representative}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">委托代理人:</span>{buyer_representative_commissioned}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">委托代理人:</span>{supply_representative_commissioned}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">电&nbsp;&nbsp;&nbsp; 话:</span>{buyer_mobile}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">电&nbsp;&nbsp;&nbsp; 话:</span>{supply_mobile}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">传&nbsp;&nbsp;&nbsp; 真:</span>{buyer_fax}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">传&nbsp;&nbsp;&nbsp; 真:</span>{supply_fax}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">开户银行:</span>{buyer_opening_bank}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">开户银行:</span>{supply_opening_bank}
</p>
</td>
</tr>
<tr style="text-align:left">
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="5">
<p style="margin:3px">
<span style="font-family:宋体">帐&nbsp;&nbsp;&nbsp; 号:</span>{buyer_bank_account}
</p>
</td>
<td style=" border: 1px solid #ccc; font-weight: normal;" colspan="3">
<p style="margin:3px">
<span style="font-family:宋体">帐&nbsp;&nbsp;&nbsp; 号:</span>{supply_bank_account}
</p>
</td>
</tr>
</tbody>
</table>

解决php7兼容问题

Deprecated: Methods with the same name as their class will not be constructor

原因是php7不在支持同名构造函数了。

解决问题的办法比较简单,吧同名构造函数  __construct就可以

demo中报错的地方需要修改四个地方,问题都是一样的

mpdf/mpdf.php  

mpdf/classes/grad

mpdf/classes/mpdfform

mpdf/classes/cssmgr

问题就解决了,如果还有类似,找到对应文件就可以

$mpdf = new mPDF('utf-8', 'A4', 16, '', 10, 10, 15, 1);
die;

看具体报什么错误

函数使用和说明

注意: 在目录下面有个examples,有很多例子可以使用

不明白为什么函数没写变量注释

$mpdf = new mPDF($mode='',$format='A4',$default_font_size=0,$default_font='',$mgl=15,$mgr=15,$mgt=16,$mgb=16,$mgh=9,$mgf=9, $orientation='P');

mpdf中文开发使用文档附demo实例的更多相关文章

  1. JS文档和Demo自动化生成工具 - SmartDoc发布

    曾几何时,当你码神附体,一路披荆斩棘的完成代码后,带着“一码在手,天下我有”的傲然环顾之时,却发现单元测试.API文档.Demo实例陆续向你砸来,顿时有木有一种冰水挑战后的感觉.而这时你应该:哟哟,快 ...

  2. (转载)中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  3. 中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  4. WEB前端开发规范文档(转)

    http://codeguide.bootcss.com/  编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范上面的文档 再结合下面的规范: 无论是从技术角度还是开发视角,对于web前端开发 ...

  5. 网站开发进阶(三十八)Web前端开发规范文档你需要知道的事

    Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...

  6. Web前端开发规范文档你需要知道的事

    Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...

  7. jcseg-1.8.7版本发布 - 多配置适应+完整开发帮助文档

    jcseg是使用java开发的一款开源中文分词器, 并且提供了最新版本的lucene和solr分词接口. jcseg 1.8.7版本发布了: 1. 更改了内部设计, 加入JcsegTaskConfig ...

  8. WEB前端开发规范文档[转]

    为新项目写的一份规范文档, 分享给大家. 我想前端开发过程中, 无论是团队开发, 还是单兵做站, 有一份开发文档做规范, 对开发工作都是很有益的. 本文档由本人编写, 部分意见来源于网络, 以此感谢, ...

  9. Enterprise Solution 2.2 开发帮助文档集合

    首先是一个PPT文档,从宏观层面展示Enterprise Soltion的几个功能特色. Enterprise Solution解决方案安装与配置 将源代码解决方案和演示程序在电脑中进行配置,作为了解 ...

随机推荐

  1. python接口自动化测试(六)-unittest-单个用例管理

    前面五节主要介绍了环境搭建和requests库的使用,可以使用这些进行接口请求的发送.但是如何管理接口案例?返回结果如何自动校验?这些内容光靠上面五节是不行的,因此从本节开始我们引入python单元测 ...

  2. TensorFlow实战Google深度学习框架5-7章学习笔记

    目录 第5章 MNIST数字识别问题 第6章 图像识别与卷积神经网络 第7章 图像数据处理 第5章 MNIST数字识别问题 MNIST是一个非常有名的手写体数字识别数据集,在很多资料中,这个数据集都会 ...

  3. Spring HttpInvoker 从实战到源码追溯

    Spring HttpInvoker 作为 Spring 家族中老牌远程调用模型,深受开发者喜爱. 其主要目的是来执行基于 HTTP 的远程调用(轻松穿越防火墙),并使用标准的 JDK 序列化机制. ...

  4. intellij 自动导包

  5. java生成32的md5签名串

    import java.security.MessageDigest; import lombok.extern.slf4j.Slf4j; /** * 签名帮助类 * * @author yangzl ...

  6. C# 反射获取控件

    Control control = Controls.Find(]; //object o = control.GetType().GetProperty("PropertyName&quo ...

  7. YAML文件格式_k8s/docker-compose配置文件

    YAML(Yet Another Markup Language),是一个JSON的超集,意味着任何有效JSON文件也都是一个YAML文件.它规则如下: )大小写敏感 )使用缩进表示层级关系,但不支持 ...

  8. hdoj:2056

    #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; str ...

  9. MySQL 下载地址获取

    http://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.26-winx64.msi 首先安装 mysql installer 安装器进行安装,之前会让你选 ...

  10. 内建模块 datetime使用

    #-*- coding:utf-8 -*- from datetime import datetime #获取当前日期和时间 now =datetime.now() print now # 2017- ...