17

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.

freeloader
  • 336
  • 2
  • 6
  • 18

5 Answers5

50

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.

BrianS
  • 13,284
  • 15
  • 62
  • 125
  • 7
    +1 for array('Attachment'=>0) thought I would have to download this pdf file to my computer all my life – Immutable Brick Apr 16 '13 at 15:35
  • Can someone, please, explain what means " the simplest way is to provide a link with a target attribute"? – renathy Jul 03 '13 at 20:55
  • 4
    @renathy is this in relation to your question [here](http://stackoverflow.com/q/17458553/264628)? In this case the answer presumes the PDF generation script is accessed by link, not form. A target attribute on a `a` or `form` element will cause the browser to open the content in a new window/tab. e.g. `` or `
    `.
    – BrianS Jul 05 '13 at 16:09
3

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);
}
FabryProg
  • 106
  • 1
  • 8
3

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.

Philip John
  • 5,275
  • 10
  • 43
  • 68
2

Well that you can do with the ->stream(); at the end of the chain.

Example:

//Routes  (web.php in case laravel version >= 5.4)
Route::get('/pdf', 'PdfController@pdfStream')->name('pdfStream');

//PdfController.php
public function pdfStream(Request $request) {
   $data["info"] = "I is usefull!";
   $pdf = PDF::loadView('whateveryourviewname', $data);
   return $pdf->stream('whateveryourviewname.pdf');
}

//yourViewPage.blade.php
<a href="{{route("pdfStream")}}" target="_blank" > click me to pdf </a>

Here more information

Omashi
  • 21
  • 4
1

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.