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));
?>
Related
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.
I have a large html file.
With Mpdf, I output a pdf and it perfectly work but now I want to add header and footer to all pages.
I have only one section define by my html file.
Here mpdf is in landscape mode.
I try this :
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'orientation' => 'L'
]);
$mpdf->SetHTMLHeader('<h1>TEST</h1>');
$mpdf->SetHTMLFooter('<h1>TEST</h1>');
$mpdf->WriteHTML($html);
$mpdf->Output($namePDF,'F');
but nothing shown in pdf file...
Can't figure why
Try this
<?php
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetHeader('First section header');
$mpdf->SetFooter('First section footer');
$mpdf->WriteHTML('First section text...');
// Set the new Header before you AddPage
$mpdf->SetHeader('Second section header');
$mpdf->AddPage();
// Set the new Footer after you AddPage
$mpdf->SetFooter('Second section footer');
$mpdf->WriteHTML('Second section text...');
$mpdf->Output();
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.
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.
I'm having a problem when trying to render html to pdf using dompdf.
I have the code placed inside a class and after the procedure code I would like it to create a pdf of the html.
This is the code I have at the moment:
$templatefile = file_get_contents("templates/costreport.htm");
//fill headers
$templatefile = str_replace("%DATES%",stripslashes($startdate)." - ".stripslashes($enddate),$templatefile);
if ($siteid>0) {
$pdfname = "costreport-".$clientid.".pdf";
} else {
$pdfname = "costreport-".$clientid."-".$siteid.".pdf";
}
//insert into database
//Close and output PDF document
$pdfname = str_replace("/","-",$pdfname);
$pdfname = str_replace("\\","-",$pdfname);
//create pdf
// unregister Yii's autoloader
spl_autoload_unregister('my_autoloader');
// register dompdf's autoloader
require_once("../system/dompdf/dompdf_config.inc.php");
// register Yii's autoloader again
spl_autoload_register('my_autoloader');
$dompdf = new DOMPDF();
$dompdf->set_paper("A4","portrait");
$dompdf->load_html($templatefile);
//set_time_limit(240);
$dompdf->render();
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it
// to the folder.
file_put_contents('../tmp/'.$clientid.'/'.$pdfname, $pdf);
The code fails when the dompdf->render(); is in but once I take that line out the code works and the file is created but I cant open it if it hasn't rendered.
I've tried debugging the code and made the template is HTML valid but I'm at a loss now.
The error I am getting back is just boolen false when I run the script with the dompdf->render(); in it.
The problem was that I had this piece of code at the top of my class.
/set up path to new dir including dir to be created
chdir("../tmp/");
$newdirpath = getcwd()."/".$clientid;
//if an old invoice ticket pack exists unlink it so as not to get things confused with the new pack being created.
if (file_exists(getcwd()."/$clientid_$siteid_CostReport.pdf")) {
unlink(getcwd()."/$clientid_$siteid_CostReport.pdf");
}
//setup temp dir
if (!is_dir($newdirpath)) {
//create new dir if it doesn't already exist!
mkdir($newdirpath);
} else {
//dir already exists we need to empty it out first incase there's old stuff in there we don't want to duplicate data
//$this->removedirectory($newdirpath,false);
}