FPDF添加新字体

时间:2013-07-05 03:14:57

标签: php fonts fpdf

我正在使用PHP的FPDF库来生成报告,但现在我需要使用另一种不在核心字体中的字体(Verdana)。我添加了一行:

$pdf->AddFont('Verdana','','verdana.php');

我将文件verdana.php和verdana.z复制到fonts目录中。如果我使用下面的说明,每件事都可以正常工作:

$pdf->SetFont('Verdana','',6);

但是如果我尝试使用下一条指令(使用粗体字):

$pdf->SetFont('Verdana','B',6);

我收到错误:

FPDF error: Undefined font: verdana B

我尝试为Verdana Bold添加另一种字体:

$pdf->AddFont('Verdana-Bold','B','verdanab.php');

当然,我将文件verdanab.php和verdanab.z放在fonts目录中。但我得到了同样的错误。我缺少什么或如何使用Verdana字体(普通和粗体)?

提前致谢。

5 个答案:

答案 0 :(得分:8)

我读了一篇有趣的文章。它可以帮助你找到你想要的东西。

Adding TrueType Fonts to FPDF Documents

也许是这样的:

$pdf->AddFont('Verdana','B','verdanab.php');

答案 1 :(得分:2)

确保在require('fpdf.php')之前添加了脚本顶部的字体目录;

define('FPDF_FONTPATH','./font/');

如果你已经这样做了,那么只需从setFont()方法中删除'B'。它是一个快速修复,而不是一个好的做法。

$pdf->SetFont('Verdana','',6);

如需更多帮助,您可以完成此操作 Adding new fonts and encoding support

答案 2 :(得分:2)

使用以下语法:

$pdf->AddFont('Verdana','','verdanab.php');

而不是使用:

$pdf->AddFont('Verdana','B','verdanab.php');

答案 3 :(得分:1)

Standart字体家族:

<--- Last few GCs --->

[9364:000001CD3A848F50]    98564 ms: Mark-sweep 1266.3 (1441.0) -> 1253.3 (1441.5) MB, 982.1 / 0.0 ms  (average mu = 0.236, current mu = 0.194) allocation failure scavenge might not succeed
[9364:000001CD3A848F50]    99810 ms: Mark-sweep 1268.5 (1441.5) -> 1255.2 (1441.5) MB, 942.6 / 0.0 ms  (average mu = 0.240, current mu = 0.244) allocation failure scavenge might not succeed


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0000029F089DC5C1]
Security context: 0x02528061e6e9 <JSObject>
    1: _addHashTableEntry$3 [0000029F613885B9] [C:\Users\Jahid\Documents\Vue Apps\foxvue\node_modules\sass\sass.dart.js:~22929] [pc=0000029F0A784FF3](this=0x03975ce20131 <JsLinkedHashMap map = 0000016E4169F479>,table=0x03975ce207d1 <Object map = 00000006753069D9>,key=0x02832ded6969 <String[23]: property-class-modifier>,value=1)
    2: putIfAbsent$2 [000002...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF7CCE9DD8A v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4506
 2: 00007FF7CCE78886 node::MakeCallback+4534
 3: 00007FF7CCE79200 node_module_register+2032
 4: 00007FF7CD1930DE v8::internal::FatalProcessOutOfMemory+846
 5: 00007FF7CD19300F v8::internal::FatalProcessOutOfMemory+639
 6: 00007FF7CD379804 v8::internal::Heap::MaxHeapGrowingFactor+9620
 7: 00007FF7CD3707E6 v8::internal::ScavengeJob::operator=+24550
 8: 00007FF7CD36EE3C v8::internal::ScavengeJob::operator=+17980
 9: 00007FF7CD377B87 v8::internal::Heap::MaxHeapGrowingFactor+2327
10: 00007FF7CD377C06 v8::internal::Heap::MaxHeapGrowingFactor+2454
11: 00007FF7CD4A1CD8 v8::internal::Factory::AllocateRawArray+56
12: 00007FF7CD4A2652 v8::internal::Factory::NewFixedArrayWithFiller+66
13: 00007FF7CD223FB6 v8::internal::Dictionary<v8::internal::NumberDictionary,v8::internal::NumberDictionaryShape>::AtPut+15542
14: 00007FF7CD21D7AD v8::internal::HashTable<v8::internal::SimpleNumberDictionary,v8::internal::SimpleNumberDictionaryShape>::KeyAt+7837
15: 00007FF7CD492822 v8::internal::wasm::WasmCodeManager::LookupCode+13426
16: 0000029F089DC5C1
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! foxflue@1.0.0 generate: `nuxt generate`
npm ERR! Exit status 134
npm ERR!
npm ERR! Failed at the foxflue@1.0.0 generate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Jahid\AppData\Roaming\npm-cache\_logs\2020-05-20T15_21_51_902Z-debug.log

您可以在此处为Fpdf创建自己的.php文件:

http://www.fpdf.org/makefont/

答案 4 :(得分:0)

我通过为每种样式定义一种字体来解决它:

$pdf->AddFont('Verdana','','verdana.php');
$pdf->AddFont('Verdanabold','','verdanabold.php');

然后使用:

$pdf->SetFont('Verdana','',6); // Regular style
$pdf->SetFont('Verdanabold','',6); // Bold style