0

I need help for my laravel project. I am using Fpdi to import PDF but when I display it on localhost, I have only unicode characters like this :

%PDF-1.4 3 0 obj <> endobj 4 0 obj <> stream x�E�K�0D�9�,a�:i�d��H�|�4�ⓠr|�!y�aƗ!C*�r�y���{�98
L>|a�)w endstream endobj 1 0 obj <> endobj 5 0 obj <> stream x�]R�n�0�

Here is my code:

use setasign\ Fpdi;

// initiate FPDI
$pdf = new Fpdi\ Fpdi();
// add a page
$pdf - > AddPage();
// set the source file
$pdf - > setSourceFile(storage_path("justificatif.pdf"));
// import page 1
$tplIdx = $pdf - > importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf - > useImportedPage($tplIdx, 10, 10, 100);

// now write some text above the imported page
$pdf - > SetFont('Helvetica');
$pdf - > SetTextColor(255, 0, 0);
$pdf - > SetXY(30, 30);
$pdf - > Write(0, 'This is just a simple text');

$pdf - > Output();

My route:

Route::get('/testpdf', function() {
    return view('layouts/essai'); 
});

Why is this happening and how to fix this?

1 Answers1

1

Try to return it like this The browser doesn't know the type of the response unless you send it so it doesn't know it should show a pdf

...
return response($pdf->Output('S'))->withHeaders(['Content-Type' => 'application/pdf']);

Source docs https://laravel.com/docs/5.6/responses#attaching-headers-to-responses

Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32
  • The Response content must be a string or object implementing__toString(), "object" given. – Gihad Bayoudh Mar 15 '18 at 19:23
  • no other way to fix this ? maybe my pdf document is not adapt – Gihad Bayoudh Mar 15 '18 at 19:40
  • Hmmm weird, I have an working example the way I answered.. try this `return response($pdf->Output())->withHeaders(['Content-Type' => 'application/pdf']);` – Sérgio Reis Mar 15 '18 at 22:57
  • Update it to `return response($pdf->Output('S'))->withHeaders(['Content-Type' => 'application/pdf']);` to get the PDF [as a string](http://fpdf.org/en/doc/output.htm). – Jan Slabon Mar 16 '18 at 06:53