tp6 使用Mpdf进行导出功能实现,兼容中文和复杂的css

本文介绍了一种使用PHP和MPDF库自动生成购销合同PDF的方法。通过编写控制器代码,可以将HTML模板转换为PDF文件,便于下载和打印。

类库下载

composer require mpdf/mpdf

3、控制器代码

use Mpdf\Mpdf;

public function createPdf()
{
	//为了防止文件下载的时候出现跨域问题
	header("Content-type: text/html; charset=utf-8");
	
	$mpdf = new Mpdf();
	//自动识别文字字符
	$mpdf->autoScriptToLang = true;
	$mpdf->autoLangToFont = true;
	//要生成的html文件
	$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">
                 
            </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">
                 
       
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值