Dompdf并设置不同的字体系列

Joh*_*ohn 22 php dompdf

生成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;
}
Run Code Online (Sandbox Code Playgroud)

PDF生成如下:

$pdf = PDF::loadHTML($view);
return $pdf->stream();
Run Code Online (Sandbox Code Playgroud)

如何设置我想要的字体?

Bri*_*anS 25

PDF文档内部支持以下字体:Helvetica,Times-Roman,Courier,Zapf-Dingbats和Symbol(全部使用Windows ANSI编码).dompdf会在PDF中嵌入任何引用的字体,只要它已经预先加载或者可以被dompdf访问并在CSS @font-face规则中引用.加载过程是必要的,以便生成用于类型设置的字体度量.

dompdf支持与基础R&OS PDF类相同的字体:只要字体度量(.afm/.ufm)可用,就输入类型1(.pfb)和TrueType(.ttf).捆绑的基于PHP的php-font-lib支持加载和子设置字体.

加载字体的过程因您的需求和服务器访问而异.加载字体有三种方法:

  1. 使用CSS @ font-face规则在运行时加载字体.
  2. 从命令行使用dompdf/load_font.php.
  3. 浏览包含的管理站点中的dompdf/www/fonts.php.

使用CSS @ font-face规则在运行时加载字体

无需命令行访问.只要您想要加载的字体在线可用,您就可以通过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');
}
Run Code Online (Sandbox Code Playgroud)

从命令行使用dompdf/load_font.php

如果您可以访问命令行,则加载字体非常简单:

[php] load_font.php "NeutraText-Book" /path/to/neutratext.ttf
Run Code Online (Sandbox Code Playgroud)

运行不带任何参数的命令以查看帮助文本.但很快,参数是:字体名称,普通字体文件,粗体字体文件,斜体字体文件,粗体斜体字体文件

浏览包含的管理站点中的dompdf/www/fonts.php

不言自明(样本).您唯一需要做的就是确保修改了管理员用户名/密码组合


注意:从dompdf 0.7.0开始,默认情况下不会包含load_font.php和admin站点

改编自dompdf wiki(Unicode操作方法,关于字体和字符编码)和其他来源.


May*_*a M 15

我有类似的问题,我一直在寻找解决方案2天......对于新版本,接受的答案不再适用.

@ jay-bienvenu答案是对的.新版本的DomPDF不包含所有内容,而且文档也很差.

所以你必须:

  1. 下载load_font.php并将其放在项目的根目录中: curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php

  2. 然后用编辑器打开load_font.php并将正确的路径放到autoload.inc.php中,例如 require_once 'lib/dompdf/autoload.inc.php';

  3. 打开命令行,转到项目的根文件夹,然后运行该实用程序,其中包含您要注册的字体的名称以及TFF文件的路径,例如 php load_font.php SourceSansPro ./pathToYourFolder/lib/dompdf/SourceSansPro-Regular.ttf ./pathToYourFolder/lib/dompdf/SourceSansPro-Bold.ttf

现在安装了字体.您可以像在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>
Run Code Online (Sandbox Code Playgroud)

并在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();
Run Code Online (Sandbox Code Playgroud)

编辑:我发了一篇关于如何使用dompdf和应用自定义字体的博客文章,以便更加详细:https://www.blog.lab21.gr/using-domdpf-create-pdf-php


han*_*uge 9

我在实现自定义字体方面遇到了很多困难。

我找到了一个简单且有效的解决方案。所有积分请访问https://peterdev.pl/how-to-set-a-font-in-pdf/

如果您使用 Composer 安装了 dompdf,那么这就是您的操作方法:

$html = '<style type="text/Css">
      @font-face {
         font-family: "Muli";
         font-style: normal;
         font-weight: normal;
      }

      div{ 
         font-family: "Muli";
      }
   </style>

    <div>My PDF File in Muli</div>';

$fontDirectory = '/var/www/html/fonts'; //change to the diretory where you fonts is located on your server
$options = new Dompdf\Options();
$options->setChroot($fontDirectory);

$dompdf = new Dompdf\Dompdf($options);
$dompdf->getFontMetrics()->registerFont(
        ['family' => 'Muli', 'style' => 'normal', 'weight' => 'normal'],
        $fontDirectory . '/muli-v20-latin-regular.ttf'
); // you have to set the style (e.g. italic) and weight (e.g. bold)
$dompdf->loadHtml($html);
$dompdf->render();
Run Code Online (Sandbox Code Playgroud)

Dompdf 会将字体保存到此目录:

供应商/dompdf/dompdf/lib/字体

您的脚本需要对此目录的写入权限(chmod 775 和 chown www-data:www-data 应该有效)。

就是这样。希望它也适合你:)


Jay*_*enu 7

Dompdf 的About Fonts and Character Encoding说字体实用程序已包含在内,但没有告诉您如何获取和运行它。就是这样:

  1. load_font.php下载到项目的根目录。curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php

  2. 使用文本编辑器(例如 vim)打开 load_font.php。更改require_once "autoload.inc.php";require_once "vendor/autoload.php";

  3. 使用您正在注册的字体名称和 TFF 文件的路径运行该实用程序。例如:php load_font.php 'Brush Script MT' https/fonts/brush-script-mt.ttf

阅读load_font.php的代码以获取有关如何使用此命令的更多信息。


8ct*_*pus 7

这是 2022 年的方法:

作曲家需要 dompdf/dompdf:^1.2


use Dompdf\Dompdf;

require '../vendor/autoload.php';

$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Tangerine&display=swap" rel="stylesheet" />
<style>

.m {
    font-family: 'Montserrat';
}

.t {
    font-family: 'Tangerine';
}

</style>
</head>
<body>
    <p class="m">
        Montserrat
    </p>
    <p class="t">
        Tangerine
    </p>
</body>
</html>
HTML
;

//$_dompdf_show_warnings = true;
//$_dompdf_debug = true;

$tmp = sys_get_temp_dir();

$dompdf = new Dompdf([
    'logOutputFile' => '',
    // authorize DomPdf to download fonts and other Internet assets
    'isRemoteEnabled' => true,
    // all directories must exist and not end with /
    'fontDir' => $tmp,
    'fontCache' => $tmp,
    'tempDir' => $tmp,
    'chroot' => $tmp,
]);

$dompdf->loadHtml($html);

$dompdf->render();

$dompdf->stream('hello.pdf', [
    'compress' => true,
    'Attachment' => false,
]);
Run Code Online (Sandbox Code Playgroud)