Laravel 5:输出tFPDF
Laravel 5: Output tFPDF
我想使用 tFPDF in Laravel 5.5. I added the package with composer and then I tried to output it the according to the docs 但没有任何效果。
我试过直接输出到浏览器:
Route::get('/test',function(){
$pdfLibrary = new tFPDF\PDF();
$pdfLibrary->AddPage();
$pdfLibrary->AddFont('DejaVuSansCondensed', '', 'DejaVuSansCondensed.ttf', true);
$pdfLibrary->SetFont('DejaVuSansCondensed', '', 14);
$pdfLibrary->Write(8, 'Hallo!');
$pdfLibrary->output();
});
但画面一片空白。当我尝试 return $pdfLibrary->output();
然后它刚刚返回
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�u�� �@S�)>+�p�.......
我也试过简单地将pdf保存到文件中,但即使
$pdfLibrary->output('F', storage_path() . '/test.pdf');
没用。
如何保存和输出由 tFPDF 创建的 pdf?
我从 SetASign 找到了另一个包裹,这正是我想要的。具有命名空间支持的简单 tFPDF
包。这是开箱即用的,非常棒。
我强烈建议使用 SetASing 包而不是 DocNetUk 包,因为 DocNetUK 包有很多怪癖。以下是我在使用 DocNetUK 软件包时遇到的一些问题:
- 输出 returns 原始二进制文件,请参阅下文如何使用它
- 重命名字体名称不起作用。
- 方法
tFPDF
已更改为 __construct
,因此您每次都必须在构造函数中调用 parent::construct
,这是必要的,因为命名空间(请参阅 What's difference between __construct and function with same name as class has? )
- 变量名随机更改,没有相关文档:
$this->w
=>$this->flt_current_width
$this->h
=>$this->flt_current_height
这些更改使得来自 DocNetUK 的版本基本上与所有附加组件不兼容。
*过时的答案
我终于意识到 tFPDF 的非官方作曲家版本稍微改变了 output
函数的行为。输出功能只是 returns pdf 内容,但不能将其保存到文件中,也不能直接在浏览器中呈现。
这是在 Laravel
中将原始二进制文件显示为 pdf 的方法
\Response::make($this->output(), 200, ['content-type'=>'application/pdf', 'Content-Disposition' => 'inline; temp.pdf']);
保存原始二进制文件是这样的:
Storage::put('file.pdf', $pdfLibrary->output());
或 file_put_contents
如果没有使用 Laravel。
我想使用 tFPDF in Laravel 5.5. I added the package with composer and then I tried to output it the according to the docs 但没有任何效果。
我试过直接输出到浏览器:
Route::get('/test',function(){
$pdfLibrary = new tFPDF\PDF();
$pdfLibrary->AddPage();
$pdfLibrary->AddFont('DejaVuSansCondensed', '', 'DejaVuSansCondensed.ttf', true);
$pdfLibrary->SetFont('DejaVuSansCondensed', '', 14);
$pdfLibrary->Write(8, 'Hallo!');
$pdfLibrary->output();
});
但画面一片空白。当我尝试 return $pdfLibrary->output();
然后它刚刚返回
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�u�� �@S�)>+�p�.......
我也试过简单地将pdf保存到文件中,但即使
$pdfLibrary->output('F', storage_path() . '/test.pdf');
没用。
如何保存和输出由 tFPDF 创建的 pdf?
我从 SetASign 找到了另一个包裹,这正是我想要的。具有命名空间支持的简单 tFPDF
包。这是开箱即用的,非常棒。
我强烈建议使用 SetASing 包而不是 DocNetUk 包,因为 DocNetUK 包有很多怪癖。以下是我在使用 DocNetUK 软件包时遇到的一些问题:
- 输出 returns 原始二进制文件,请参阅下文如何使用它
- 重命名字体名称不起作用。
- 方法
tFPDF
已更改为__construct
,因此您每次都必须在构造函数中调用parent::construct
,这是必要的,因为命名空间(请参阅 What's difference between __construct and function with same name as class has? ) - 变量名随机更改,没有相关文档:
$this->w
=>$this->flt_current_width
$this->h
=>$this->flt_current_height
这些更改使得来自 DocNetUK 的版本基本上与所有附加组件不兼容。
*过时的答案
我终于意识到 tFPDF 的非官方作曲家版本稍微改变了 output
函数的行为。输出功能只是 returns pdf 内容,但不能将其保存到文件中,也不能直接在浏览器中呈现。
这是在 Laravel
中将原始二进制文件显示为 pdf 的方法 \Response::make($this->output(), 200, ['content-type'=>'application/pdf', 'Content-Disposition' => 'inline; temp.pdf']);
保存原始二进制文件是这样的:
Storage::put('file.pdf', $pdfLibrary->output());
或 file_put_contents
如果没有使用 Laravel。