-->

Open PDF in a new tab using dompdf

2020-02-26 07:11发布

问题:

Im trying to generate pdf using dompdf, how can I open the pdf in a new tab in a browser? Like, I Click A link for the PDF and it should open in a new tab, not save it automatically. I want to give the user a choice to save the file after seeing it first. how do i do that?

whenever i use $pdf->output at the end of the file, it does not change a thing, the file is still downloaded automatically. please help. thanks.

回答1:

Whether a PDF is downloaded or viewed in the browser depends on a couple of factors. One is your browser settings, but we have no control there. The second is how dompdf present the PDF to the browser. You can tell dompdf to offer the PDF for direct viewing using $dompdf->stream('my.pdf',array('Attachment'=>0));.

As far as opening in a new tab. That depends on how you are generating the PDF. But the simplest way it to provide a link with a target attribute.



回答2:

I have a same problem into my site (http://www.pdfwebcreator.com)

My solution is:

    $myfile = fopen($strFileName, "r") or die("Unable to open file!");
    $fileSize = filesize($strFileName);
    header("HTTP/1.1 200 OK");
    header("Pragma: public");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    header("Cache-Control: private", false);

    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=\"temporaryPdf.pdf\""); 

    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $fileSize);

    echo fread($myfile, $fileSize);
}


回答3:

Am still experimenting, but something like this works fine as well

$pdf->loadView('file/path', compact('values'));
return $pdf->stream();

With this you can add dynamic values to your pdf file page within the browser.



回答4:

I don't know if you got, but if you use false in this line:

$dompdf-> stream("pasta/doc/relatorio.pdf", array("Attachment" => false));

You can see the pdf in the browser.



标签: dompdf