thinkphp6.0 html生成pdf

安装wkhtmltopdf

命令行安装

1.更新软件包列表

sudo apt update

2.‌安装 wkhtmltopdf

sudo apt install wkhtmltopdf

3.‌安装必要的依赖项(如需支持图形界面功能)

sudo apt install xvfb

4.测试安装是否成功

wkhtmltopdf --version

5.安装中文包

sudo apt update

sudo apt install fonts-wqy-zenhei fonts-noto-cjk

6.验证字体是否安装成功

fc-list :lang=zh

7.html中加上字体

body {

font-family: "WenQuanYi Zen Hei", "SimSun", "Noto Sans CJK", sans-serif;

}

php代码

复制代码
    public function generatePdf(Request $request){
        $param = $request->param();
        header('Access-Control-Allow-Origin: *'); // 允许所有来源跨域
        header('Access-Control-Allow-Methods: GET, POST'); // 允许的HTTP方法
        header('Access-Control-Allow-Headers: Content-Type'); // 允许的请求头

        $feneralPDF = new GeneralPDF($param['requestid']);
        $htmlContent = $feneralPDF->templateHtml();


        // 配置wkhtmltopdf路径
//        $wkhtmltopdfPath = '/usr/local/bin/wkhtmltopdf';
//        if (!file_exists($wkhtmltopdfPath)) {
            $wkhtmltopdfPath = '/usr/bin/wkhtmltopdf'; // Ubuntu默认安装路径
//        }

        // 创建临时文件
        $tempHtmlFile = tempnam(sys_get_temp_dir(), 'pdf_') . '.html';
        $tempPdfFile = tempnam(sys_get_temp_dir(), 'pdf_') . '.pdf';

        // 写入HTML内容到临时文件
        file_put_contents($tempHtmlFile, $htmlContent);

        // 构建命令
        $command = sprintf(
            '%s --encoding utf-8 --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm %s %s 2>&1',
            $wkhtmltopdfPath,
            escapeshellarg($tempHtmlFile),
            escapeshellarg($tempPdfFile)
        );

        // 执行命令
        $output = [];
        $returnVar = 0;
        exec($command, $output, $returnVar);

        // 检查执行结果
        if ($returnVar !== 0 || !file_exists($tempPdfFile)) {
            throw new \Exception('PDF生成失败: ' . implode("\n", $output));
        }

        // 读取PDF内容
        $pdfContent = file_get_contents($tempPdfFile);

        // 清理临时文件
        unlink($tempHtmlFile);
        unlink($tempPdfFile);
        $filename = $feneralPDF->flowname.'.pdf';
        // 设置响应头
        header('Content-Type: application/pdf');
        header("Content-Disposition: inline; filename={$filename}");
        header('Content-Length: ' . strlen($pdfContent));
        echo $pdfContent;
        ob_flush();
        flush();
    }
相关推荐
SimonKing12 分钟前
白嫖国产多模态大模型:商汤 SenseNova 接入指南
java·后端·程序员
小江的记录本12 分钟前
【ORM框架】MyBatis核心原理、ORM思想、MyBatis vs JPA
java·数据库·后端·spring·spring cloud·oracle·mybatis
卷无止境24 分钟前
FastAPI生产环境密钥管理全解析,从一个.env文件说起
后端·python·fastapi
祀爱28 分钟前
C# MQTT 连接服务
后端·c#·.net
卷无止境29 分钟前
SigV4与HTTPS,两套完全不同维度的安全机制
后端·python·fastapi
青石路31 分钟前
好好的OceanBase官方驱动你不用,非要用第三方驱动,ArrayIndexOutOfBoundsException了吧
java·后端
SamChan9034 分钟前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
一晌小贪欢37 分钟前
Python办公18:PDF 转 Word——利用 OCR 技术批量提取不可编辑的文档内容
开发语言·python·pdf·word·excel·数据可视化·python办公
深念Y1 小时前
微服务抽取路线图:从胖单体到 ARM 集群
前端·arm开发·数据库·后端·微服务·云原生·架构
苏灿烤鱼1 小时前
一个用 Nim 写的隐私优先 Twitter 替代前端,源码级技术剖析
后端·github·twitter