2017-11-18 174 views
2

我想在Laravel 5.5中使用tFPDF。我添加了作曲家包,然后我试图输出它根据docs,但没有任何工作。Laravel 5:输出tFPDF

我试图输出直接到浏览器:

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();   
}); 

但sceen只是一片空白。当我试图return $pdfLibrary->output(); 那么它只是返回

%PDF-1.3 3 0 OBJ <> endobj 4 0 OBJ <>流许@S)> + P .... ...

我也试图简单的PDF格式保存到一个文件,但即使

没有工作。如何保存&输出由tFPDF创建的pdf?

回答

1

我终于意识到tFPDF的非官方作曲家版本稍微改变了output函数的行为。输出函数只返回pdf内容,但不能将其保存到文件中,也不能直接在浏览器中显示。

所以不是

$pdfLibrary->output('file.pdf'); 

一个人如果没有使用Laravel使用

Storage::put('file.pdf', $pdfLibrary->output()); 

file_put_contents

如果想使其直接在浏览器一个可以使用

return response()->file($pathToFile); 

最后一个音符:永远不变的字体名称。这会在DocnetUK/tfpdf版本中引发错误,但它当然在tfpdf的官方版本中起作用。

更新

另一件事,一个应该注意的是,非官方版本作曲家改变了方法tFPDF__construct,因为命名空间的这是必要的(见What's difference between __construct and function with same name as class has?)。

然而,这意味着延长tfpdf类和在他们构造的所有类,需要调用这样的父类的构造:

class Document extends \tFPDF\PDF 
{ 
    public function __construct(){ 
      parent::__construct(); 
      // ... your code for constructor. 
    } 

    //.... 

} 

而且变量名称已更改:

  • $this->w =>$this->flt_current_width
  • $this->h =>$this->flt_current_height