php-font-lib,font.php

2023-05-16

// 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 <<

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) ));

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php-font-lib,font.php 的相关文章

  • PHP客户端验证https证书

    我需要创建一个 php 来充当客户端并使用 https 下的一些 Web 服务 我的问题是我还想验证服务器证书 我需要知道我有正确的服务器并且中间没有人充当服务器 有人能帮助我吗 Thanks 如果您有curl 扩展 则可以将其配置为验证连
  • php isset 或空中的偏移类型非法

    我的这段代码最初可以工作 但在重新启动计算机后就无法工作 我收到的错误是 警告 第 4 行 D xampp htdocs cookieboylive classes Session php 中 isset 中存在非法偏移类型或为空 我的网站
  • 如何使 PHPunit 在警告时返回非零退出状态

    当在一些失败并出现警告的测试中调用 PHPunit 时 我得到 phpunit c phpunit xml group app Warning MongoCollection insert expects parameter 1 to be
  • Joomla 2.5 中基于类别的文章的替代布局

    目前 我的 Joomla 2 5 安装中的文章有 2 个 布局 default php default links php feature link php feature link php 当在 文章管理器 的 替代布局 下的 编辑文章
  • 如何将所有输入字段值更改为大写

    我有一组 PHP 字段 在将值发送到查询之前 我想将字母更改为大写 有没有办法做类似的事情 cust name john cust gender male lower upper conversion 那么现在 john 已更改为 JOHN
  • Laravel updateOrCreate 一对一关系

    在我的网络应用程序中我有这个模型 InstagramAccount php UserPageFeed php 每个 InstagramAccount 在 UserPageFeed 中都有一条记录 并且每个 UserPageFeed 都属于
  • 如何通过 docker-php-ext-install 安装 php 扩展?

    为了解决问题 https stackoverflow com questions 37526509 how to install pdo driver in php docker image 我现在尝试通过安装 mysql pdo dock
  • PCRE 库版本太旧

    Bug Genie 3 需要 PCRE 库 8 0 或更高版本 你有 版本 7 8 2008 09 05 将您的系统更新到最新版本 你常用的来源 在我查看问题并尝试通过以下步骤更新我的 PCRE 库后 wget the latest sou
  • php PDO 可以获取两个结果集吗?如果是,1 个结果集和 1 个以上结果集哪个更好?

    如果可能的话 如何获取两个结果集 sth dbh gt prepare SELECT FROM tb1 WHERE cond1 SELECT from tb2 Where cond2 sth gt execute row sth gt fe
  • 处理表单的最佳实践

    我想知道处理表单处理的最佳实践是什么 就我而言 我做了类似的事情 if the user hasn t submited the form 显示表格 else if there are form errors 显示错误 再次显示表格 els
  • 出现致命错误 Uncaught CurlException: 26: 无法打开文件

    我正在尝试使用 PHP 的 GD 库创建朋友的动态图像 并且需要将其上传到我的朋友个人资料中 但我不断收到错误消息 致命错误 未捕获的 CurlException 26 无法打开第 820 行 home p170r760 public ht
  • 什么是好的、免费的 PHP 图表套件?

    我要做的只是基本的折线图 任何人分享的经验将不胜感激 不是真正的 PHP 但我发现 amchart 非常容易实现 而且看起来很棒 http www amcharts com http www amcharts com 还可以查看 Googl
  • PHP/Apache 中的输出缓冲块如何工作?

    假设我将随机数据从 PHP 回显到浏览器 随机数据总量约为 XGb 回显以 YKb 块的形式完成 不使用 ob start PHP 和 Apache 缓冲区已满后 echo 调用是否会阻塞 客户端无法以与生成数据相同的速度使用数据 如果是
  • 如何在 PHP 中随机组合两个数组

    如何将两个数组组合成一个数组 我请求的方式是第三个组合数组应包含一个数组中的一个值和另一个数组中的下一个值 依此类推 或者 它可以是随机的 前任 arr1 1 2 3 4 5 arr2 10 20 30 40 50 和组合数组 arr3 1
  • 我可以在 MySQL 中存储图像吗?

    这个问题在这里已经有答案了 可能的重复 MySQL 中的图像 https stackoverflow com questions 1665730 images in mysql 在 MySQL 中存储图像 https stackoverfl
  • 如何设置pdf的标题名称。查看文档时(新选项卡)[重复]

    这个问题在这里已经有答案了 我们如何更改pdf的标题名称 查看文档时 我没有使用任何 控制器是模态的 我只是在 href 标签中传递 url 但我想 更改标题名称 a target blank href class icon btn blu
  • 服务中有很多依赖项

    我在服务层的应用程序中遇到依赖关系问题 我有以下课程
  • 如何删除字符串中所有不可打印的字符?

    我想我需要删除字符 0 31 和 127 是否有一个函数或一段代码可以有效地完成此操作 7 位 ASCII 如果您的 Tardis 于 1963 年刚刚上市 并且您只想要 7 位可打印 ASCII 字符 则可以使用以下命令删除 0 31 和
  • Mac OS Snow Leopard 上的两个版本的 PHP 以及使用 PECL 安装 xdebug

    我正在使用MAMP在我阅读一些有关内置 Apache PHP 的文章之前 我已经在 PHP 开发 标准 中使用过一段时间了雪豹 我决定转向那些我提供的但似乎有很多问题的人 第一件事是当我使用MAMP时 我将PEAR从1 9 0升级到1 9
  • PHP/regex:如何获取HTML标签的字符串值?

    我需要有关正则表达式的帮助或preg match http php net preg match因为我对这些还没有那么丰富的经验 所以这就是我的问题 我需要获取值 get me 但我认为我的函数有错误 html 标签的数量是动态的 它可以包

随机推荐