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?

4
  • Your output looks like the browser doesn't realize it's a PDF-file. Do you send the correct mime-type? Commented May 3, 2015 at 8:52
  • in fact not; I send data I need from a form, simply <input type = "submit" value = "Send" onclick = "this.form.action = '/ evaluacion_PDF'"> 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'); } Commented May 3, 2015 at 9:09
  • I would not know where add the pdf mime type, if you can guide me a bit would greatly appreciate it Commented May 3, 2015 at 9:12
  • You need to set the content-type: stackoverflow.com/a/18685427/955026 I think you need the type application/pdf Commented May 3, 2015 at 9:20

1 Answer 1

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'
        ]
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.