当前位置: 首页 > article >正文

thinkphp通过html生成pdf

thinkphp 生成pdf

{__NOLAYOUT__}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>合同模板</title>
    <style>
        /*打印内容*/
        @media print {
            @page {
                size: A4 landscape;  /* auto is the initial value */
                margin: 0 auto; /* this affects the margin in the printer settings */
            }
        }
        .printMask {
            width: 100%;height: 100%;
            position: absolute;top: 0;left: 0;
            background-color: #F2F2F2;
            z-index: -1;
        }
        .printContent {
            /*display: none;*/
            width: 1123px;height: auto;
            position: absolute;top: 10px;left: 0;
            z-index: -2;
            background: #fff;
            font-size: 14px;color: #606266 !important;
        }
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        .content {
            margin-bottom: 20px;
        }
        .signature {
            text-align: right;
        }
    </style>
</head>
<body>
<div class="printContent">
    <div class="header">
        <h1>合同</h1>
    </div>
    <div class="content">
        <p>
            这里是合同的条款,你可以根据具体需求编辑这里的文本。
        </p>
        <!-- 其他合同条款 -->
    </div>
    <div class="signature">
        <p>
            签名:<br><br><br>
            日期:
        </p>
    </div>
</div>
</body>
</html>

composer require mpdf/mpdf 下载类库


<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use Mpdf\Mpdf;
use think\Config;

class Pdf extends Backend
{
    protected $file_directory = THINK_PATH.'..'.DS.'public'.DS.'pdf'.DS.'agreement'.DS;
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ["*"];
    public function _initialize() {
        parent::_initialize();
    }

    public function index(){
        $params = $this->request->param();

        $order_sn = '';
        $mark_text = '';


        $file_path = $this->file_directory."{$order_sn}.pdf";
        if(file_exists($file_path)){
        header("location:{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/pdf/agreement/{$order_sn}.pdf");
        }
        $html = view('common@print/order_contract')
            ->assign('order',[])
            ->getContent();

        if(!empty($html)){

            $dest = 'I';
            $file_path = $order_sn.'.pdf';
//            $mpdf = new Mpdf(['mode' => 'utf-8', 'format'=> 'A4-L','orientation'=>'L']);
            $mpdf = new Mpdf(['mode' =>'utf-8','format'=>'A4','margin_left'=> 10,'margin_right'=>10,'margin_top'=>0,'margin_bottom'=>20,'margin_header'=>0,'margin_footer'=>0,'orientation'=>'P']);
            $mpdf->showWatermarkText = true;
            $mpdf->useSubstitutions = true;
            $mpdf->watermark_font = 'GB';
            $mpdf->SetDisplayMode('fullpage');
            $mpdf->SetWatermarkText($mark_text, 0.05);
            $mpdf->WriteHTML($html);

            //$mpdf->Output("{$order_sn}.pdf","D");
            $mpdf->Output($file_path,$dest);
            if(file_exists($file_path) && $dest=='f'){
                header("location:{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/pdf/agreement/{$order_sn}.pdf");
            }
            exit();
        }
    }
}

http://www.kler.cn/a/472242.html

相关文章:

  • C#设计模式(行为型模式):观察者模式
  • 国产编辑器EverEdit - 如何对汉字按笔划数排序?
  • 态势感知是自动化,势态知感是智能化
  • Excel表头/字段一致的表格拼接【python语言】
  • 【UE5 C++课程系列笔记】22——多线程基础——FRunnable和FRunnableThread
  • fmql裸跑编译过程记录
  • Allure 集成 pytest
  • 学习echarts折线图tooltip属性中formatter文字设置不同颜色的方法
  • 新型企业电站 源网荷储充一体化管理系统 EMS3.0具备哪些功能?
  • 太速科技-754-基于Agilex™ 7 FPGA F 系列的PCIe4.0X16 加速卡
  • 全新免押租赁系统打造便捷安全的租赁体验
  • Web3 游戏 2024:牛市下的缺席者?
  • 利用Spark实现WordCount(Scala语言)
  • 【C】文件操作
  • CUDA编程【7】 线程束
  • 青少年编程与数学 02-006 前端开发框架VUE 10课题、侦听器
  • STM32-笔记35-DMA(直接存储器访问)
  • Approaching a Chatbot Service - Part 3: Bot Model
  • flask后端开发(13):登录功能后端实现和钩子函数
  • 扩散模型论文概述(三):Stability AI系列工作【学习笔记】