Code does not execute after creating a PDF file with DOMPDF - php

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

Related

codeigniter DOMPDF showing blank screen, no errors, html generating properly but pdf not generating

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

(Inline) PHP in domPDF 7.0

I switched from TCPDF to domPDF because it seems more convenient to handle when creating invoices from html to pdf (I am rather a low pro on PHP :)). Now that I created the html file as a PDF file I recognized it does not output any PHP in the PDF - since the data from my sql databanks should fill the PDF it is kinda a problem.
I saw that you can enable PHP in the options.php included in the src-folder and I tried to do like it is written in the manual (and also tried various other code lines) but it just doesn't want to work:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once ("$root/../xxx/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->setIsPhpEnabled('true');
$dompdf = new Dompdf($options);
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("bla",array("Attachment"=>0));
The PDF is shown but without the input from any PHP code.
If someone would be so kind, I would also be interested in knowing why and in how far enabling PHP is a security risk since I actually want to use that for my business. Would it be more advisable to wrap it all up in the main php file without loading external html and css files?
Thanks a lot in advance!
You could do something like this (not tested the code). Replace
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
With
ob_start();
include 'testdomhtml.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
More options How to execute and get content of a .php file in a variable?
Your file_get_contents("testdomhtml.php") will get actual content of file and will not execute any code inside it. Instead make it web accessible and pass URL to this page:
$dompdf->load_html_file('http://yourdomain.ext/testdomhtml.php');

dompdf stream pdf in browser using localhost, but not in network ip

i need to know why dompdf doesn't stream a pdf in browser before download on network ip like 192.168.1.77 , but it works on localhost.
when using network ip, it just immediately download the file , it won't show pdf preview in browser
i'm using dompdf version 0.6.2
this is my code
$dompdf = new DOMPDF();
ob_start();
$this->load->view('report_me',array_merge($this->sesi,$this->session->userdata('printdata')));
// header('Pragma','public');
$html = ob_get_contents();
ob_end_clean();
// $this->session->unset_userdata('printdata');
// $dompdf->set_option('setIsHtml5ParserEnabled', true);
$dompdf->load_html($html);
// (Optional) Setup the paper size and orientation
// $dompdf->setPaper('A4', 'potrait');
// Render the HTML as PDF
$dompdf->render();
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("helvetica", "bold");
$canvas->page_text(44, 760, "Page {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0,0,0));
//ERASE SESSION
// Output the generated PDF to Browser
$dts = date('Y-m-d');
// $dompdf->output();
// $dompdf->stream();
$dompdf->stream('inv_'.$this->Master_model->getInvoiceNumber().'_'.$dts.'.pdf',array('Attachment'=>0));
i had enabling the
ENABLE_REMOTE
to TRUE
Seems like i'm the only one that face the problem, hope someone can help me through this..
Encountered a similar problem on Laravel and tried adding headers. Doesn't work.
I added exit; after stream() and it started displaying. I am guessing the framework may be doing something after stream() causing headers to revert to text/html.
You need to set the header at the top:
header("Content-type: application/pdf");
This should display it in the browser.
If you want it to download set the header like this:
header('Content-disposition: attachment; filename='path/to/file/name.pdf');
this can be set after you have created the pdf and saved it somewhere
NB: path/to/file/name.pdf is on the server not on your local machine
Try to remove some spaces or line breaks between html head body starting and closing tags. Like this <html><head> </head><body> </body><html>.
Also remove thead tbody tfoot tags from your html (sometimes this is not necessary, but in mycase I needed to remove).
And if you are calling the DOM class multiple times. Change the assigned variables (dont let them to be same variable which is in my case was $pdf = new DOMPDF. In second class call I was using same variable. I changed it to $pdf2)
It has taken from here; https://github.com/dompdf/dompdf/issues/902#issuecomment-197655763

Page Not Redirecting to controller after generating a pdf file

I am using Codeigniter as a PHP framework and DOM PDF to generate pdf files. I have the following codes in my Controller.
// Some other codes
include_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$base_path = $_SERVER['DOCUMENT_ROOT'];
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("invoice_$studentid.pdf");
redirect("my_Controller");
The problem is after generating the pdf file it is not redirecting to the Controller. Could you please tell me how to solve this problem?
$dompdf->stream is sending the PDF to the browser. You can't also send a redirect header. You're trying to output two responses to one request, which is impossible.
This doesn't seem like it should be a problem. The browser will stay on whichever page the user was on when they clicked the link to download the PDF. If you really want them to be forced elsewhere (you probably don't, that's a very different user experience from how download links work everywhere else) you could do something with JavaScript.

PHP - Read dynamically generated (and echoed) HTML into a string?

I have a file that pulls some information from the database and creates some relatively simple dynamic HTML.
The file can then be used by DOMPDF to turn it into a PDF document. DOMDPF uses GET variables to achieve the basic implementation.
ob_start();
include_once("report.php?id=1249642977");
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_contents());
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));
ob_end_clean();
I thought I might be able to use something ilke that to achieve the aim but unsurprisingly it didn't work.
So how can I read the HTML that gets output to the browser if you were to load the file directly, into a string for use with the DOMPDF class?
Two problems.
First, you can't call a PHP page like that using include_once - the query string will be ignored. You have to give the id to report.php some other way.
Second, you're correctly buffering the output. However, you're passing the current output buffer to DOMPDF, telling it to generate a PDF to the output buffer, and the discarding the output buffer. You probably want something like:
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));
That'll get the current output buffer, discard it, and disable output buffering. The $dompdf->stream should then work.
What about using a simple HTTP request to get the HTML file and than loading it in the $dompdf object:
<?php
//...
$dompdf->load_html(file_get_contents("http://yoursite.net/report.php?id=1249642977"));
//...
?>
I don't see why you need output buffering here..
BlackAura is on the right track. File_get_contents, as others have suggested will not pass along the GET vars (or POST). But you can use cURL to do that. The code below should work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://YOURFULLURL.COM/report.php?id=1249642977');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$my_html = curl_exec($ch);
curl_close($ch);
$dompdf = new DOMPDF();
$dompdf->load_html($my_html);
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));
Hope that helps.
-Kevin
Simply use PHP's file_get_contents as such:
$page_html = file_get_contents("page.php?id=number");
then pass $page_html to dompdf.
Hope this helps :)
I eventually decided to alter the php page that produces the HTML so that it becomes a function returning an HTML string. This is more of a workaround than a solution.
Future Googlers to this page should be aware that DOMPDF allows you to run inline PHP on a page using:
<script type="text/php">
//some PHP here
</script>
BlackAura has the right idea. You're appending the generated PDF to the output buffer, and then discarding everything.

Categories