dompdf导出pdf中文乱码显示问号?、换行问题、设置图片大小

news2024/9/20 10:28:24

环境: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 <<<EOD

Usage: {$_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-italic
                  face 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.ttf

EOD;
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 readable
  if ( !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 readable
    if ( !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);

        $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模板文件

换行问题可以修改:vendor\phpoffice\phpword\src\PhpWord\Writer\HTML\Part\Head.php

writeStyles方法
        $astarray = [
            'font-family' => $this->getFontFamily(Settings::getDefaultFontName(), $this->getParentWriter()->getDefaultGenericFont()),
            'font-size' => Settings::getDefaultFontSize() . 'pt',
            'word-wrap' => 'break-word',
            'overflow-wrap' => 'break-word'
        ];

设置图片大小(Dpi默认是96 值越大图片越小):

            $options = new Options();
            $options->set('isRemoteEnabled', true);
            $options->setDefaultFont('SimSun');
            $options->setDpi(168);
            $dompdf = new Dompdf($options);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2070443.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ES6笔记总结(Xmind格式):第四天

Xmind鸟瞰图&#xff1a; 简单文字总结&#xff1a; node的模块化&#xff1a; 1.CommonJS 规范&#xff1a;Node.js 遵循 CommonJS 模块规范&#xff0c;该规范定义了如何在服务器环境中实现模块化&#xff0c;包括如何定义模块、如何引入和使用模块。 2.模块的定义&…

百度文心一言API批量多线程【改写伪原创文章软件】-key免费无限写

百度文心大模型的两款主力模型ENIRE Speed、ENIRE Lite全面免费&#xff0c;即刻生效。 百度文心大模型的两款主力模型 这意味着&#xff0c;大模型已进入免费时代&#xff01; 据了解&#xff0c;这两款大模型发布于今年 3 月&#xff0c;支持 8K 和 128k 上下文长度。 ER…

[MRCTF2020]Hello_ misc

解压得一个png图片和一个flag.rar 图片拖入010editor 选择带zip头的这段蓝色全部复制&#xff0c;file-new-new Hex File&#xff0c;黏贴到新文件&#xff0c;另存为为1.zip 要密码,线索中断&#xff08;当然try to restore it.png&#xff0c;隐藏了zip压缩包&#xff0c;可…

git提交本地项目到远程仓库

1、查看项目目录&#xff0c;是否存在.git文件夹&#xff08;若存在则删除&#xff09; 2、登录git并新建一个空白项目 3、idea创建本地git仓库&#xff08;选择本地项目&#xff09; 4、添加要提交的项目&#xff08;项目右键&#xff09; 5、提交代码到本地仓库 6、配置远程…

代码随想录算法训练营第十一天| 150. 逆波兰表达式求值 239. 滑动窗口最大值 347.前 K 个高频元素

目录 一、LeetCode 150. 逆波兰表达式求值思路&#xff1a;C代码 二、LeetCode 239. 滑动窗口最大值思路C代码 三、LeetCode 347.前 K 个高频元素思路C代码 总结 一、LeetCode 150. 逆波兰表达式求值 题目链接&#xff1a;LeetCode 150. 逆波兰表达式求值 文章讲解&#xff1a…

D - Pedometer AtCoder Beginner Contest 367

题意: 一个长度为n的数组a首尾相接&#xff0c;求满足a[i]~a[j]的和是m的倍数的[i,j]对数 思路&#xff1a; 由于首位相接&#xff0c;那么区间i-->j的所有数有两种情况&#xff1a;第一种是i<j的情况&#xff0c;第二种是i>j的情况 为了简化处理&#xff0c;我们可…

信息学奥赛初赛天天练-74-NOIP2016普及组-基础题5-树、父节点、根节点、叶子节点、非叶节点、组合、组合排除法

NOIP 2016 普及组 基础题5 21 从一个 44的棋盘&#xff08;不可旋转&#xff09;中选取不在同一行也不在同一列上的两个方格&#xff0c;共有( )种方法。 22 约定二叉树的根节点高度为 1。一棵结点数为 2016 的二叉树最少有( )个叶子结点&#xff1b;一棵结点数为 2016 的二叉…

STM32 HAL SDADC DMA

1、简介 由于项目需要使用STM32F373单片机的SDADC功能对电位计进行检测,网上资料比较少,踩了很多坑,下面进行总结。 2、STM32CubeMX配置 2.1 RCC配置 2.2 SYS 配置 2.3 SDADC 2 配置 2.3.1 Parameter Settings配置 SDADC共有三种输入模式,分别为差分模式、 单端偏移模…

Web-ssrfme

文章目录 环境分析攻击 环境 首先下载资源包&#xff0c;Ubuntu通过docker拉取环境。 docker-compose up -d分析 <?php highlight_file(__file__); function curl($url){ $ch curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, 0);e…

开源在线文档管理工具MrDoc

MrDoc&#xff0c;也被称为觅思文档或觅道文档&#xff0c;是一款基于Python开发的在线文档系统。它支持Markdown和所见即所得的富文本编辑&#xff0c;适合个人和小型团队作为文档、笔记和知识管理工具。 开源地址&#xff1a;MrDoc: MrDoc觅思文档&#xff0c;适合于个人和中…

Vue | 简单说说 Vuex 实现响应式的原理

Vuex 通过结合 Vue.js 的响应式系统实现了状态的响应式。Vuex 的状态存储于 Vue 的 data 对象中&#xff0c;这确保了对状态的任何 mutation 都是响应式的。 Vuex 使用单一状态树&#xff0c;并通过响应式来进行状态管理。其响应式的实现主要依赖于 Vue 的响应式系统。 Vuex 的…

Linux简单介绍(1)

一、Linux简介与安装 1.1 计算机的相关概念 1. 什么是计算机? 能够接收使用者输入的指令与数据&#xff0c;经由中央处理器的算术与逻辑单元运算处理后&#xff0c;以产生或存储有用的新数据。比如计算器&#xff0c;手机&#xff0c;汽车导航系统&#xff0c;提款机&am…

借题《黑神话悟空》,聊聊UE5 游戏开发中基本的 C++ 概念

最近火的一塌糊涂的《黑神话悟空》就是用UE5引擎开发的。借题发挥&#xff0c;今天讲讲UE游戏开中的一些C基本概念&#xff1b; 编写代码与蓝图&#xff08;可视化脚本&#xff09;相结合具有独特的功能&#xff0c;您需要利用这些功能来实现两全其美。编程可以帮助创建更复杂…

DevEcoStudio启动模拟器提示未开启Hyper-V

处理方式&#xff1a;&#xff08;win11专业版系统&#xff09; 勾选Hyper-V。 如果提示无法安装Hyper-v&#xff1a;该固件中的虚拟化支持被禁用。如下图&#xff1a; 需要进入BIOS启用虚拟化技术。 这个根据电脑主板不一样&#xff0c;操作方法不同&#xff0c;请自行搜索处…

LCD模组驱动开发

Linux 5.15 内核适配 驱动勾选 由于使用的是 SPI0&#xff0c;所以 TinyVision 的 LCD 模块并不支持使用MIPI-DBI进行驱动&#xff0c;这里我们使用普通的SPI模拟时序。 勾选 SPI 驱动 这里我们使用 SPI-NG 驱动&#xff0c;勾选 <*> SPI NG Driver Support for Allw…

Apifox测试SOAP接口教程

文章目录 I 请求webservice接口使用Apifox测试工具来测试soap接口配合Charles测试soap接口(前提:允许使用系统代理)使用hutool的SoapClient调用soap接口II 扩展知识webService三要素SOAP消息组成(SOAP请求消息的格式)浏览器访问暴露出的soap接口需求: AIS数据对接给其他平…

画板555

p41 这两个地 都是 板框这里按 Q可以改单位放置在原点p42 布局 和原理图一样都是 模块化 布局 (一样的放一起)原理图中选中 PCB里也会选中位号 布局时 一般放在中间因为在别的地可能会影响布局全选后 布局里的属性位置不用全选也行点击查找全部 就选上了 能看到就行板框确定好…

leetcode47. 全排列 II有重复元素的全排列,深度优先搜索

leetcode47. 全排列 II/有重复元素的全排列 给定一个可包含重复数字的序列 nums &#xff0c;按任意顺序 返回所有不重复的全排列。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,2] 输出&#xff1a; [[1,1,2], [1,2,1], [2,1,1]] 示例 2&#xff1a; 输入&#xff1a;…

【数据结构4】树的实例-模拟文件系统、二叉树的遍历(先序遍历、中序遍历、后序遍历、层次遍历)

1 树和二叉树 2 树的实例-模拟文件系统 3 二叉树 3.1 二叉树的遍历 二叉树的先序遍历 二叉树的中序遍历 二叉树的后序遍历 二叉树的层次遍历 1 树 树是一种数据结构 比如:目录结构 树是一种可以递归定义的数据结构树是由n个节点组成的集合:如果n0&#xff0c;那这是一棵空树;如…

测试用例(还需要输入1个字)

近期机缘巧合&#xff0c;连续写2个项目的测试用例。第一个项目&#xff0c;纯属没有办法&#xff0c;参与该项目的现在就只剩我一个人了&#xff0c;只能自己写了&#xff0c;这不&#xff0c;我专门跑到客户那啥都不干&#xff0c;写文档写了2天&#xff1b;第二个项目&#…