'Browser not rendering PDF correctly in Laravel
Localhost is fine but when uploaded to server not working
My Code
public function printSalesRecord()
{
$setPaperSize = 'A4';
$pdf = App::make('dompdf');
$pdf = PDF::loadView('salesrecord/PrintSalesRecord')->setPaper($setPaperSize)->setOrientation('portraite');
return $pdf->stream();
}
This is what i get on my browser
%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R >> >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text ] endobj 5 0 obj << /Creator (DOMPDF) /CreationDate (D:20161122092040+00'00') /ModDate (D:20161122092040+00'00') >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 73 >> stream x��2�300P@&�ҹ�B�M���-L�L�,BR����B��5JR�K�Drr�f�B���k��� endstream endobj 8 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Times-Roman /Encoding /WinAnsiEncoding >> endobj xref 0 9 0000000000 65535 f 0000000009 00000 n 0000000074 00000 n 0000000120 00000 n 0000000274 00000 n 0000000303 00000 n 0000000417 00000 n 0000000480 00000 n 0000000624 00000 n trailer << /Size 9 /Root 1 0 R /Info 5 0 R >> startxref 733 %%EOF
Solution 1:[1]
Please double check for vendor:publish
It's look like your pdf may contain utf-8 character.
Tip: UTF-8 support
You need to add this in your template file if you want to have support for UTF-8
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
Another possibility;
Can you change font directory from /BaseFont/dompdf/lib/fonts/Times-Roman to /BaseFont/Times-Roman?
Solution 2:[2]
Try adding response header to your Laravel code
Response::header('Content-type', 'application/pdf');
Or just add .pdf
extension to your route .
Or use download()
function instead of stream()
Solution 3:[3]
Had the same problem, add this at the end of the script or function:
exit();
Solution 4:[4]
Add support for UTF-8 in the file you are loading. Here is a sample code that worked for me
$checkedProducts = $request->input('products');
$pdf = App::make( 'dompdf.wrapper' );
$html ='<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"</head><body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
<table class="table">
<thead>
<th>Product Name</th>
</thead>
<tbody>';
for($i = 0; $i<count($checkedProducts); $i++){
$products = DB::table('products')->where('id', $checkedProducts[$i] )->value('name');
$html.= '<tr><td>'. $products .'</td></tr>';
}
$html.= '</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html> ';
$pdf->loadHTML($html);
return $pdf->download( 'quote.pdf' );
}
I had loaded the PDF file without the html tags and got the same problem as yours. I added them and also added the meta tag with UTF-8 support and worked fine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Imam Assidiqqi |
Solution 2 | |
Solution 3 | Dacosta |
Solution 4 | davidkihara |