<?php
// Include autoloader
require_once 'dompdf/autoload.inc.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// Load content from html file
$html = file_get_contents("template.php");
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("examplepdf", array("Attachment" => 0));
<style>
.customfont {
color: #35aa45;
}
</style>
<div class="customfont">
this is my text
</div>
现在,当我打开index.php文件时,它会以默认字体创建一个带有绿色文本的pdf。一切正常!
现在,我想在模板中包含自定义字体。
<style>
@font-face {
font-family: 'Roboto';
src: url('fonts/Roboto-Black.ttf') format('truetype');
}
.customfont {
font-family: 'Roboto';
color: #35aa45;
}
</style>
<div class="customfont">
this is my text
</div>
现在,黑屏,没有PDF!如果我将字体的路径更改为错误的文件夹,则绿色文本会像以前一样以pdf格式出现。我现在尝试了各种选项来包括字体,但是由于某种原因,它不包括在内!这里的所有其他帮助尚未解决此问题。有人有主意吗?
谢谢!解决方法是在我的index.php中添加tmp目录
// Instantiate and use the dompdf class
$dompdf = new Dompdf(array('tempDir'=>'/srv/www/xyz/tmp'));
仅此而已。现在所有常规的自定义字体都可以使用!