mPDF v8.0.4 - 在 PDF 文件上打印自定义字体

Pau*_*rto 4 php pdf wordpress mpdf woocommerce

1\xc2\xb0这个问题应该很容易解决,但三天以来我一直在努力奋斗。

\n

2\xc2\xb0我只需要在 PDF Roboto-Regular 和 Roboto-thin 中使用两种字体。

\n

3\xc2\xb0我已经阅读了手册,但不幸的是我仍然做错了或者文件中缺少某些内容。
\n文档:https://mpdf.github.io/fonts-languages/font-names.html

\n

4\xc2\xb0我刚刚在 PD 文件上打印了Roboto-Regular ,但没有打印Roboto-Thin
\n非常非常感谢任何帮助!

\n

文件testmpdf.php

\n
// Load MPDF Loader\nrequire_once __DIR__ . \'/vendor/autoload.php\';\n\nuse Mpdf\\Mpdf;\n\n$mpdf = new Mpdf();\n$mpdf = new \\Mpdf\\Mpdf([\'tempDir\' => __DIR__ . \'/custom/temp/dir/path\']);\n\n$defaultConfig = (new \\Mpdf\\Config\\ConfigVariables())->getDefaults();\n$fontDirs = $defaultConfig[\'fontDir\'];\n\n$defaultFontConfig = (new \\Mpdf\\Config\\FontVariables())->getDefaults();\n$fontData = $defaultFontConfig[\'fontdata\'];\n\n$mpdf= new \\Mpdf\\Mpdf(\n[\'mode\' => \'utf-8\',\n\'format\' => \'A4\',\n\'margin_left\' => 0,\n\'margin_right\' => 0,\n\'margin_top\' => 0,\n\'margin_bottom\' => 0,\n\'margin_header\' => 0,\n\'margin_footer\' => 0,\n\'fontDir\' => array_merge($fontDirs, [\n        __DIR__ . \'/assets/css/fonts\',\n    ]),\n    \'fontdata\' => $fontData + [\n        \'roboto\' => [\n            \'R\' => \'Roboto-Regular.ttf\',\n            \'I\' => \'Roboto-Thin.ttf\',\n        ]\n    ],\n    \'default_font\' => \'roboto\'\n\n]); //use this customization\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

HTML部分

\n
$html = \'\n<div class="bkg_div">\n    <p><img style="margin-top:20px;margin-left:35px;" src="images/silhouette_office.png" width="80" /></p>\n\n  <p style="font-family:roboto;font-weight:900;font-size: 30px;color: #ffffff;text-transform: uppercase;margin-left:35px;">BOOKING INVOICE</p>\n\n  <p style="margin-left:35px;font-family:Roboto-thin;font-weight:100;">Gratid&#227;o \'.$username.\'. Your order has been ordered. We sent the proof of purchase by email to you.<br/>\n    Order Booking Time: <b>48 hours</b> - Please make payment now before you lose your reservation!<br/>\n  </p>  \n</div>\n\n
Run Code Online (Sandbox Code Playgroud)\n

CSS文件

\n
li.woocommerce-order-overview__payment-method.method{\n    background: #C8D2D9;\n    padding: 5px;\n    width: 80%;\n    position: relative;\n    border-bottom: 3px dotted #ECEFF1 !important;\n    border-radius: 0px 0px 24px 24px;\n    text-align: center;\n    margin: 0px 50px 0px 50px; \n    font-family:\'Roboto-thin\',sans-serif;\n    font-weight:100;\n    letter-spacing: 0.04em;    \n}\n\n.font_p1 {\n  font-weight:400;\n  font-size: 30px;\n  font-family: Roboto, sans-serif;\n  color: #ffffff;\n  text-transform: uppercase;\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

Fin*_*nwe 8

由于您的字体是在 mPDF 实例化中定义的,因此您需要一般使用font-family: roboto(注意小写),而 Roboto Thin 则需要使用斜体

或者,定义两个字体系列:

'fontdata' => $fontData + [
    'roboto' => [
        'R' => 'Roboto-Regular.ttf',
    ],
    'roboto-thin' => [
        'R' => 'Roboto-Thin.ttf',
    ]
],
Run Code Online (Sandbox Code Playgroud)

并使用robotoroboto-thin用于字体系列声明。