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>
Related
In codeigniter,
DOMPDF showing blank screen..
no errors (in development mode)..
html generating properly..
but pdf not generating.. only blank screen is there
My Code Snippet:
function pdf_create($html, $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->setPaper('A4', 'potrait');
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf", array("Attachment" => 0));
} else {
return $dompdf->output();
}
}
function invoicepdf()
{
$orderId = base64_decode($this->uri->segment('3'));
$this->load->helper(array('dompdf', 'file'));
$query = "SELECT * FROM `ci_booking` WHERE id='".$orderId."'";
$this->data['order_details'] = $this->home->customQuery($query);
$html = $this->load->view('reports/printinvoice', $this->data, true);
//echo $html; die;
$this->pdf_create($html, 'Invoice -'.$orderId);
}
When I un-comment echo statement of invoicepdf() function.. then output generating properly. but when it passes to pdf_create() function then its showing blank screen, no errors after setting ini_reporting to 1.
I have attached blank screen inspect code, why its showing like this?
please suggest me the changes.
There's two issues with your code:
1.- A small typo that goes a long way:
You're setting a paper orientation which is unknown to DomPDF here $dompdf->setPaper('A4', 'potrait'); you need to change that to portrait or DomPDF may not really understand what you want and fail to render.
2.- All the human-readable text (not HTML formatting) in your view is being generated with Javascript, which requires a browser rendering engine that DomPDF doesn't have (DomPDF renders server-side, not client-side, which is important to consider).
The fact that everything works fine when loading the view in the browser is because in that case your browser handles the document.write correctly. DomPDF however does not (because it's not a real DOM/JS rendering engine, it just formats a plain HTML in a way that fits in a given paper size and orientation).
Try making the text a regular non-JS HTML
Its solved.. thanks to everyone for help.. it was totally layout issue..wrong colspan was number was there in view file
I am using mPDF through PHP in order to generate an exact replica PDF of an HTML page. This PDF is then being saved to the server so that it can be automatically printed for the user through PHP socket programming.
When I print preview the HTML page, it looks exactly as I would want it printed. When I convert the HTML to a PDF with mPDF, it becomes wide and distorted. I just want it to look exactly like the HTML print preview with no changes.
Here is my PHP code:
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
ob_start();
include 'cert.html';
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML($html);
$mpdf->Output();
Ditch mPDF and use a browser based library such as wkhtmltopdf.
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";
}
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');
I'm trying to user HTML2FPDF (http://html2fpdf.sourceforge.net/) to create a PDF of a page, but I can't seem to get it to work properly.
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.
HTML2FPDF code:
function createPDF() {
define('ABSPATH', dirname(__FILE__).'/');
require(ABSPATH.'classes/pdf/html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$html = ob_get_contents();
//$html = htmlspecialchars($html);
if ($html) {
$fileName = "testing.pdf";
$pdf->WriteHTML($html);
$pdf->Output("pdfs/".$fileName);
echo "<p>PDF file is generated successfully! Click here to open it.</p>";
} else {
echo "<p>There has been an error in creating your PDF.</p>";
};
};
If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?
Cheers
A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.
The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.
I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.
The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.
Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.
This would work, but it might be a bit much.
Regards.