Laravel Load Pdf in Dompdf

0md85ypi  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(145)

我生成的报告需要Bookman Old Style字体。但它并没有反映在PDF中。如果我只是将其作为HTML查看,它将以'奥尔兹'显示文本

function print_ta()
{
    //return view('calendaractivities.calendar_activities.generate_ta');

    // Create a new instance of Dompdf
    $dompdf = new Dompdf();

    // Set options for Dompdf
    $options = new Options();
    $options->setIsHtml5ParserEnabled(true);
    $options->setIsRemoteEnabled(true);
    $dompdf->setOptions($options);

    // Render the view file and convert it to HTML
    $html = view('calendaractivities.calendar_activities.generate_ta')->render();

    // Load the HTML into Dompdf
    $dompdf->loadHtml($html);

    // Render the PDF
    $dompdf->render();

    // Output the generated PDF to the browser
    $dompdf->stream('document.pdf', ['Attachment' => false]);
}

这是我的HTML代码。此页只有首页有页眉,每页有页脚。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>

      <head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Header & Footer test</title>

<style>
    @page {
        margin: 10mm;
    }

    body {
        line-height: 1.3;
        /* Avoid fixed header and footer to overlap page content */
        margin-top: 10px;
        /* Adjust this value as needed */
        margin-bottom: 50px;
    }

    .first-page #header {
        position: fixed;
        top: -105px;
        left: -90px;
        width: 100%;
        height: 100px;
        /* For testing */
    }

    .other-pages #header {
        display: none;
    }

    #footer {
        position: fixed;
        bottom: 0;
        left: -90px;
        width: 100%;
        height: 70px;
        font-size: 6pt;
        color: #777;
        /* For testing */
        background: red;
    }

    /* Print progressive page numbers */
    .page-number:before {
        /* counter-increment: page; */
        content: "Pagina "counter(page);
    }

    /* table css 1 px solid black and collapse all borders */
    table {
        border-collapse: collapse;
        border: 1px solid black;
    }

    /* add border to tr th and td */
    table tr th,
    table tr td {
        border: 1px solid black;
    }

    .th_header {
        height: 2cm;
        font-size: 16.66px;
        text-decoration: underline;
        font-family: "Bookman Old Style", serif !important;
        color: red;
    }
</style>
 </head>

<body>
<div class="first-page">
    <header id="header">
        
        <img src="{{ url('images/header_image.png') }}" width="123%" height="130%">
    </header>
</div>

<div class="other-pages">
    <header id="header">
        {{-- add an empty header for other pages --}}
    </header>
</div>

<footer id="footer">
    <img src="{{ url('images/footer_image.png') }}" width="123%">
</footer>

<div id="content">
    <br>
    <table style="width:100%">
        <tr>
            <th class="th_header" colspan="4">
                AUTHORITY TO TRAVEL</th>
        </tr>
    </table>
</div>
</body>

</html>
yzuktlbb

yzuktlbb1#

如何在HTML中加入自定义字体

我们总是可以使用CSS指定HTML代码的字体类型。在自定义字体类型的情况下,必须始终有一个包含字体类型的文件,您可以在CSS中为该文件分配一个名称,稍后在指定font-family时将引用该名称。

MyCustomFont声明示例
@font-face {
  font-family: MyCustomFont;
  src: url(http://mydomainname.com/storage/fonts/MyCustomFont.ttf) format('truetype');
}
MyCustomFont使用示例
.classname {
  font-family: MyCustomFont;
}

What is @font-face?

在DOMPDF上启用自定义字体

$options = $dompdf->getOptions();

// It enables all file type of fonts using
$options->set('defaultMediaType', 'all');

// It enables font subsetting, which means that only the characters you actually use will be included in the generated PDF
$options->set('isFontSubsettingEnabled', true);

在DOMPDF上设置字体的文件夹

如果您的字体存储在storage/app/public/fonts文件夹中,则可以通过符号链接从public调用storage/fonts

$options = $dompdf->getOptions();
$options->setFontCache(storage_path('fonts'));
$options->set('isRemoteEnabled', true);
$options->set('pdfBackend', 'CPDF');
$options->setChroot([
  'resources/views/',
  storage_path('fonts'),
]);

Dompdf自定义字体加载-问题

WOFF或TTF使用更好的PDF?

我附上@K J对PDF中使用的支持字体格式的出色见解。
我鼓励旧的PDF.TTF字体,而不是现代的HTML.WOFF,因为woff的许多变体将不被旧的PDF使用所接受,因为只有TTF可以作为嵌入的替代品

相关问题