freeloader
freeloader

Reputation: 346

Open PDF in a new tab using dompdf

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.

Upvotes: 18

Views: 56106

Answers (6)

Genki
Genki

Reputation: 499

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');"
  ]);

Upvotes: 2

Omashi
Omashi

Reputation: 21

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

Upvotes: 2

Andressa Henriques
Andressa Henriques

Reputation: 29

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.

Upvotes: 2

Abdur Rahman Turawa
Abdur Rahman Turawa

Reputation: 581

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.

Upvotes: 1

FabryProg
FabryProg

Reputation: 104

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);
}

Upvotes: 2

BrianS
BrianS

Reputation: 13924

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.

Upvotes: 52

Related Questions