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

2024-08-21 12:36

本文主要是介绍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中文乱码显示问号?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093180

相关文章

java导出pdf文件的详细实现方法

《java导出pdf文件的详细实现方法》:本文主要介绍java导出pdf文件的详细实现方法,包括制作模板、获取中文字体文件、实现后端服务以及前端发起请求并生成下载链接,需要的朋友可以参考下... 目录使用注意点包含内容1、制作pdf模板2、获取pdf导出中文需要的文件3、实现4、前端发起请求并生成下载链接使

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

SpringBoot实现导出复杂对象到Excel文件

《SpringBoot实现导出复杂对象到Excel文件》这篇文章主要为大家详细介绍了如何使用Hutool和EasyExcel两种方式来实现在SpringBoot项目中导出复杂对象到Excel文件,需要... 在Spring Boot项目中导出复杂对象到Excel文件,可以利用Hutool或EasyExcel

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4