添加新字体需要两个步骤:
对于您的情况,我们需要生成字体定义文件。例如,我们可以使用命令行:
~/w/t/fpdf181> php makefont/makefont.php font/dejavu-sans/DejaVuSans.ttf cp1252
Font file compressed: DejaVuSans.z
Font definition file generated: DejaVuSans.php
Add font
$fontname = "DejaVuSans";
$pdf->AddFont($fontname, '', 'DejaVuSans.php', true);
http://www.fpdf.org/en/tutorial/tuto7.htm http://www.fpdf.org/en/tutorial/tuto7.htm
[EDIT]
根据您的评论,您使用了该库http://www.fpdf.org/en/script/script92.php http://www.fpdf.org/en/script/script92.php
1) Dejavu 已添加到tfpdf/font/unifont
文件夹。而且,名字应该是
$fontname = 'DejaVu';
2)如果我们想添加自定义字体文件夹,我们需要覆盖_SYSTEM_TTFONTS
// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
// define("_SYSTEM_TTFONTS", "C:/Windows/Fonts/");
tfpdf/tfpdf.php
if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file ))
{
$ttffilename = _SYSTEM_TTFONTS . $file ;
}
else {
$ttffilename = $this->_getfontpath().'unifont/'.$file ;
}
我创建了一个示例测试:
<?php
// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
//define("_SYSTEM_TTFONTS", "font/dejavu-sans/");
require "tfpdf.php";
$pdf = new tFPDF('P', 'mm', 'Letter');
$pdf->AddPage();
//Example object data
/** @var StdClass $item */
$item = new StdClass();
$item->ordinal = 'Item Ordinal';
$item->domain_label = "Item Domain Label";
$item->domain_class_ordinal = "Item Domain Class Ordinal";
$item->class_label = 'Item Class Label';
$item->diagnosis_code = 'Item Diagnosis Code';
$item->diagnosis_label = 'Item Diagnosis Label';
// Add a Unicode font (uses UTF-8)
$fontname = 'DejaVu';
$pdf->AddFont($fontname,'','DejaVuSans.ttf',true);
$pdf->AddFont($fontname, 'I', 'DejaVuSans-Oblique.ttf', true);
$pdf->AddFont($fontname, 'B', 'DejaVuSans-Bold.ttf', true);
$pdf->SetFont('DejaVu','',14);
$domain_header = "Domain".chr(160).($item->ordinal).".".chr(160).($item->domain_label);
$pdf->Cell(90, 6, $domain_header, 0, 0, 'L');
$class_header = "Class".chr(160).($item->domain_class_ordinal).".".chr(160).($item->class_label);
$pdf->Cell(90, 6, $class_header, 0, 1, 'R');
$pdf->Ln(10);
$pdf->SetFont($fontname, '', 10);
$domain_class_info = "Domain".chr(160).($item->ordinal).chr(160).chr(183).chr(160)."Class".chr(160).($item->domain_class_ordinal).chr(160).chr(183).chr(160)."Diagnosis Code".chr(160).($item->diagnosis_code);
$pdf->Cell(40, 6, $domain_class_info);
$pdf->Ln(10);
$pdf->SetFont($fontname, '', 14);
$pdf->Cell(40, 8, $item->diagnosis_label);
$pdf->Ln(10);
$pdf->Output('S');