0

I am having problems opening an existing pdf from a directory in laravel. The code works perfectly with pure PHP, but taking it to laravel not work properly because it does not display correctly in the browser, it looks like this:

%PDF-1.5 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x�}��NAE���1�E=��bq~�I�� ��= .....

the code is as follows:

//eval_pdf.blade.php

File::requireOnce('fpdf.php');
File::requireOnce('fpdi.php');
$pdf = new FPDI('P','mm','Letter');
$pdf->AddPage();
$pdf->SetAutoPageBreak(true,10);  
$sourceFileName = app_path().'/include/formato.pdf';
$pdf->setSourceFile($sourceFileName);
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('helvetica','B',8);
 .
 .
 .
$pdf->SetXY(86, 21);
$pdf->Write(0, utf8_decode($comp));
.
.
.
$pdf->Output();

What could be the problem and how to solve it?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
D3vilRemix
  • 9
  • 2
  • 6
  • Your output looks like the browser doesn't realize it's a PDF-file. Do you send the correct mime-type? – Snicksie May 03 '15 at 08:52
  • in fact not; I send data I need from a form, simply I have .. in routes.php Route :: post ('/ evaluacion_PDF' 'HomeController @ evaluacion_pdf') and my controller evaluacion_pdf public function () { View :: make return ('frontend.evaluacion_pdf'); } – D3vilRemix May 03 '15 at 09:09
  • I would not know where add the pdf mime type, if you can guide me a bit would greatly appreciate it – D3vilRemix May 03 '15 at 09:12
  • You need to set the content-type: http://stackoverflow.com/a/18685427/955026 I think you need the type ```application/pdf``` – Snicksie May 03 '15 at 09:20

1 Answers1

1

You should use something like this:

instead of $pdf->Output();

use:

$pdfContent = $pdf->Output('', "S");

to save content into string

and then return response for example:

    return response($pdfContent, 200,
        [
            'Content-Type'        => 'application/pdf',
            'Content-Length'      =>  strlen($pdfContent),
            'Content-Disposition' => 'attachment; filename="mypdf.pdf"',
            'Cache-Control'       => 'private, max-age=0, must-revalidate',
            'Pragma'              => 'public'
        ]
    );
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291