PHP TCPDF导出订单合同并且每页加一个印章

安装

composer require tecnickcom/tcpdf

示例代码

 try {
    $order = requestAPI(env('A2_URL') . 'akinst/order/detail', ['user_id' => Auth::id(), 'order_number' => $request->get('p')]);
    $data = Arr::get($order,'0',[]);
    $data['total_amount_ch'] = convertAmountToCn(Arr::get($data,'total_amount',0));
    $data['downdate'] = date('Y-m-d',time()).'星期'.mb_substr( "日一二三四五六",date("w" ,time()),1,"utf-8" );;
    $view = view('tcpdf/contract', compact('data'));
    $content = response($view)->getContent();
    $pdf = new \TCPDF('p', 'mm', 'A4', true, 'UTF-8', false);
    // set margins
    $pdf->SetFont('stsongstdlight', '', 10);
    $pdf->SetCreator('创作者');
    $pdf->SetAuthor('作者');
    $pdf->SetTitle('网络销售合同');
    $pdf->SetSubject('网络销售合同');
    $pdf->SetKeywords('网络销售合同');

    // set default header data
    $pdf->SetHeaderData('', 63, '', '头部' , array(0, 0, 0), array(0, 0, 0));

    //$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    $pdf->SetMargins(15, 20, 15);

    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, 0);

    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $pdf->setPrintHeader(true);
    $pdf->setPrintFooter(false);
    // set some language-dependent strings (optional)
    // ---------------------------------------------------------
    // Add a page
    $pdf->AddPage();
    $html = $content;
    $pdf->setPageMark();

    $pdf->SetXY(15, 20);

    $pdf->writeHTML($html, true, false, true, false, '');

    // ---------------------------------------------------------
    $tmpPath =  "pdf/" . date("Ymd");
    if (!Storage::disk('public')->exists($tmpPath)) {
        Storage::disk('public')->makeDirectory($tmpPath);
    }
    $filename = date("YmdHis") . "_" . rand(0, 10000) . '.pdf';
    $filepath = 'storage/' . $tmpPath . "/" . $filename;
    //循环每页生成公章
    for ($i = 1; $i <= $pdf->getNumPages(); $i++) {
        $pdf->setPage($i);
        $pdf->Image(public_path('/mallWeb/images/official.png'), 140, 220, 50, 43, '', '', '', false, 168, '', false, false, false, false, false, false);
    }
    $pdf->Output(public_path($filepath), 'F');

    if (file_exists(public_path($filepath))) {
        $retval['code'] = 0;
        $retval['filename'] = url($filepath);
        $retval['msg'] = '恭喜,生成合同的PDF文件成功.';
        return $retval;
    } else {
        $retval['code'] = 1;
        $retval['msg'] = '生成合同的PDF文件失败,请联系系统管理员.';
        return $retval;
    }
} catch (\Exception $e) {
    return $e;
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
PHPWord 是一款用于动态生成和操作 Microsoft Word 文档的 PHP 库。在生成 Word 文档时,有时候需要在文档的开头添一个目录,以便读者可以快速查找到各个部分的内容。下面是使用 PHPWord 生成目录的步骤: 1. 首先,我们需要创建一个新的 PHPWord 对象: ```php $phpWord = new \PhpOffice\PhpWord\PhpWord(); ``` 2. 在创建文档的过程中,我们可以使用 `$phpWord->addTableOfContents` 方法来添目录。该方法接受一个参数,用于设置目录的格式。例如,可以指定标题的字体、字号和样式: ```php $section = $phpWord->addSection(); $section->addText('Contents', array('size' => 24, 'bold' => true)); $tableOfContents = $section->addTableOfContents(array('fontSize' => 12, 'marginLeft' => 96, 'tabLeader' => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT)); ``` 3. 在生成文档的过程中,可以使用 `$section->addText` 方法来添正文内容。 ```php $section->addText('Section 1'); $section->addText('Section 2'); $section->addText('Section 3'); ``` 4. 最后,在生成文档的任意位置,可以使用 `$section->addTOC` 方法来插入目录。这将创建一个可点击的目录链接。 ```php $section->addTOC(); ``` 5. 最后,我们将生成Word 文档保存到服务器或者下载到本地: ```php $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $writer->save('path/to/save/document.docx'); ``` 以上就是使用 PHPWord 生成目录的基本步骤。通过添目录,我们可以方便地在 Word 文档中导航和查找各个部分的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值