zoukankan      html  css  js  c++  java
  • mpdf中文开发使用文档附demo实例

    官网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 MpdfMpdf();
    $mpdf->WriteHTML('<h1>Hello world!</h1>');
    $mpdf->Output();


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

    设置和配置

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

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

    <?php

    $mpdf = new MpdfMpdf(['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源代码

      1 <table style="border-collapse: collapse;  900px; height: 56px; font-family: 宋体; font-size: 18px; position: relative;">
      2     <tbody>
      3         <tr style="height:75px;text-align:center" class="firstRow">
      4             <td style="font-weight: bold;font-size:40px;text-align:center" colspan="8">
      5                 购 销 合 同
      6             </td>
      7         </tr>
      8         <tr style="height:75px;">
      9             <td colspan="8">
     10                 &nbsp;
     11             </td>
     12         </tr>
     13         <tr style="height:32px;text-align:left">
     14             <td style="font-weight: normal;" colspan="4">
     15                 买方:{buyer_company_name}
     16             </td>
     17             <td style="font-weight: normal;"></td>
     18             <td style="font-weight: normal; text-align: right; word-break: break-all;">
     19                 合同编号:
     20             </td>
     21             <td style="font-weight: normal;" colspan="2">
     22                 {order_key}
     23             </td>
     24         </tr>
     25         <tr style="height:32px">
     26             <td style="font-weight: normal;"></td>
     27             <td style="font-weight: normal;"></td>
     28             <td style="font-weight: normal;"></td>
     29             <td style="font-weight: normal;"></td>
     30             <td style="font-weight: normal;"></td>
     31             <td style="font-weight: normal;text-align:right">
     32                 签订地点:
     33             </td>
     34             <td style="font-weight: normal;"></td>
     35             <td style="font-weight: normal;"></td>
     36         </tr>
     37         <tr style="height:32px">
     38             <td style="font-weight: normal;" colspan="4">
     39                 卖方:{supply_company_name}
     40             </td>
     41             <td style="font-weight: normal;"></td>
     42             <td style="font-weight: normal; text-align: right; word-break: break-all;">
     43                 签订时间:
     44             </td>
     45             <td style="font-weight: normal; word-break: break-all;" colspan="2">
     46                 {current_time}
     47             </td>
     48         </tr>
     49         <tr style="height:32px">
     50             <td style="font-weight: normal;"></td>
     51             <td style="font-weight: normal;"></td>
     52             <td style="font-weight: normal;"></td>
     53             <td style="font-weight: normal;"></td>
     54             <td style="font-weight: normal;"></td>
     55             <td style="font-weight: normal;"></td>
     56             <td style="font-weight: normal;"></td>
     57             <td style="font-weight: normal;"></td>
     58         </tr>
     59         <tr style="height:75px;">
     60             <td colspan="8">
     61                 &nbsp;
     62             </td>
     63         </tr>
     64         <tr style="height:32px;text-align:left">
     65             <td style="    font-weight: normal;" colspan="8">
     66                 第一、订单号码、产品规格型号、数量、单价、金额;
     67             </td>
     68         </tr>
     69         <tr style="height:32px;text-align:center;font-size:14px">
     70             <td style="border: 1px solid #ccc;font-weight: normal;  13%;text-align:center;font-size:14px;">
     71                 货物名称
     72             </td>
     73             <td style="border: 1px solid #ccc;font-weight: normal;  16%;text-align:center;font-size:14px;">
     74                 品牌
     75             </td>
     76             <td style="border: 1px solid #ccc;font-weight: normal;  10%;text-align:center;font-size:14px;">
     77                 型号规格
     78             </td>
     79             <td style="border: 1px solid #ccc;font-weight: normal;  2%;text-align:center;font-size:14px;">
     80                 单位
     81             </td>
     82             <td style="border: 1px solid #ccc;font-weight: normal; 9%;text-align:center;font-size:14px;">
     83                 数量
     84             </td>
     85             <td style="border: 1px solid #ccc;font-weight: normal; 13%;text-align:center;font-size:14px;">
     86                 含税单价(元)
     87             </td>
     88             <td style="border: 1px solid #ccc;font-weight: normal; 12%;text-align:center;font-size:14px;">
     89                 金额(元)
     90             </td>
     91             <td style="border: 1px solid #ccc;font-weight: normal; 25%;text-align:center;font-size:14px;">
     92                 备注
     93             </td>
     94         </tr>
     95         <tr style="height:32px;text-align:center;font-size:14px;text-align:center">
     96             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
     97                 {product_name}
     98             </td>
     99             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    100                 {product_brand}
    101             </td>
    102             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    103                 {product_type}
    104             </td>
    105             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    106 107             </td>
    108             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    109                 {product_number}
    110             </td>
    111             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    112                 {order_amount}
    113             </td>
    114             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    115                 {order_amount}
    116             </td>
    117             <td style="border: 1px solid #ccc;font-weight: normal;text-align:center;font-size:14px;">
    118                 {buyer_remark}
    119             </td>
    120         </tr>
    121         <tr style="height:32px;text-align:left;font-size:14px">
    122             <td style="border: 1px solid #ccc;font-weight: normal;font-size:14px;" colspan="5">
    123                 合计人民币金额(大写):{order_amount_capital}
    124             </td>
    125             <td style="border: 1px solid #ccc;font-weight: normal;font-size:14px;" colspan="3">
    126                 RMB {order_amount}
    127             </td>
    128         </tr>
    129         <tr style="height:75px;">
    130             <td colspan="8">
    131                 &nbsp;
    132             </td>
    133         </tr>
    134         <tr style="text-align:left">
    135             <td style="   font-weight: normal;" colspan="8">
    136                 <p style="margin:3px">
    137                     <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>
    138                 </p>
    139             </td>
    140         </tr>
    141         <tr style="text-align:left">
    142             <td style="font-weight: normal;" colspan="8">
    143                 <p style="margin:3px">
    144                     <span style="font-family: 宋体">第三条、 质量、包装标准:按《中华人民共和国国家标准》的规范GB/T 470-2008或按生产厂家提供的质量保证书为准。</span>
    145                 </p>
    146             </td>
    147         </tr>
    148         <tr style="text-align:left">
    149             <td style="  font-weight: normal;" colspan="8">
    150                 <p style="margin:3px">
    151                     <span style="font-family: 宋体">第四条、合理损耗标准及计算方法:允许合理磅差±2‰,超出合理磅差范围部分由买卖双方协商解决,交货尾差±5%,卖方提供相对应的生产厂出厂磅码单,按磅码单抄码重量结算。 </span>
    152                 </p>
    153             </td>
    154         </tr>
    155         <tr style="text-align:left">
    156             <td style="  font-weight: normal;" colspan="8">
    157                 <p style="margin:3px">
    158                     <span style="font-family: 宋体">第五条、检验标准、方法、地点及期限:按国家标准验收。如有异议,买方须在收货七天内提出,同时买方应保存异议货品留待卖方或第三方鉴定处理。 </span>
    159                 </p>
    160             </td>
    161         </tr>
    162         <tr style="text-align:left">
    163             <td style="   font-weight: normal;" colspan="8">
    164                 <p style="margin:3px">
    165                     <span style="font-family: 宋体">第六条、结算方式及期限:款到发货,买方应于提货前以电汇/汇票等方式支付全部货款;卖方开具17%增值税发票。</span>
    166                 </p>
    167             </td>
    168         </tr>
    169         <tr style="text-align:left">
    170             <td style="  font-weight: normal;" colspan="8">
    171                 <p style="margin:3px">
    172                     <span style="font-family: 宋体">第七条、违约责任:本合同受《中华人民共和国合同法》保护。买卖双方如有一方无法履行合同(不可抗力除外),由此造成的经济损失由违约方承担。遇不可抗力因素,不能履行本合同时,应及时通知对方,双方协商解决。</span>
    173                 </p>
    174             </td>
    175         </tr>
    176         <tr style="text-align:left">
    177             <td style=" font-weight: normal;" colspan="8">
    178                 <p style="margin:3px">
    179                     <span style="font-family: 宋体">第八条、合同争议的解决方式:本合同在履行过程中发生的争议,由双方当事人协商解决;协商不成任何一方均可向合同签订所在地有管辖权的人民法院提起诉讼。</span>
    180                 </p>
    181             </td>
    182         </tr>
    183         <tr style="text-align:left">
    184             <td style=" font-weight: normal;" colspan="8">
    185                 <p style="margin:3px">
    186                     <span style="font-family: 宋体">第九条、其他约定事项:本合同以传真方式签订,经双方盖章后生效,传真件与原件具有同等法律效力;与本合同有关的货权转移凭据及其传真件同具法律效力;有效期:自买卖双方签订合同之日起,至货款、发票结清之日为止。</span>
    187                 </p>
    188             </td>
    189         </tr>
    190         <tr style="height:75px;">
    191             <td colspan="8">
    192                 &nbsp;
    193             </td>
    194         </tr>
    195         <tr style="text-align:left">
    196             <td style=" border: 1px solid #ccc; text-align:center;  font-weight: normal;" colspan="5">
    197                 <p style="text-align:center">
    198                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
    199                 </p>
    200             </td>
    201             <td style=" border: 1px solid #ccc; text-align:center;  font-weight: normal;" colspan="3">
    202                 <p style="text-align:center">
    203                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
    204                 </p>
    205             </td>
    206         </tr>
    207         <tr style="text-align:left">
    208             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    209                 <p style="margin:3px">
    210                     <span style="font-family:宋体">单位名称(章): </span>{buyer_company_name}
    211                 </p>
    212             </td>
    213             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="3">
    214                 <p style="margin:3px">
    215                     <span style="font-family:宋体">单位名称(章): </span>{supply_company_name}
    216                 </p>
    217             </td>
    218         </tr>
    219         <tr style="text-align:left">
    220             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    221                 <p style="margin:3px">
    222                     <span style="font-family:宋体">单位地址:</span>{buyer_company_address}
    223                 </p>
    224             </td>
    225             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="3">
    226                 <p style="margin:3px">
    227                     <span style="font-family:宋体">单位地址:</span>{buyer_company_address}
    228                 </p>
    229             </td>
    230         </tr>
    231         <tr style="text-align:left">
    232             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    233                 <p style="margin:3px">
    234                     <span style="font-family:宋体">法定代表人:</span>{buyer_representative}
    235                 </p>
    236             </td>
    237             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="3">
    238                 <p style="margin:3px">
    239                     <span style="font-family:宋体">法定代表人:</span>{supply_representative}
    240                 </p>
    241             </td>
    242         </tr>
    243         <tr style="text-align:left">
    244             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    245                 <p style="margin:3px">
    246                     <span style="font-family:宋体">委托代理人:</span>{buyer_representative_commissioned}
    247                 </p>
    248             </td>
    249             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="3">
    250                 <p style="margin:3px">
    251                     <span style="font-family:宋体">委托代理人:</span>{supply_representative_commissioned}
    252                 </p>
    253             </td>
    254         </tr>
    255         <tr style="text-align:left">
    256             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="5">
    257                 <p style="margin:3px">
    258                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp; 话:</span>{buyer_mobile}
    259                 </p>
    260             </td>
    261             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="3">
    262                 <p style="margin:3px">
    263                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp; 话:</span>{supply_mobile}
    264                 </p>
    265             </td>
    266         </tr>
    267         <tr style="text-align:left">
    268             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    269                 <p style="margin:3px">
    270                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp; 真:</span>{buyer_fax}
    271                 </p>
    272             </td>
    273             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="3">
    274                 <p style="margin:3px">
    275                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp; 真:</span>{supply_fax}
    276                 </p>
    277             </td>
    278         </tr>
    279         <tr style="text-align:left">
    280             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    281                 <p style="margin:3px">
    282                     <span style="font-family:宋体">开户银行:</span>{buyer_opening_bank}
    283                 </p>
    284             </td>
    285             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="3">
    286                 <p style="margin:3px">
    287                     <span style="font-family:宋体">开户银行:</span>{supply_opening_bank}
    288                 </p>
    289             </td>
    290         </tr>
    291         <tr style="text-align:left">
    292             <td style=" border: 1px solid #ccc;   font-weight: normal;" colspan="5">
    293                 <p style="margin:3px">
    294                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp; 号:</span>{buyer_bank_account}
    295                 </p>
    296             </td>
    297             <td style="  border: 1px solid #ccc;  font-weight: normal;" colspan="3">
    298                 <p style="margin:3px">
    299                     <span style="font-family:宋体">&nbsp;&nbsp;&nbsp; 号:</span>{supply_bank_account}
    300                 </p>
    301             </td>
    302         </tr>
    303     </tbody>
    304 </table>
    View Code

    解决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');
  • 相关阅读:
    用SQL实现的一个自动排课机制
    如何读懂复杂的C声明
    Mingw32配置
    test
    HttpSession API
    java程序逻辑控制
    方法的定义及使用
    memcached 安装及集成(转)
    cookie和session的的区别以及使用示例?
    构造方法和普通方法的区别?
  • 原文地址:https://www.cnblogs.com/zx-admin/p/8352003.html
Copyright © 2011-2022 走看看