TCPDF打印从入门到精通

1. TCPDF的模块导入

TCPDF的官网为https://tcpdf.org

官方文档有几十个例子;详情参看官方文档!

2. 使用 TCPDF打印

例如:横版表格打印并自动分页

/**
     * 打印
     */
    public function actionPrint($id)
    {
        //设置无布局
        $this->layout = false;

        //获取打印数据
        $model = $this->findModel($id);

        if(!($model->is_del == 0)){
            throw new ForbiddenHttpException("该单据无法打印");
        }

        $wmsCheckDetail = new \backend\models\WmsCheckDetailSearch();
        $dataProvider = $wmsCheckDetail->searchByCheckId($id);

        //生成页面内容
        $content = $this->render('print', [
            'model' => $model,
            'dataProvider'=>$dataProvider,
        ]);
        //打印对象
//        $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,true, 'UTF-8', false);
        $pdf = (new \TCPDF('L', 'mm', array(240, 140), true, 'UTF-8', false));
        //设置无打印头
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        //设置PDF打印纸张的编剧
        $pdf->SetMargins(20, 20,20);
        //设置单元格内边距
        $pdf->setCellPaddings(0, 0, 0, 0);
        //设置自动进入下一页
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_RIGHT);
        $pdf->AddPage();
        //设置字体
        $pdf->SetFont('stsongstdlight', '', 10);
        $pdf->writeHTML($content, true, false, true, false, '');
        $pdf->lastPage();
        $pdf->get('库存盘点.pdf');

    }
<style type="text/css">
    @page {
        size: A4;
        margin: 0px;
    }

    .out {
        width: 100%;
        /*border: 1px solid #ddd;*/
        /*margin: 0 auto;*/
    }

    .out .title {
        text-align: center;
    }

    table.table {
        margin: 10px;
        width: 100%;
    }

    table.table-bordered {
        /*table-layout: fixed;*/
        margin: 100px;
        /*border-collapse: collapse;*/
        border: none;
        width: 100%;
    }

    table.table-bordered td {
        border: solid 1px #000 ;
        height: 26px;
        line-height: 26px;
        text-indent: 0.5em;
    }

    .footer {
        width: 100%;
        margin: 10px;
    }
</style>
<div class="out">
    <h2 class="title"><?= $model->common_producer_info_name ?> | 库存盘点清单</h2>
    <table class="table-bordered" style="font-size: 10px">
        <tr>
            <td colspan="24" class="active">基本信息</td>
        </tr>
        <tr>
            <td colspan="2" class="active" align="center">编号</td>
            <td colspan="6" align="center">
                <?= $model->wms_check_code; ?>
            </td>
            <td colspan="4" class="active" align="center">存货类型</td>
            <td colspan="4" align="center">
                <?= $model->common_producer_herb_type == 1 ? '原料' : '成品'; ?>
            </td>
            <td colspan="4" class="active" align="center">截止日期</td>
            <td colspan="4" align="center">
                <?= date('Y-m-d', strtotime('-1 day', $model->wms_check_end_at)) ?>
            </td>
        </tr>
        <tr>
            <td colspan="24" class="active">库存清单</td>
        </tr>
        <tr>
            <td colspan="2" class="active" align="center">名称</td>
            <td colspan="2" class="active" align="center">等级</td>
            <td colspan="4" class="active" align="center">单号</td>
            <td colspan="2" class="active" align="center">件数(袋)</td>
            <td colspan="2" class="active" align="center">实盘</td>
            <td colspan="2" class="active" align="center">差异</td>
            <td colspan="2" class="active" align="center">规格</td>
            <td colspan="2" class="active" align="center">重量(公斤)</td>
            <td colspan="2" class="active" align="center">实盘</td>
            <td colspan="2" class="active" align="center">差异</td>
            <td colspan="2" class="active" align="center">说明</td>
        </tr>
        <?php foreach ($dataProvider->getModels() as $detail){ ?>
            <tr>
                <td colspan="2" align="center"><?= $detail->common_producer_herb_info_name ?></td>
                <td colspan="2" align="center"><?= substr($detail->common_producer_herb_grade_info_name, 0, 12) ?></td>
                <td colspan="4" align="center"><?= $detail->wms_herb_in_sheet_number ?></td>
                <td colspan="2" align="center"><?= $detail->wms_check_detail_system_package_num ?></td>
                <td colspan="2" align="center"></td>
                <td colspan="2" align="center"></td>
                <td colspan="2" align="center"></td>
                <td colspan="2" align="center"><?= \common\models\Base::weightBcdiv($detail->wms_check_detail_system_weight) ?></td>
                <td colspan="2" align="center"></td>
                <td colspan="2" align="center"></td>
                <td colspan="2" align="center"></td>
            </tr>
        <?php } ?>
        <tr>
            <td colspan="24" class="active">库管签字</td>
        </tr>
        <tr>
            <td colspan="4" class="active" align="center">盘点人</td>
            <td colspan="8">

            </td>
            <td colspan="4" class="active" align="center">盘点日期</td>
            <td colspan="8">

            </td>
        </tr>
        <tr>
            <td colspan="4" align="center">盘点备注</td>
        <td colspan="20" class="note">

        </td>
        </tr>

        <tr>
            <td colspan="24" class="active">财务签字</td>
        </tr>
        <tr>
            <td colspan="4" class="active" align="center">复核人</td>
            <td colspan="8">

            </td>
            <td colspan="4" class="active" align="center">复核日期</td>
            <td colspan="8">

            </td>
        </tr>
        <tr>
            <td colspan="4" align="center">复核备注</td>
            <td colspan="20" class="note">
            </td>
        </tr>
    </table>
    <div class="footer">
    </div>
</div>

$content 返回的是html页面打印的具体内容

纸张竖着打印用

$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,true, 'UTF-8', false);

纸张横着打印使用

$pdf = (new \TCPDF('L', 'mm', array(240, 140), true, 'UTF-8', false));

即宽度为240,高度为140的A4纸

注意:

重要的是纸张的自动分页,如遇到表格也会形成两个表格

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_RIGHT);

上一篇:【转】2014区域赛小结(牡丹江&&鞍山)by kuangbin


下一篇:mysql client--笔记-修改密码-登录-查看数据库-创建数据库