Reputation: 17
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?
Upvotes: 0
Views: 612
Reputation: 2523
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
Upvotes: 1