Skip to content

PHP 处理PDF文件示例 ​

require_once YII_APP_BASE_PATH . '/vendor/tcpdf/tcpdf.php';
        require_once YII_APP_BASE_PATH . '/vendor/FPDI161/fpdi.php';
        $pdf = new FPDI();
        $pdf->setSourceFile($sourcePdf);
        // 导入页面
        $templateId = $pdf->importPage(1);
        // 获取页面的尺寸
        $size = $pdf->getTemplateSize($templateId);
        $pdf->setPrintHeader(false);//隐藏页眉
        $pdf->setPrintFooter(false);//隐藏页脚
        $pdf->AddPage('P', array(101.6, 180));
        // 使用模板
        $pdf->useTemplate($templateId);
        //设置分拣码
        if(isset($this->parameters['port'])){
            $pdf->SetFont('helvetica', 'B', 8);
            $marginLeft = 6;
            if (isset($this->parameters['marginLeft']) && $this->parameters['marginLeft'] > 0) {
                $marginLeft = $this->parameters['marginLeft'];
            }
            $pdf->writeHTMLCell('', '', $marginLeft, '134.8', $this->parameters['port']);
        }
        // 绘制白色矩形覆盖
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Rect(20, 103.8, 70, 12.4, 'F');
        // 生成条形码
        $barcode = new \TCPDFBarcode($this->waybillInfo['code'], 'C128');
        $barcodeImage = $barcode->getBarcodePngData(70, 11.8);
        $pdf->Image('@'.$barcodeImage, 16, 104.8, 70, 11.8,'PNG');
        // 输出 PDF 文件
        $pdf->Output($newPdf, 'F');

        $_pdf = new FPDI();
        $_pdf->setSourceFile($newPdf);
        // 导入页面
        $templateId = $_pdf->importPage(1);
        // 获取页面的尺寸
        $_pdf->getTemplateSize($templateId);
        $_pdf->setPrintHeader(false);//隐藏页眉
        $_pdf->setPrintFooter(false);//隐藏页脚
        $_pdf->AddPage('P', array(101.6, 152.4));
        // 使用模板
        $_pdf->useTemplate($templateId);
        // 输出 PDF 文件
        $_pdf->Output($newPdf, 'F');

woodlc