tFPDF 生成一个空的 pdf 文件

2024-03-14

我正在尝试使用 tFPDF 库在 PHP 中生成 PDF 文件。我从 FPDF 开始,除了 UTF-8 字符是乱码之外,它工作得很好。经过一番搜索,我发现可以实现 tFPDF 以在 PDF 文件中包含 UTF-8 字符。但我所有的尝试都导致了一个无法打开的空文件 - 0 KB 文件写入我的硬盘驱动器,并且当我双击它时出现“文件格式有问题”消息。这是我的代码。我在错误日志中没有收到任何 PHP 错误,因此很难判断出了什么问题。

protected function getPDFFileData($diagnosis_id)
{
    define('FPDF_FONTPATH', JPATH_COMPONENT.'/helpers/font/');
    require_once JPATH_COMPONENT.'/helpers/tfpdf.php';
    //require_once JPATH_COMPONENT.'/helpers/fpdf.php';

    $item = $this->getItem($diagnosis_id);

    $pdf = new tFPDF('P', 'mm', 'Letter');
    //$pdf = new FPDF('P', 'mm', 'Letter');
    $pdf->AddPage();

    $fontname = "dejavusans";
    $pdf->AddFont($fontname, '', 'DejaVuSans.ttf', true);
    $pdf->AddFont($fontname, 'B', 'DejaVuSans-Bold.ttf', true);
    $pdf->AddFont($fontname, 'I', 'DejaVuSans-Oblique.ttf', true);
    $pdf->SetFont($fontname, '', 10);

    /* $fontname = "helvetica";
    $pdf->AddFont($fontname, '', 'helvetica.php');
    $pdf->AddFont($fontname, 'B', 'helveticab.php');
    $pdf->AddFont($fontname, 'I', 'helveticai.php');
    $pdf->SetFont($fontname, '', 10); */

    $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();
    $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();
    $pdf->SetFont($fontname, '', 14);
    $pdf->Cell(40, 8, $item->diagnosis_label);
    $pdf->Ln();

    // More code that generates more text . . . 

    return $pdf->Output('S');
}

添加新字体需要两个步骤:

  • 字体定义文件的生成
  • 脚本中字体的声明

对于您的情况,我们需要生成字体定义文件。例如,我们可以使用命令行:

~/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');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

tFPDF 生成一个空的 pdf 文件 的相关文章

