Adam
Adam

Reputation: 29079

Laravel 5: Output tFPDF

I want to use tFPDF in Laravel 5.5. I added the package with composer and then I tried to output it the according to the docs but nothing worked.

I tried to output it directly to the browser:

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

but the sceen was just blank. When I tried return $pdfLibrary->output(); then it just returned

%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�u�� �@S�)>+�p�.......

I also tried to simply save the pdf to a file, but even

$pdfLibrary->output('F', storage_path() . '/test.pdf');

did not work.

How can I save & output the pdfs created by tFPDF ?

Upvotes: 2

Views: 1105

Answers (1)

Adam
Adam

Reputation: 29079

I found another package from SetASign which is just want I wanted. Simple tFPDF packge with namespace support. This works out of the box and is amazing.

I would strongly recommend to use the SetASing package over the DocNetUk package, because the DocNetUK package has just to many quirks. Here are some of the issues that I had using the DocNetUK package:

  1. Output returns raw binarys, see below how to work with it
  2. Renaming font names does not work.
  3. method tFPDF was changed to __construct, so you have to call parent::construct in your constructor each time, which was necessary because of the namespacing(see What's difference between __construct and function with same name as class has?)
  4. The variable names have been changed randomly and there are no docs for it:
    • $this->w =>$this->flt_current_width
    • $this->h =>$this->flt_current_height

These changes make the version from DocNetUK incompatible with basically all add-ons.


*Outdated answer I finally realized that the unofficial composer version of tFPDF changed slightly the behavior of the output function. The output functions just returns the pdf content, but cannot save it to a file nor render it directly in the browser.

This is how to display raw binarys as a pdf in Laravel

 \Response::make($this->output(), 200, ['content-type'=>'application/pdf', 'Content-Disposition' => 'inline; temp.pdf']);

Saving raw binaries goes like this:

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

or file_put_contents if one is not using Laravel.

Upvotes: 2

Related Questions