dompdf getTextWidth() 获取自定义字体的正确字符串宽度
dompdf getTextWidth() get correct string width for custom font
如何为我的自定义字体获取正确的字符串宽度?
我正在使用 barryvdh/laravel-dompdf (v0.9.0) 创建带有自定义字体的 PDF。但我的问题与纯 dompdf (v1.0.2) 相同。
我需要计算 PDF 中字符串的宽度以确保它适合页面。使用 load_font.php 我安装了字体并创建了 .ufm 文件(AdobeFontMetrics?)。
现在我尝试计算 PDF 中的字符串宽度:
$pdf->getDomPDF()->getFontMetrics()
->getTextWidth($title, 'itc avant garde pro', 32, 0.0, 0.0)
HTML 包含这个:
<style>
h1 {
font-family: 'itc avant garde pro', serif;
font-size: 32px;
letter-spacing: 0;
word-spacing: 0;
}
</style>
<h1 style="margin: 0">WWWWWWWWWWWWWWWWW</h1>
<h1 style="margin: 0">BBBBBBBBBBBBBBBBBBBBBBBBBB</h1>
<div style="background-color:green; height: 20px; width: 539px;"></div>
我在文本下方有一个绿色条来比较宽度。
如果我创建两个长度与我的绿色条相同的字符串 getTextWidth()
不会 return 相同的宽度。
$title = 'WWWWWWWWWWWWWWWWW";
// is the almost same length as the bar and getTextWidth() returns 522.24
$title = 'BBBBBBBBBBBBBBBBBBBBBBBBBB';
// is the almost same length as the bar and getTextWidth() returns width 477.568
我尝试了 CPDF 和 PDFLib 后端。我也尝试手动更改 .ufm 文件,但这对 getTextWidth()
.
没有任何影响
如何为我的字体获取正确的字符串宽度?
getTextWidth()
的第二个参数不是字符串。它实际上是字体本身。还要确保获得正确的字体粗细。
这有效:
$font = $pdf->getDomPDF()->getFontMetrics()->getFont('itc avant garde pro', 'bold');
$pdf->getDomPDF()->getFontMetrics()
->getTextWidth($title, $font, 32, 0.0, 0.0)
如何为我的自定义字体获取正确的字符串宽度?
我正在使用 barryvdh/laravel-dompdf (v0.9.0) 创建带有自定义字体的 PDF。但我的问题与纯 dompdf (v1.0.2) 相同。
我需要计算 PDF 中字符串的宽度以确保它适合页面。使用 load_font.php 我安装了字体并创建了 .ufm 文件(AdobeFontMetrics?)。
现在我尝试计算 PDF 中的字符串宽度:
$pdf->getDomPDF()->getFontMetrics()
->getTextWidth($title, 'itc avant garde pro', 32, 0.0, 0.0)
HTML 包含这个:
<style>
h1 {
font-family: 'itc avant garde pro', serif;
font-size: 32px;
letter-spacing: 0;
word-spacing: 0;
}
</style>
<h1 style="margin: 0">WWWWWWWWWWWWWWWWW</h1>
<h1 style="margin: 0">BBBBBBBBBBBBBBBBBBBBBBBBBB</h1>
<div style="background-color:green; height: 20px; width: 539px;"></div>
我在文本下方有一个绿色条来比较宽度。
如果我创建两个长度与我的绿色条相同的字符串 getTextWidth()
不会 return 相同的宽度。
$title = 'WWWWWWWWWWWWWWWWW";
// is the almost same length as the bar and getTextWidth() returns 522.24
$title = 'BBBBBBBBBBBBBBBBBBBBBBBBBB';
// is the almost same length as the bar and getTextWidth() returns width 477.568
我尝试了 CPDF 和 PDFLib 后端。我也尝试手动更改 .ufm 文件,但这对 getTextWidth()
.
如何为我的字体获取正确的字符串宽度?
getTextWidth()
的第二个参数不是字符串。它实际上是字体本身。还要确保获得正确的字体粗细。
这有效:
$font = $pdf->getDomPDF()->getFontMetrics()->getFont('itc avant garde pro', 'bold');
$pdf->getDomPDF()->getFontMetrics()
->getTextWidth($title, $font, 32, 0.0, 0.0)