Open PDF in a new tab using dompdf
Asked Answered
D

6

18

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.

Dormouse answered 20/6, 2012 at 5:53 Comment(0)
H
52

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.

Hammerless answered 3/7, 2012 at 17:13 Comment(3)
+1 for array('Attachment'=>0) thought I would have to download this pdf file to my computer all my lifeLandfall
Can someone, please, explain what means " the simplest way is to provide a link with a target attribute"?Mishamishaan
@Mishamishaan is this in relation to your question here? 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. <a href="pdfcreate.php" target="_blank"> or <form method="post" action="pdfcreate.php" target="_blank">.Hammerless
C
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);
}
Cassidycassie answered 27/9, 2014 at 18:52 Comment(1)
'Content-Transfer-Encoding: binary' is the keyCentonze
L
2

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.

Loire answered 6/12, 2017 at 10:33 Comment(0)
R
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

Ripon answered 7/9, 2020 at 2:38 Comment(0)
A
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.

Alveta answered 23/1, 2017 at 8:44 Comment(0)
R
1

Submit a web form to dompdf and display the PDF in a new window:

In this example, when the form is submitted, the server script generate_pdf.php sends the form input to dompdf and generates a pdf which is displayed in a new browser window:

<form action="generate_pdf.php" target="_blank" onsubmit="window.open('about:blank', 'popup', 'width=800,height=800,scrollbars=yes,status=yes,resizable=yes,screenx=100,screeny=100');" method="post">

Example using PHP and Codeigniter 3:

echo form_open_multipart('notice/display/' . $notice_id,
  ['class' => 'form-horizontal', 'target' => 'results_popup',
    'onsubmit' => "window.open('about:blank', 'results_popup', 'width=800,height=800,scrollbars=yes,status=yes,resizable=yes,screenx=100,screeny=100');"
  ]);
Rosco answered 6/11, 2023 at 7:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.