php Dompdf 并设置不同的字体系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24412203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dompdf and set different font-family
提问by John
When generating a PDF it totally ignores my font-family
attribute applied to my CSS. Instead of say Verdana, it uses Times New Roman.So my CSS look like:
生成 PDF 时,它完全忽略了我font-family
应用于 CSS 的属性。而不是说Verdana,它使用Times New Roman 。所以我的 CSS 看起来像:
.sub-header {
font-size: 1.4rem;
text-transform: uppercase;
font-family: NeutraText-Book !important;
padding-bottom: 1.5rem;
}
The PDF is generated like this:
PDF是这样生成的:
$pdf = PDF::loadHTML($view);
return $pdf->stream();
How can I set a font I want?
如何设置我想要的字体?
回答by BrianS
PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol (all using Windows ANSI encoding). dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and referenced in a CSS @font-face
rule. The loading process is necessary in order to produce the font metrics used for type setting.
PDF 文档内部支持以下字体:Helvetica、Times-Roman、Courier、Zapf-Dingbats 和 Symbol(均使用 Windows ANSI 编码)。dompdf 将在 PDF 中嵌入任何引用的字体,只要它已被预加载或可被 dompdf 访问并在 CSS@font-face
规则中引用。为了生成用于类型设置的字体度量,加载过程是必要的。
dompdf supports the same fonts as the underlying R&OS PDF class: Type 1 (.pfb) and TrueType (.ttf) so long as the font metrics (.afm/.ufm) are available. The bundled, PHP-based php-font-lib provides support for loading and sub-setting fonts.
dompdf 支持与底层 R&OS PDF 类相同的字体:Type 1 (.pfb) 和 TrueType (.ttf),只要字体规格 (.afm/.ufm) 可用。捆绑的基于 PHP 的 php-font-lib 支持加载和子设置字体。
The process for loading a font varies depending on your needs and server access. There are three ways you can load a font:
加载字体的过程因您的需要和服务器访问而异。您可以通过三种方式加载字体:
- Use CSS @font-face rules to load a font at run-time.
- From the command line use dompdf/load_font.php.
- Browse to dompdf/www/fonts.php in the included admin site.
- 使用 CSS @font-face 规则在运行时加载字体。
- 从命令行使用 dompdf/load_font.php。
- 在包含的管理站点中浏览到 dompdf/www/fonts.php。
Use CSS @font-face rules to load a font at run-time
使用 CSS @font-face 规则在运行时加载字体
No command line access required. So long as the font you want to load is available online you can load it easily via CSS.
不需要命令行访问。只要您要加载的字体在线可用,您就可以通过 CSS 轻松加载它。
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: normal;
src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format('truetype');
}
From the command line use dompdf/load_font.php
从命令行使用 dompdf/load_font.php
If you have access to the command line then loading a font is as simple as:
如果您有权访问命令行,则加载字体非常简单:
[php] load_font.php "NeutraText-Book" /path/to/neutratext.ttf
Run the command without any parameters to see help text. Quickly, though, the parameters are: name of the font, normal font file, bold font file, italic font file, bold-italic font file
运行不带任何参数的命令以查看帮助文本。不过很快,参数是:字体名称、普通字体文件、粗体字体文件、斜体字体文件、粗斜体字体文件
Browse to dompdf/www/fonts.php in the included admin site
在包含的管理站点中浏览到 dompdf/www/fonts.php
Self-explanatory (sample). The only thing you need to do is make sure you've modified the admin username/password combo
不言自明(样本)。您唯一需要做的就是确保您已经修改了管理员用户名/密码组合
Note: load_font.php and the admin site will not be included by default starting with dompdf 0.7.0
注意:从 dompdf 0.7.0 开始,默认情况下将不包含 load_font.php 和管理站点
Adapted from the dompdf wiki (Unicode How-To, About Fonts and Character Encoding) and other sources.
改编自 dompdf wiki(Unicode How-To,About Fonts and Character Encoding)和其他来源。
回答by Mayra M
I have a similar problem and i have been looking a solution for 2 days...With the new version, the accepted answer does not work any more.
我有一个类似的问题,我一直在寻找解决方案 2 天......使用新版本,接受的答案不再有效。
@jay-bienvenu answer is correct. The new version of DomPDF does not include everything and also there is a very poor documentation.
@jay-bienvenu 答案是正确的。新版本的 DomPDF 并不包括所有内容,而且文档也很差。
So you will have to:
所以你必须:
download load_font.php and place it to the root directory of your project:
curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php
then open load_font.php with your editor and place the correct path to your autoload.inc.php, eg
require_once 'lib/dompdf/autoload.inc.php';
Open the command line, go to the root folder of your project, and run the utility with the name of the font you are registering and the path to the TFF file eg
php load_font.php SourceSansPro ./pathToYourFolder/lib/dompdf/SourceSansPro-Regular.ttf ./pathToYourFolder/lib/dompdf/SourceSansPro-Bold.ttf
下载 load_font.php 并将其放在项目的根目录中:
curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php
然后用你的编辑器打开 load_font.php 并把正确的路径放到你的 autoload.inc.php,例如
require_once 'lib/dompdf/autoload.inc.php';
打开命令行,转到项目的根文件夹,然后使用您注册的字体名称和 TFF 文件的路径运行该实用程序,例如
php load_font.php SourceSansPro ./pathToYourFolder/lib/dompdf/SourceSansPro-Regular.ttf ./pathToYourFolder/lib/dompdf/SourceSansPro-Bold.ttf
Now the font is installed. You may use it as you would normally would as a webfont in html:
现在字体已安装。您可以像往常一样使用它作为 html 中的 webfont:
<html>
<head>
<meta http-equiv="Content-Type" content="charset=utf-8" />
<style type="text/css">
@page {
margin: 0;
}
* { padding: 0; margin: 0; }
@font-face {
font-family: "source_sans_proregular";
src: local("Source Sans Pro"), url("fonts/sourcesans/sourcesanspro-regular-webfont.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
body{
font-family: "source_sans_proregular", Calibri,Candara,Segoe,Segoe UI,Optima,Arial,sans-serif;
}
</style>
</head>
<body>
Your code here ....
</body>
</html>
and to make the pdf in php:
并在php中制作pdf:
require_once 'lib/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($html, 'UTF-8');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4');
$dompdf->set_option('defaultMediaType', 'all');
$dompdf->set_option('isFontSubsettingEnabled', true);
// Render the HTML as PDF
$dompdf->render();
EDIT: i made a blog post about how to use dompdf and apply custom fonts so that things can be more detailed: https://www.blog.lab21.gr/using-domdpf-create-pdf-php
编辑:我写了一篇关于如何使用 dompdf 和应用自定义字体的博客文章,以便更详细:https: //www.blog.lab21.gr/using-domdpf-create-pdf-php
回答by Jay Bienvenu
Dompdf's About Fonts and Character Encodingsays the font utility is included but doesn't tell you how to get it and run it. Here's how:
Dompdf 的About Fonts and Character Encoding说字体实用程序已包含在内,但没有告诉您如何获取和运行它。就是这样:
Download load_font.phpto the root directory of your project.
curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php
Open load_font.php with a text editor (e.g. vim). Change
require_once "autoload.inc.php";
torequire_once "vendor/autoload.php";
Run the utility with the name of the font you are registering and the path to the TFF file. For example:
php load_font.php 'Brush Script MT' https/fonts/brush-script-mt.ttf
将load_font.php下载到项目的根目录。
curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php
使用文本编辑器(例如 vim)打开 load_font.php。更改
require_once "autoload.inc.php";
为require_once "vendor/autoload.php";
使用您正在注册的字体名称和 TFF 文件的路径运行该实用程序。例如:
php load_font.php 'Brush Script MT' https/fonts/brush-script-mt.ttf
Read the code for load_font.phpfor more information about how to use this command.
阅读load_font.php的代码以获取有关如何使用此命令的更多信息。
回答by Ramesh
You need to use dompdf library for generate pdf with specific font family.
您需要使用 dompdf 库来生成具有特定字体系列的 pdf。
$html ='<!doctype html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
$html.='</head><body>';
$html.="<style>
@font-face { font-family: 'Roboto Regular'; font-weight: normal; src: url(\'fonts/Roboto-Regular.ttf\') format(\'truetype\'); }
@font-face { font-family: 'Roboto Bold'; font-weight: bold; src: url(\'fonts/Roboto-Bold.ttf\') format(\'truetype\'); }
body{ font-family: 'Roboto Regular', sans-serif; font-weight: normal; line-height:1.6em; font-size:17pt; }
h1,h2{ font-family: 'Roboto Bold', sans-serif; font-weight: bold; line-height:1.2em; }
</style>";
$html.='test <br>
<span style="font-family:\'Open Sans\'"> test </span> <br> ';
$html.= '</body></html>';
$dompdf = new Dompdf\Dompdf();
$dompdf->load_html($html,'UTF-8');
$dompdf->set_paper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
回答by cOle2
回答by Developer
Thanks for the answer by Brians above, but in my case only one option worked which is by installing font using "dompdf/www/fonts.php"
感谢上面布赖恩斯的回答,但在我的情况下只有一种选择是通过使用“dompdf/www/fonts.php”安装字体
it installed the font by Browsing to dompdf/www/fonts.php in the included admin site. (it will be something like http://yoursite.com/sites/all/modules/print/lib/dompdf/www/fonts.php)
它通过浏览到包含的管理站点中的 dompdf/www/fonts.php 来安装字体。(它将类似于http://yoursite.com/sites/all/modules/print/lib/dompdf/www/fonts.php)
Go to bottom of the page, and click on a link
转到页面底部,然后单击链接
[Authenticate to access this section][1]
it comes up with alert saying "password must be changed in dompdf_config.custom.inc.php".
它出现警告说“必须在 dompdf_config.custom.inc.php 中更改密码”。
- Go to dompdf_config.custom.inc.php file and add the user name and password in the bottom of the file. you can find "DOMPDF_ADMIN_USERNAME" and "DOMPDF_ADMIN_PASSWORD". uncomment these 2 lines and add user name and password for installing the font.
Now, again click on
[Authenticate to access this section][2]
, it asks for user name and password. enter user user name and password which u have given in step one.if you browse the same fonts.php path again in your browser you can find install new fonts section in the bottom of the page( you can see in image)
,(however, i moved my rockwell.ttf and rockwell.afm files to fonts folder first and then i installed here)
choose your desired font ttf file and then install it. it installed and showing in installed fonts but it did not work for me. so I had to create to .afm file for my new font.(here i wanted to use rockwell font)
- 转到 dompdf_config.custom.inc.php 文件并在文件底部添加用户名和密码。您可以找到“DOMPDF_ADMIN_USERNAME”和“DOMPDF_ADMIN_PASSWORD”。取消注释这两行并添加用于安装字体的用户名和密码。
现在,再次单击
[Authenticate to access this section][2]
,它会询问用户名和密码。输入您在第一步中提供的用户名和密码。如果您在浏览器中再次浏览相同的 fonts.php 路径,您可以在页面底部找到安装新字体部分(您可以在图片中看到)
,(但是,我将 Rockwell.ttf 和 Rockwell.afm 文件移动到了字体首先文件夹,然后我在这里安装)
选择您想要的字体 ttf 文件,然后安装它。它安装并以已安装的字体显示,但对我不起作用。所以我不得不为我的新字体创建 .afm 文件。(这里我想使用 Rockwell 字体)
steps I did to create my rockwell.afm file. 1
我为创建rockwell.afm 文件所做的步骤。1
I did this like dompdf other 14 native fonts, I'm not sure how acceptable is this. but it worked for me. I just wanted to get it done.
我这样做就像 dompdf 其他 14 种原生字体一样,我不确定这是否可以接受。但它对我有用。我只是想完成它。
I've added my rockwell font where ever i found the dompdf native font names in php files
Added "rockwell" to
public static $native_fonts
array in dompdf/include/dompdf.cls.php file- Added "rockwell" array to
dompdf/lib/fonts/dompdf_font_family_cache.dist.php file like below
我在 php 文件中找到 dompdf 原生字体名称的地方添加了我的 Rockwell 字体
向
public static $native_fonts
dompdf/include/dompdf.cls.php 文件中的数组添加了“rockwell”- 向
dompdf/lib/fonts/dompdf_font_family_cache.dist.php 文件添加了“rockwell”数组,如下所示
thats it. it generated rockwell.afm file for me like this
就是这样。它像这样为我生成了rockwell.afm文件
just use whatever font family you want to use like
body { font-family: 'rockwell'; font-size:10; }
只需使用您想使用的任何字体系列
body { font-family: 'rockwell'; font-size:10; }
回答by Woody Hayday
Lots of answers here, struggled to get any to provide cross-language support reliably. I believe that for those of us making distributed software, there is also server-setting blocks which stop some functionality such as @import
and src:url()
in pdfdom automatically working to embed a font.
这里有很多答案,很难找到可靠地提供跨语言支持的答案。我认为,对于我们这些做分布式软件,也有服务器设定块,其阻止一些功能,比如@import
和src:url()
在pdfdom自动工作嵌入字体。
The following solution has worked across many servers & locally hosted sites, and requires no command line access:
以下解决方案适用于许多服务器和本地托管站点,并且不需要命令行访问:
- Retrieve font you want to use as a .ttf (for language support including Cyrillic, Greek, Devanagari, Latin, and Vietnamese, we used Noto Sanswith all optional languages checked)
- Run/build-in the following script and fire PDFBuilder_install_font_family() ONCE only (singular install)
- 检索要用作 .ttf 的字体(为了支持包括西里尔文、希腊文、梵文、拉丁文和越南文在内的语言,我们使用了Noto Sans,并选中了所有可选语言)
- 运行/内置以下脚本并仅触发 PDFBuilder_install_font_family() 一次(单一安装)