php dompdf is not rendering the html - php

Below is the function that I am using to generate the pdf file. I am loading all the required files and classes as specified in the github repository documentation.
public function get_invoice_converted_to_pdf(){
try {
// reference the Dompdf namespace
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->loadHtml('<p>hello world</p>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
echo $dompdf->stream();
} catch (Exception $e) {
echo $e->getMessage();
}
}
It generates the pdf file but in that pdf file all I see is HTML code. Not the rendered output of that HTML.

Related

Why aren't my images loading now I've updated DomPDF to 2.0.3?

I just updated from a fairly old version of DOMPDF and now can't see the images which were previously generating.
This is how I'm calling the PDF generation. It generates everything on the PDF apart from the images.
require_once 'dompdf_2-0-3/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isJavascriptEnabled', TRUE);
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html); // adds the html to the pdf
$dompdf->setPaper('A4', 'portrait'); // paper size and orientation
$dompdf->render(); // Render the HTML as PDF
$date = (string)date("d.m.y");
$dompdf->stream("Yandiya Calc Results ".($date));
Below is an example of where I use images in the HTML document.
<div class="Page3 page-break">
<div class="House_Back_Container"><img src="images/PDF/buildingInformation.png" class="Template"></img></div>
<div class="House_Type_Icon"><img src="images/{HOUSE_TYPE_ICON}" class="House_Type_Icon"></img></div>
<h1 class="House_Type">{HOUSE_TYPE}</h1>
<h1 class="House_Age">{HOUSE_AGE}</h1>
<h1 class="Tariff_Value">{TARIFF}</h1>
<img src="images/{LOCATION_ICON}" class="Location"></img>
</div>
Obviously, before I updated, the images were rendering with the PDF generation, so it's been confusing me as to what has changed since.
Text displayed as {EXAMPLE} is for my PHP file where the dompdf is ran to replace with variables that I send through AJAX from my client-side.

CodeIgniter library dompdf: How to download Pdf file directly, without asking for saving pdf in codeIgniter?

CodeIgniter library dompdf: How to download Pdf file directly, without asking for saving pdf in codeIgniter?
vendor/dompdf/dompdf/lib/class.pdf.php): failed to open stream: No such file or directory
class Pdfgenerator {
public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait")
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf", array("Attachment" => 0));
} else {
return $dompdf->output();
}
}
}

DOMPDF Doesn't Work if HTML is Already Present

I have a function that uses DOMPDF to generate a .pdf of whatever I set for the $html argument.
function generate_pdf($html){
//DOMPDF stuff
}
This works, but the problem I'm running into is that when I call this function from a page that already has HTML content, it fails.
Fails...
<?php
require_once '../header.php'; //This has HTML content in it.
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
This also fails...
<?php
echo 'stuff';
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Works...
<?php
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Is there any way around this?
Contents of function generate_pdf($html)
function generate_pdf($html){
//Get the necessary dompdf files
include_once DOMPDF_PATH . '/autoload.inc.php';
// instantiate and use the dompdf class
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($html);
// Render the HTML as PDF
$dompdf->render();
//Output the PDF
$dompdf->stream();
}
Note that the file in which this function lives has a namespace, so that's why $dompdf = new \Dompdf\Dompdf(); might appear wrong, but that line is working fine.
CHANGED ANSWER after full code was added to question:
I have the complete HTML content in an included file, no extra HTML before or after it (also no php echoing).
I made a mistake before - I mixed up the inclusion of the dompdf autoload and the content - but see below how it works in my case. This is the content of a php file, there is no function:
<?php
require_once '../../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->set_option('isPhpEnabled', true);
ob_start();
//Here I am getting a few variables (from the URL via GET) which I use in the included php file
include_once "your_content_in_one_file.php";
$html = ob_get_contents();
ob_end_clean();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($pdf_name);
?>
It ended up being related to the fact that ob_start(); had already been called somewhere else in the system. Using...
if (ob_get_level()){
ob_end_clean();
}
fixed my issue. Hope it helps someone else.

PDF data displayed but file not downloaded

I want to preview and download an order invoice pdf using this code :
public function generatePDFByIdOrder()
{
$order = new Order(1); //I want to download the invoice PDF of $order_id '1'
if (!Validate::isLoadedObject($order)) {
throw new PrestaShopException('Can\'t load Order object');
}
$order_invoice_collection = $order->getInvoicesCollection();
$this->generatePDF($order_invoice_collection, PDF::TEMPLATE_DELIVERY_SLIP);
}
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->render();
}
And calling it with the following code :
$order = new order();
echo $order->generatePDFByIdOrder();
I have the pdf's data printed on the browser console but not downloaded .
How can I manipulate that data to download a pdf file ?
PrestaShop use TCPDF.
Edit generatePDF in this way:
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->Output('name.pdf', 'I');
}
I guess you only have to set the proper headers before rendering the PDF with TCPDF like so :
header("Content-type:application/pdf");
But "downloading" a PDF will depend on what the user's browser settings. It might download them (in which case you'd have to set another header called Content-Disposition:attachment) or display it inside the browser.
We recommend you, to create a separate controller to render the PDF file and to always open that controller in a new tab. It will help you to have separate logic using DOMPDF library.
Invoice controller will be as follows (invoice.php)
include_once(_PS_MODULE_DIR_.'supercehckout/libraries/dompdf/dompdf_config.inc.php');
class SuperCheckoutInvoiceModuleFrontController extends ModuleFrontController
{
public function initContent()
{
parent::initContent();
$this->generateInvoice(ORDER_ID);
}
}
Note: SuperCheckout is the example module name.
generateInvoice() function will be as follows:
function generateInvoice($order_id)
{
$dompdf = new DOMPDF();
$html = utf8_decode(INVOICE_HTML);
$dompdf->load_html(INVOICE_HTML);
$dompdf->render();
}

Convert the same page to PDF

I want to convert the same document to PDF or I want to add post method in url just like:
$dompdf->loadHtml($_POST['name']);
Here is the code:
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml(file_get_contents('http://localhost/));
// (Optional) Setup the paper size and orientation
$dompdf->setPaper(array(0,0,850,2250), 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
?>
You're missing single quote in line if file_get_contents
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml(file_get_contents('http://localhost/'));
// (Optional) Setup the paper size and orientation
$dompdf->setPaper(array(0,0,850,2250), 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
?>

Categories