generate the pdf on newtab in dompdf - php

Is there any way to open the created pdf by dompdf to new browser tab ?
I tried these.
When I click the generate button (now it is a submit button) the controllers action
is given below
Controller:
function generatePdf()
{
require_once("dompdf/dompdf_config.inc.php");
$data="this is a sample pdf";
$dompdf = new DOMPDF();
$html = $this->load->view('report/modelpdfview', $data, true);
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("mypdffile.pdf",array('Attachment'=>0));
$this->load->view('view/mysiteView', $data);
}
But it open on the same location which leads the user will lose control from the site. (I know there is back button in browser)

Create a PHP script that creates the PDF content you want to display. If appropriate use some command line parameters to control what's created, or you can store your content in a session variable. Let's call it makemypdf.php
From Javascript, open up a pop-up window with that as a url:
window.open(makemypdf.php, "mypdfwindow","...other window specs");
The new window will contain your PDF file, with your existing page still open.
Your question is a bit light on content, so you'll have to fill in some of the mechanics yourself.
In your main page:
session_start();
$_SESSION['pdfcontent'][0] = "First PDF content";
$_SESSION['pdfcontent'][1] = "Second PDF content";
Your window opener becomes:
window.open("makemypdf.php?pdfcontent=1", "mypdfwindow","...other window specs");
And in makemypdf.php
session_start();
$pdfcontent = $_SESSION['pdfcontent'][$_GET['pdfcontent'];
// render your PDF document here

Related

mPDF version 7 Progress bar workaround

I upgraded mPDF to version 7 and I noticed that progress bar functionality was removed.
I'm looking for something simple: show some html information to the user (i.e. "Processing file...") while mPDF processes the output file.
This is a simplified version of the code and it is working properly:
$mpdf = new Mpdf();
$html_for_pdf = "<p>Hi, this is the html content to render the PDF file</p>";
$mpdf->WriteHTML( $html_for_pdf );
$mpdf->Output();
In order to get what I want, so far I tried:
a) Echo the "Processing file..." html info to screen, but (due to buffering I guess) nothing shows during the processing until the output PDF file finally comes up. The code I tried is this:
$mpdf = new Mpdf();
$html_for_pdf = "<p>Hi, this is the html content to render the PDF file</p>";
$html_processing = "<p>Processing file...</p>";
echo $html_processing;
$mpdf->WriteHTML( $html_for_pdf );
$mpdf->Output();
b) Handle the output buffering with ob_flush(), which shows the "Processing file..." html on screen but the mPDF file rendering breaks and the output file neves shows. The code I tried is this:
$mpdf = new Mpdf();
$html_for_pdf = "<p>Hi, this is the html content to render the PDF file</p>";
$html_processing = "<p>Processing file...</p>";
echo $html_processing;
ob_end_flush();
ob_flush();
flush();
ob_start();
$mpdf->WriteHTML( $html_for_pdf );
$mpdf->Output();
I googled a lot to get any kind of workaround for this without any luck.
Is there any chance to make this work?
Thanks!
Trying to do this during PDF generation in the same document does not make much sense.
Even the ProgressBar in mPDF 6.x updated the information with javascript.
The easiest way would be to process the HTML in a separate request with "Ajax". Any of options to send a request is possible, Fetch API, XMLHttpRequest, jQuery.ajax()…
You would then update content of the original HTML document with javascript:
On sending the request you would set "Processing file..." text,
On request completion you would either change this text to link to
the generated PDF, or send the PDF to the user directly to download.
A simplified example using the Fetch API:
<script>
// this code would be executed on a link click or form submit
document.getElementById('status').innerText = 'Processing file...';
fetch('http://example.com/pdf.php')
.then((response) => {
// check response.ok
document.getElementById('status').innerText = 'File processed';
// handle contents of the document
})
</script>

Open generated pdf in browser in another tab

I'm generating pdf through FPDF. Now all I need is to open this generated pdf in browser.
Searched lot for it, but all am getting is solution for existing pdf where as here we need solution for generated pdf through fpdf.
Following is my code:
<?php require('../pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,10,'This is demo');
$pdf->Output();
?>
A quick browse of the FPDF documentation shows that you can add a couple of parameters to the Output() function call to provide display in browser or download functionality
string Output([string dest [, string name [, boolean isUTF8]]])
See more here.
For example:
<?php require('../pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,10,'This is demo');
$pdf->Output('I');
?>
The above example uses 'I' for inline. The other options are:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
It is all available in the documentation.
For people still looking, adding target="_blank" to your form tag will open the PDF in a new window.
HTML:
<form method="GET" action="/target-destination/" id="pdfForm" target="_blank">
<!-- Content goes here -->
</form>
mPDF:
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
$mpdf->Output('filename.pdf', 'I');
The FPDF inline method demonstration will attempt to insecurely open an About:Blank which should not be relied on as a method for Inline Display, it simply triggers a security response in the browser to auto download to AV check in sandbox. Use one of the traditional methods like A Href=download I will trust that more and can run a URL checker on your link first...

how to open print window after creating pdf in dompdf?

i converted my html to Pdf using dompdf and i want to print it. My problem is how to open my pdf file in print window instead of opening the download dialog?
include('dompdf/dompdf_config.inc.php');
$savein = 'pdfdirectory/';
$html = " my htmlcode here "
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents(($savein.'file.pdf'), $pdf);
$dompdf->stream('file.pdf');
You can't force the user to open the file in their browser, but you can specify options for the stream() method that hint to the browser whether or not to download the file. The default is to tell the browser that the file is an "attachment" which usually translates to a download. But if you modify the last line of your code as follows most browsers should display the PDF in the browser:
$dompdf->stream( 'file.pdf' , array( 'Attachment'=>0 ) );

Code does not execute after creating a PDF file with DOMPDF

After creating a PDF file in PHP Using DOMPDF, I need to redirect user to different page.
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($fileName . '.pdf', array("Attachment" => 0));
header('location:newpage.php');
This code creates the PDF file but does not redirect. How can I fix this problem?
Note : This is my previous question. Still I'm trying to get this fixed, still no luck.
The $dompdf->stream() call starts outputting data to the client. Once content is being sent, it is impossible to modify the headers, and thus perform the redirection you want. You can see this by turning on the warnings in PHP (error_reporting).
A way to do this is to open a new window, but it requires Javascript.
function downloadPDF(){
window.open("get-pdf.php?id=12345");
location.href = "newpage.php";
}

Export a html page to pdf with everything that's written on it, after submit button

I need to export html page to pdf file with everything that's written in it, after I press submit button. It will open new page, with info, and I need for script to automatically make .pdf file (already uploaded to webserver), and get the link from file. Could you give me some easy example (if available, without any plugins, or other features that I must download, I would prefer clean PHP).
Just try this
HTML to PDF with PHP
Using open-source HTML2FPDF project
This solution uses HTML2PDF project (sourceforge.net/projects/html2fpdf/). It simply gets a HTML text and generates a PDF file. This project is based upon FPDF script (www.fpdf.org), which is pure PHP, not using the PDFlib or other third party library. Download [HTML2PDF][1], add it to your projects and you can start coding.
Code example
require("html2fpdf.php");
$htmlFile = "your link";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');

Categories