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

dompdf导出pdf中文乱码显示问号?

环境:PHP 8.0   框架:ThinkPHP 8   软件包:phpoffice/phpword 、dompdf/dompdf

看了很多教程(包括GitHub的issue、stackoverflow)都没有解决、最终找到解决问题的根本!

背景:用Word模板做转PDF的时候,中文乱码,做法是先用模板替换好变量以后,转成HTML,再转成PDF。

解决方案:

1、先将load_font.php放在项目根目录,跟vendor同级

  A、GITHUB下载地址: load_font.php

  B、新建文件load_font.php复制下面代码

<?php
// 1. [Required] Point to the composer or dompdf autoloader
require_once "vendor/autoload.php";// 2. [Optional] Set the path to your font directory
//    By default dompdf loads fonts to dompdf/lib/fonts
//    If you have modified your font directory set this
//    variable appropriately.
//$fontDir = "lib/fonts";// *** DO NOT MODIFY BELOW THIS POINT ***use Dompdf\Dompdf;
use Dompdf\CanvasFactory;
use Dompdf\Exception;
use Dompdf\FontMetrics;
use Dompdf\Options;use FontLib\Font;/*** Display command line usage*/
function usage() {echo <<<EODUsage: {$_SERVER["argv"][0]} font_family [n_file [b_file] [i_file] [bi_file]]font_family:      the name of the font, e.g. Verdana, 'Times New Roman',monospace, sans-serif. If it equals to "system_fonts", all the system fonts will be installed.n_file:           the .ttf or .otf file for the normal, non-bold, non-italicface of the font.{b|i|bi}_file:    the files for each of the respective (bold, italic,bold-italic) faces.If the optional b|i|bi files are not specified, load_font.php will search
the directory containing normal font file (n_file) for additional files that
it thinks might be the correct ones (e.g. that end in _Bold or b or B).  If
it finds the files they will also be processed.  All files will be
automatically copied to the DOMPDF font directory, and afm files will be
generated using php-font-lib (https://github.com/PhenX/php-font-lib).Examples:./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf
./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttfEOD;
exit;
}if ( $_SERVER["argc"] < 3 && @$_SERVER["argv"][1] != "system_fonts" ) {usage();
}$dompdf = new Dompdf();
if (isset($fontDir) && realpath($fontDir) !== false) {$dompdf->getOptions()->set('fontDir', $fontDir);
}/*** Installs a new font family* This function maps a font-family name to a font.  It tries to locate the* bold, italic, and bold italic versions of the font as well.  Once the* files are located, ttf versions of the font are copied to the fonts* directory.  Changes to the font lookup table are saved to the cache.** @param Dompdf $dompdf      dompdf main object * @param string $fontname    the font-family name* @param string $normal      the filename of the normal face font subtype* @param string $bold        the filename of the bold face font subtype* @param string $italic      the filename of the italic face font subtype* @param string $bold_italic the filename of the bold italic face font subtype** @throws Exception*/
function install_font_family($dompdf, $fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {$fontMetrics = $dompdf->getFontMetrics();// Check if the base filename is readableif ( !is_readable($normal) )throw new Exception("Unable to read '$normal'.");$dir = dirname($normal);$basename = basename($normal);$last_dot = strrpos($basename, '.');if ($last_dot !== false) {$file = substr($basename, 0, $last_dot);$ext = strtolower(substr($basename, $last_dot));} else {$file = $basename;$ext = '';}if ( !in_array($ext, array(".ttf", ".otf")) ) {throw new Exception("Unable to process fonts of type '$ext'.");}// Try $file_Bold.$ext etc.$path = "$dir/$file";$patterns = array("bold"        => array("_Bold", "b", "B", "bd", "BD"),"italic"      => array("_Italic", "i", "I"),"bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB"),);foreach ($patterns as $type => $_patterns) {if ( !isset($$type) || !is_readable($$type) ) {foreach($_patterns as $_pattern) {if ( is_readable("$path$_pattern$ext") ) {$$type = "$path$_pattern$ext";break;}}if ( is_null($$type) )echo ("Unable to find $type face file.\n");}}$fonts = compact("normal", "bold", "italic", "bold_italic");$entry = array();// Copy the files to the font directory.foreach ($fonts as $var => $src) {if ( is_null($src) ) {$entry[$var] = $dompdf->getOptions()->get('fontDir') . '/' . mb_substr(basename($normal), 0, -4);continue;}// Verify that the fonts exist and are readableif ( !is_readable($src) )throw new Exception("Requested font '$src' is not readable");$dest = $dompdf->getOptions()->get('fontDir') . '/' . basename($src);if ( !is_writeable(dirname($dest)) )throw new Exception("Unable to write to destination '$dest'.");echo "Copying $src to $dest...\n";if ( !copy($src, $dest) )throw new Exception("Unable to copy '$src' to '$dest'");$entry_name = mb_substr($dest, 0, -4);echo "Generating Adobe Font Metrics for $entry_name...\n";$font_obj = Font::load($dest);$font_obj->saveAdobeFontMetrics("$entry_name.ufm");$font_obj->close();$entry[$var] = $entry_name;}// Store the fonts in the lookup table$fontMetrics->setFontFamily($fontname, $entry);// Save the changes$fontMetrics->saveFontFamilies();
}// If installing system fonts (may take a long time)
if ( $_SERVER["argv"][1] === "system_fonts" ) {$fontMetrics = $dompdf->getFontMetrics();$files = glob("/usr/share/fonts/truetype/*.ttf") +glob("/usr/share/fonts/truetype/*/*.ttf") +glob("/usr/share/fonts/truetype/*/*/*.ttf") +glob("C:\\Windows\\fonts\\*.ttf") +glob("C:\\WinNT\\fonts\\*.ttf") +glob("/mnt/c_drive/WINDOWS/Fonts/");$fonts = array();foreach ($files as $file) {$font = Font::load($file);$records = $font->getData("name", "records");$type = $fontMetrics->getType($records[2]);$fonts[mb_strtolower($records[1])][$type] = $file;$font->close();}foreach ( $fonts as $family => $files ) {echo " >> Installing '$family'... \n";if ( !isset($files["normal"]) ) {echo "No 'normal' style font file\n";}else {install_font_family($dompdf, $family, @$files["normal"], @$files["bold"], @$files["italic"], @$files["bold_italic"]);echo "Done !\n";}echo "\n";}
}
else {call_user_func_array("install_font_family", array_merge( array($dompdf), array_slice($_SERVER["argv"], 1) ));
}

2、下载配置字体

下载地址:simsun

下载之后将ttf字体文件放到项目根目录,跟load_font、vendor同级,这里我改名改成了SimSun.ttf

执行PHP命令:

php load_font.php "SimSun" SimSun.ttf

显示如下:

php load_font.php "SimSun" SimSun.ttf
Unable to find bold face file.
Unable to find italic face file.
Unable to find bold_italic face file.
Copying SimSun.ttf to D:\phpstudy_pro\WWW\newcrm.com\vendor\dompdf\dompdf/lib/fonts/SimSun.ttf...
Generating Adobe Font Metrics for D:\phpstudy_pro\WWW\newcrm.com\vendor\dompdf\dompdf/lib/fonts/SimSun...

如果php命令有问题,检查一下是不是没有配置环境变量,没有配置的话另行寻找配置教程

3、PHP代码如下:

    public function test(){$path = '/storage/contract/' . date('Ymd');$directoryPath = public_path() . $path;if (!file_exists($directoryPath)) {mkdir($directoryPath, 0755, true);}$options = new Options();$options->set('isRemoteEnabled', true);// 重点设置字体$options->setDefaultFont('SimSun');$dompdf = new Dompdf($options);$htmlFile = $directoryPath . '/index.html';$htmlContent = file_get_contents($htmlFile);$dompdf->loadHtml($htmlContent,'UTF-8');$dompdf->setPaper('A4');$dompdf->render();$pdfName = 'index.pdf';$pathToSavePdf = $directoryPath . '/' . $pdfName;$output = $dompdf->output();file_put_contents($pathToSavePdf, $output);}

<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title></title>
</head>
<body>
<div>世界和平
</div>
</body>
</html>

生成PDF后

下面配一个WORD模板(动态变量)->转HTML->生成PDF文件

    public function generateContract($param): array{$contract = $this->contractModel->with(['customer','contacts'])->where('id', $param['id'])->find();if (!$contract) {throw new BusinessException(Code::NOT_FOUND, '合同订单不存在');}$contract = $contract->toArray();$file = public_path() . '/static/template/contract/2024.docx';$templateProcessor = new TemplateProcessor($file);$templateProcessor->setValue('customer', $contract['customer_name']);$templateProcessor->setValue('address', $contract['customer_city'] . $contract['customer_address']);$path = '/storage/contract/' . date('Ymd');$directoryPath = public_path() . $path;if (!file_exists($directoryPath)) {mkdir($directoryPath, 0755, true);}$name = $contract['code'] . mt_rand(1000, 9999);$wordName = $name . '.docx';$pathToSave = $directoryPath . '/' . $wordName;$templateProcessor->saveAs($pathToSave);// 转换 Word 文件为 HTML$phpWord = IOFactory::load($pathToSave);$htmlWriter = IOFactory::createWriter($phpWord, 'HTML');$htmlFile = $directoryPath . '/' . $name . '.html';$htmlWriter->save($htmlFile);// 使用 Dompdf 将 HTML 转换为 PDF$options = new Options();$options->set('isRemoteEnabled', true);$options->setDefaultFont('SimSun');$dompdf = new Dompdf($options);$htmlFile = $directoryPath . '/' . $name . '.html';$htmlContent = file_get_contents($htmlFile);$dompdf->loadHtml($htmlContent,'UTF-8');$dompdf->setPaper('A4');$dompdf->render();$pdfName = $name . '.pdf';$pathToSavePdf = $directoryPath . '/' . $pdfName;$output = $dompdf->output();file_put_contents($pathToSavePdf, $output);// 删除临时 HTML 文件unlink($htmlFile);return ['url' => $path . '/' . $pdfName];}

注:doc文件不兼容,用docx模板文件

相关文章:

dompdf导出pdf中文乱码显示问号?

环境&#xff1a;PHP 8.0 框架&#xff1a;ThinkPHP 8 软件包&#xff1a;phpoffice/phpword 、dompdf/dompdf 看了很多教程&#xff08;包括GitHub的issue、stackoverflow&#xff09;都没有解决、最终找到解决问题的根本&#xff01; 背景&#xff1a;用Word模板做转PDF…...

韩顺平Java-第二十四章:MYSQL基础篇

一 数据库 1 数据库简单原理图 2 使用命令行窗口连接MYSQL数据库 &#xff08;1&#xff09;mysql -h 主机名 -P 端口 -u 用户名 -p密码&#xff1b; &#xff08;2&#xff09;登录前&#xff0c;保证服务启动。 3 MySQL三层结构 &#xff08;1&#xff09;所谓安装MySQL数…...

【动态规划算法题记录】最长/最大 问题汇总 (leetcode)

目录 32. 最长有效括号思路代码 300. 最长递增子序列思路代码 674. 最长连续递增序列思路1&#xff1a;双指针代码1&#xff1a;双指针思路2&#xff1a;dp代码2&#xff1a;dp 718. 最长重复子数组思路1&#xff1a;dp代码1&#xff1a;dp思路2&#xff1a;dp优化代码2&#x…...

2020 位示图

2020年网络规划设计师上午真题解析36-40_哔哩哔哩_bilibili 假设某计算机的字长为32位&#xff0c;该计算机文件管理系统磁盘空间管理采用位示图&#xff08;bitmap&#xff09;&#xff0c;记录磁盘的使用情况。若磁盘的容量为300GB&#xff0c;物理块的大小为4MB&#xff0c;…...

富格林:防止陷入黑幕欺诈平台

富格林指出&#xff0c;不少投资者因未做好投资准备而不慎误入黑幕欺诈平台&#xff0c;造成了不必要的亏损。投资者在投资前&#xff0c;需要时刻保持警惕&#xff0c;根据市场行情&#xff0c;作出有依据的投资决定&#xff0c;而不是依赖黑幕欺诈平台的噱头进行投资。建议投…...

Cookie、Session 、token

Cookie 优点: 简单易用: 浏览器自动管理 Cookie 的发送和接收。持久性: 可以设置过期时间&#xff0c;使其可以在浏览器关闭后依旧存在。广泛支持: 所有现代浏览器都支持 Cookie。 缺点: 安全性问题: 存储在客户端&#xff0c;容易被查看和篡改。敏感信息不应直接存储在 Co…...

Json-类型映射使用TypeFactory或者TypeReference

当你需要将JSON数据转换为Java中的复杂类型时,可以使用Jackson库中的TypeFactory或 者TypeReference。这两种方式可以帮助你处理复杂的泛型类型,例如 List<Map<String, Object>> 或者 Map<String, List<Object>>。 示例 1: 使用 TypeFactory 和 T…...

Linux shell编程学习笔记73:sed命令——沧海横流任我行(上)

0 前言 在大数据时代&#xff0c;我们要面对大量数据&#xff0c;有时需要对数据进行替换、删除、新增、选取等特定工作。 在Linux中提供很多数据处理命令&#xff0c;如果我们要以行为单位进行数据处理&#xff0c;可以使用sed。 1 sed 的帮助信息&#xff0c;功能&#xff…...

内网渗透之icmp隧道传输

原理 # 为什么要建立隧道 在实际的网络中&#xff0c;通常会通过各种边界设备软/硬件防火墙、入侵检测系统来检查对外连接的情况&#xff0c;如果发现异常&#xff0c;会对通信进行阻断。 ​ # 什么是隧道 就是一种绕过端口屏蔽的方式&#xff0c;防火墙两端的数据包通过防火墙…...

【C++ 第十五章】map 和 set 的封装(封装红黑树)

1. map 和 set 的介绍 ⭐map 与 set 分别是STL中的两种序列式容器; 它们是一种树形数据结构的容器&#xff0c;且其的底层构造为一棵红黑树; 而在上一篇文章中提到,其实红黑树本身就是一棵二叉搜索树,是基于二叉搜索树的性质对其增加了平衡的属性来提高其综合性能 ⭐当然也…...

LIN通讯

目录 1 PLinApi.h 2 TLINFrameEntry 结构体 3 自定义函数getTLINFrameEntry 4 TLINScheduleSlot 结构体 5 自定义函数 getTLINScheduleSlot 6 自定义LIN_SetScheduleInit函数 7 自定义 LIN_StartSchedule 8 发送函数 9 线程接收函数 1 PLinApi.h 这是官方头文件 ///…...

zabbix常见架构及组件

Zabbix作为一个开源的、功能全面的监控解决方案&#xff0c;广泛应用于各类组织中&#xff0c;以实现对网络、服务器、云服务及应用程序性能的全方位监控。部署架构灵活性高&#xff0c;可支持从小型单一服务器环境到大型分布式系统的多种场景。基本架构通常包括监控端&#xf…...

plsql表格怎么显示中文 plsql如何导入表格数据

在Oracle数据库开发中&#xff0c;PL/SQL Developer是一款广泛使用的集成开发环境&#xff08;IDE&#xff09;&#xff0c;它提供了丰富的功能来帮助开发人员高效地进行数据库开发和管理。在使用PL/SQL Developer时&#xff0c;许多用户会遇到表格显示中文的问题&#xff0c;以…...

chromedriver下载地址大全(包括124.*后)以及替换exe后仍显示版本不匹配的问题

Chrome for Testing availability CNPM Binaries Mirror 若已经更新了系统环境变量里的chromdriver路径下的exe&#xff0c;仍显示版本不匹配&#xff1a; 则在cmd界面输入 chromedriver 会跳出version verison与刚刚下载好的exe不匹配&#xff0c;则再输入&#xff1a; w…...

拦截器实现 Mybatis Plus 打印含参数的 SQL 语句

1.实现拦截器 package com.sample.common.interceptor;import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import or…...

Oracle Subprogram即Oracle子程序

Oracle Subprogram&#xff0c;即Oracle子程序&#xff0c;是Oracle数据库中存储的过程&#xff08;Procedures&#xff09;和函数&#xff08;Functions&#xff09;的统称。这些子程序是存储在数据库中的PL/SQL代码块&#xff0c;用于执行特定的任务或操作。下面详细介绍Orac…...

自然语言处理实战项目30-基于RoBERTa模型的高精度的评论文本分类实战,详细代码复现可直接运行

大家好,我是微学AI,今天给大家介绍一下自然语言处理实战项目30-基于RoBERTa模型的高精度的评论文本分类实战,详细代码复现可直接运行。RoBERTa模型是由 Facebook AI Research 和 FAIR 的研究人员提出的一种改进版的 BERT 模型。RoBERTa 通过采用更大的训练数据集、动态掩码机…...

RK3588J正式发布Ubuntu桌面系统,丝滑又便捷!

本文主要介绍瑞芯微RK3588J的Ubuntu系统桌面演示&#xff0c;开发环境如下&#xff1a; U-Boot&#xff1a;U-Boot-2017.09 Kernel&#xff1a;Linux-5.10.160 Ubuntu&#xff1a;Ubuntu20.04.6 LinuxSDK&#xff1a; rk3588-linux5.10-sdk-[版本号] &#xff08;基于rk3…...

基于GPT-SoVITS的API实现批量克隆声音

目标是将每一段声音通过GPT-SoVITS的API的API进行克隆,因为拼在一起的整个片段处理会造成内存或者缓存溢出。 将目录下的音频文件生成到指定目录下,然后再进行拼接。 通过AI工具箱生成的数据文件是这样的结构,temp目录下是没个片段生成的部分,connect_是正常拼接的音频文件…...

详解华为项目管理,附华为高级项目管理内训材料

&#xff08;一&#xff09;华为在项目管理中通过有效的沟通、灵活的组织结构、坚持不懈的努力、细致的管理和科学的考核体系&#xff0c;实现了持续的创新和发展。通过引进先进的管理模式&#xff0c;强调以客户需求为导向&#xff0c;华为不仅优化了技术管理和项目研发流程&a…...

Perl(Practical Extraction and Reporting Language)脚本

Perl&#xff08;Practical Extraction and Reporting Language&#xff09;是一种非常灵活的脚本语言&#xff0c;主要用于文本处理、系统管理以及快速原型开发等领域。Perl 脚本可以用来执行一系列任务&#xff0c;包括文件操作、网络通信、数据处理等。 下面是一些关于编写…...

单例模式详细

文章目录 单例模式介绍八种方式1、饿汉式&#xff08;静态常量&#xff09;2、饿汉式&#xff08;静态代码块&#xff09;3、懒汉式&#xff08;线程不安全&#xff09;4、懒汉式&#xff08;线程安全&#xff0c;同步方法&#xff09;5、懒汉式&#xff08;线程不安全&#xf…...

Unity3D 自定义窗口

Unity3D 自定义窗口的实现。 自定义窗口 Unity3D 可以通过编写代码&#xff0c;扩展编辑器的菜单栏和窗口。 简单的功能可以直接一个菜单按钮实现&#xff0c;复杂的功能就需要绘制一个窗口展示更多的信息。 编辑器扩展的脚本&#xff0c;需要放在 Editor 文件夹中。 菜单栏…...

dubbo:dubbo整合nacos实现服务注册中心、配置中心(二)

文章目录 0. 引言1. nacos简介及安装2. 注册中心实现3. 配置中心实现4. 源码5. 总结 0. 引言 之前我们讲解的是dubbozookeeper体系来实现微服务框架&#xff0c;但相对zookeeper很多企业在使用nacos, 并且nacos和dubbo都是阿里出品&#xff0c;所以具备一些天生的契合性&#…...

个人博客指路

Pudding 个人博客 比较懒&#xff0c;直接 github page 了&#xff0c;没国内代理加速。 欢迎大佬们&#xff0c;踩一踩 没做留言&#xff0c;觉得很鸡肋。有问题可以在本文底下评论、或者直接邮件...

【STM32 HAL】多串口printf重定向

【STM32 HAL】多串口printf重定向 前言单串口printf重定向原理实现CubeMX配置Keil5配置 多串口printf重定向 前言 在近期项目中&#xff0c;作者需要 STM32 同时向上位机和手机发送数据&#xff0c;传统的 printf 重定向只能输出到一个串口。本文介绍如何实现 printf 同时输出…...

帆软报表,达梦数据库驱动上传失败

1、按照正常操作新建数据库连接&#xff0c;上传准备好的达梦驱动时&#xff0c;提示如图一需要修改SystemConfig.driverUpload为true才可以。 2、FineDB存储了数据决策系统中除平台属性配置以外的所有信息。详情请参见&#xff1a; FineDB 数据库简介。 3、因此管理员可通过…...

CSS选择器的优先级是如何确定的?有哪些方法可以提高选择器的效率?

CSS选择器的优先级是如何确定的&#xff1f; CSS选择器的优先级决定了当多个选择器同时应用于一个元素时&#xff0c;哪个选择器将最终生效。CSS选择器的优先级由多个因素决定&#xff0c;主要包括以下几个方面&#xff1a; 特殊性&#xff08;Specificity&#xff09; 特殊性…...

【MySQL】基础入门(第二篇)

1.MySQL基本数据类型 数值类型 MySQL 支持所有标准 SQL 数值数据类型。 这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL 和 NUMERIC)&#xff0c;以及近似数值数据类型(FLOAT、REAL 和 DOUBLE PRECISION)。 关键字INT是INTEGER的同义词&#xff0c;关键字DEC是D…...

勇闯机器学习(第二关-数据集使用)

以下内容&#xff0c;皆为原创&#xff0c;重在无私分享高质量知识&#xff0c;制作实属不易&#xff0c;请点点关注。 好戏开场了~~~(这关涉及到了加载数据集的代码&#xff0c;下一关&#xff0c;教你们安装机器学习库) 一.数据集 这一关的目标 知道数据集被分为训练集和测…...

手机上的html编辑器/天津搜索引擎seo

android apk文件的反编译现在有两种形式,一种是转换成smali的字节码格式,一种是专为jar格式的class文件. 先讲第一种. 首先去下一个apktool.它能将apk转译成smali文件并解析出正确的xm格式l的布局文件. 使用时将需要反编译到apk(如weibo.apk)放到解压后的apktool目录下,运行…...

织梦网站tag怎么做/站长工具免费

AcWing 直方图中最大的矩形 题目&#xff1a; 有图。转链接题解&#xff1a; 单调栈。学OI二年了才去学这道经典题… …一种朴素做法就是每次以当前矩形的高为高&#xff0c;然后往左右两边找到第一个高度比自己小的矩形&#xff08;也就是找到了左右边界&#xff09;&#xff…...

wordpress网站源码/seo排名哪家有名

...

做我女朋网站源码/什么是引流推广

总体分为翻译类编程类应用类&#xff08;占大多数&#xff09;翻译类标题格式tra-openpgptra-sqlitetra-bash工具类标题格式win-client-putty,Bitvisewin-client-VNCViewerwin-client-tortoiseSVNwin-client-toad,navicat服务类标题格式分守护类与内核类守护类分网络类与系统类…...

今日头条母公司做网站规划/管理方面的培训课程

TokenStream是一个能在被调用后产生语汇单元流的类&#xff0c;但是 TokenStream 类有两个不同的类型&#xff1a;Tokenizer 类和 TokenFilter 类。这两个类都是从抽象类TokenStream类继承而来。Tokenizer 对象通过Java.io.Reader 对象读取字符创建语汇单元&#xff0c;而Token…...

有谁知道知乎网站是谁做的/四川餐饮培训学校排名

region数量的影响&#xff1a; 通常较少的region可使集群运行的更加平稳&#xff0c;官方指出&#xff0c;每个regionServer大约100个regions的时候效果最佳&#xff0c;理由如下&#xff1a; 1&#xff09;hbase的一个特性MSLAB&#xff0c;它有助于防止堆内存的碎片化&#x…...