随机推荐

  • 最小元素错误

    我不是 C 编码员 所以也许这很容易 我有一个 Point 类向量 我想找到 AABB 矩形 最小 x 最小 y 最小 x 最大 y 最大 x 最小 y 最大 x 最大 y 我已经完成了一个 for 循环 保存最小值和最大值 一次用于 x
  • 强制链接与库不同的 SONAME

    如何以与具有冲突 SONAME 的库的两个现有版本兼容的方式链接二进制文件 这两个版本不共享相同的 SONAME 前缀 一个是 libcapi10 so 3 另一个是 libcapi10 so 4 我无法重新编译它们 因为我将它们作为二进制
  • 在Python中反转列表切片

    我试图在 python 中反转列表的切片 但它返回一个空列表 但是当我尝试使用整个列表时 它工作得很好 我在这里错过了什么吗 l 1 2 3 4 5 6 7 8 l 1 8 7 6 5 4 3 2 1 lt lt lt This worke
  • Flink CEP:对于不同类型的事件,使用哪种方法加入数据流?

    假设我有两种不同类型的数据流 一种提供天气数据 另一种提供车辆数据 我想使用 Flink 对数据进行复杂的事件处理 Flink 1 3 x 中哪种方法是正确的使用方法 我看到了不同的方法 如 Union Connect Window Joi
  • 获取 iframe 的源代码

    有没有办法获取 iframe 加载的页面的源代码 我不想更改任何代码 我只想阅读它 我还需要能够使用 javascript html 来获取它 document getElementById iframeID contentWindow d
  • Swift 2 中“kGMSMarkerAnimationPop”错误的使用不明确

    我在尝试为 GMSMarker 制作动画时遇到错误 我已遵循 Google 文档和各种指南 但它不断返回错误 下面是我的代码 func placeMarker coordinate CLLocationCoordinate2D if loc
  • g++ 链接问题:对函数的未定义引用

    我使用 CMake 和 Visual C 构建 HyDE 库 然后 仍然在 VC 中 我能够成功创建代码并构建链接到 HyDE lib 和 HyDE 头文件的可执行文件 然后我发现 为了与我公司的其他人一起工作 最好在 Eclipse CD
  • 复选框树

    我正在寻找 Javascript 的 复选框树 小部件 我尝试使用jquery 检查树 http jquery checktree googlecode com 其声称完全符合我的要求 但它存在以下问题 它无法识别已选中的复选框 并将所有内
  • 查找用户是否是 Active Directory 组 ASP.NET VB 的成员?

    我正在使用 Active Directory 对 Intranet 站点的用户进行身份验证 我想根据用户在 Active Directory 中所在的组来优化经过身份验证的用户 有人可以向我展示或指出如何在 ASP NET 4 0 VB 中
  • 在 Angular 2 中使用 ngForTemplate 时绑定事件

    假设我有这个简单的列表渲染组件 import Input Component from angular2 core Component selector my list template div item div class MyList
  • 为什么编译器会优化掉由于 strncmp() 而导致的共享内存读取,即使使用了 volatile 关键字?

    这是一个程序foo c将数据写入共享内存 include
  • 角度结构指令上下文模板类型检查

    我似乎无法使角度模板自动完成工作 任何人都可以帮助我理解我错过了什么或做错了什么吗 div xd item div 我几乎继续研究 ngIf 和异步管道源代码 试图了解正在发生的事情 并制定了这个简单的指令 只是为了方便我的异步订阅生活 这
  • 与 Boost 和 ncurses 的静态链接

    我正在制作一个基本的角色扮演游戏 我想静态地包含 Boost 库 以便运行我的游戏的人不需要拥有它们 我研究并查找了所有你需要做的就是添加 static到命令行编译 所以我的命令是这样的 g static o karthas o lncur
  • 如何阻止不良的身份不明的机器人爬行我的网站?

    我怎样才能抵御不良的不明机器人爬行我的网站 一些名称未出现在 Apache cPanel 中的恶意机器人正在严重访问我的网站带宽 我曾在 batgap com robots txt 上尝试过 robots txt 也使用 htaccess
  • 在 boto3 1.5.36 上使用 S3.Object().put() 在 S3 中存储 matplotlib 图像

    除其他事项外 我正在使用绘图matplotlib 我想立即将其存储为 S3 对象 根据中提供的答案this https stackoverflow com q 33771318 1129682 and 这个其他 https stackove
  • 了解 Android 应用程序中的内存泄漏

    我找到了这篇文章 避免内存泄漏 http android developers blogspot it 2009 01 avoiding memory leaks html 其中据说有以下代码 private static Drawable
  • 从 vbscript 自动填充 Excel 文件中的 Excel 用户表单

    我有一个 Excel 文件 其中充满了宏 函数和用户窗体 可帮助用户在此 Excel 文件中正确填写数据 我需要编写一个访问其中一个用户窗体的 VBScript 自动填充它 我知道如何调用工作簿中存在的 VBA 宏 objExcel App
  • 创建应用程序 plist

    Mac OS X Application Info plist 文件是什么样的 我只需要最低限度 这样我就有 启动可执行文件 有一个图标 Thanks 这是真实的 最低限度的
  • 将自定义滚动函数应用于数据框

    尝试将自定义滚动函数应用于 pandas 数据框时出现异常 例如 import statsmodels api as sm import pandas as pd import numpy as np def univar regr bet
  • tFPDF 生成一个空的 pdf 文件

    我正在尝试使用 tFPDF 库在 PHP 中生成 PDF 文件 我从 FPDF 开始 除了 UTF 8 字符是乱码之外 它工作得很好 经过一番搜索 我发现可以实现 tFPDF 以在 PDF 文件中包含 UTF 8 字符 但我所有的尝试都导致