Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

php - DOMPDF custom fonts

I'm trying to load custom fonts in DomPDF, but without success, still same result.

This is how I'm using fonts:

require_once __DIR__ . '/dompdf/lib/html5lib/Parser.php';

// Sabberworm
spl_autoload_register(function($class)
{
    if (strpos($class, 'Sabberworm') !== false) {
        $file = str_replace('\', DIRECTORY_SEPARATOR, $class);
        $file = realpath(__DIR__ . '/lib/php-css-parser/lib/' . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
        if (file_exists($file)) {
            require_once $file;
            return true;
        }
    }
    return false;
});

// php-font-lib
require_once __DIR__ . '/dompdf/lib/php-font-lib/src/FontLib/Autoloader.php';

//php-svg-lib
require_once __DIR__ . '/dompdf/lib/php-svg-lib/src/autoload.php';


/*
 * New PHP 5.3.0 namespaced autoloader
 */
require_once __DIR__ . '/dompdf/src/Autoloader.php';

DompdfAutoloader::register();

use DompdfDompdf;
use DompdfOptions;


//PDF
$html = '
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style>
            @font-face {
                font-family: "Roboto";
                src: url("https://' . $_SERVER['SERVER_NAME'].'/dompdf/lib/fonts/Roboto-Regular.ttf") format('truetype');
                font-weight: normal;
                font-style: normal;
            }

            * {
                font-family: "Roboto";
            }

        </style>
    </head>
    <body>
        <header>
            <h1>Test</h1>
        </header>
        <div class="content">
            <h2>Test</h2>
            <p>Test</p>
        </div>

        <div class="footer">
            <h3>Test</h3>
            <p>test</p>
        </div>
    </body>
</html>
';

$options = new Options();
$options->setIsRemoteEnabled(true);
$options->isHtml5ParserEnabled(true);

$dompdf = new Dompdf($options);

$dompdf->loadHtml($html);

$dompdf->setPaper('A4', 'landscape');

$dompdf->render();
    
$dompdf->stream('Test.pdf', array("Attachment" => false));  

echo $html;

I'm using custom installation of DomPDF, I follwed instructions of git installation on PhpDOM github page. I also tried to manually install font-loader.php file, but it always crash with lot of memory usage (I updated php.ini to use 512MB, same result) ... Requested font file exists, and also when I use that code without exporting to PDF, it works as expected ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...