I am using smarty template to generate a page on-the-fly that consists of user-submitted form data and uploaded images. I can get to display all the contents as expected. I want the same to be output as PDF. The template is named index.tpl, and is located inside the template folder. I can't figure out how to put these two together. Any help would be appreciated, thanks. I tried the following, but doesn't work. There is no output.
require_once("dompdf/dompdf_config.inc.php");
$html = $smarty->fetch('index.tpl');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("pdf_file.pdf");
When I checked the error_log, I noticed "PHP Fatal error: Class 'DOMPDF' not found" line. HOWEVER, when I created a simple file as shown below, I get it working perfectly (PDF is generated).
require_once("dompdf/dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("pdf_file.pdf");
What is happening here? Why the difference?
I think your answer is here http://www.smarty.net/docsv2/en/api.fetch.tpl
// capture the output
$output = $smarty->fetch('index.tpl');
$tmpfile = tempnam("/tmp", "dompdf_");
file_put_contents($tmpfile, $output);
...
Related
I am using dompdf php lib for generating pdf.
Right now it ask user to save the file in their machine.
Code i tried so far:
<?php
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream($filename.'.pdf');
$output = $dompdf->output();
file_put_contents($filename.'.pdf', $output);
?>
Give "write" permission to the directory you need to put the file.
I have read lots of questions about this topic, but none of their solutions have been useful to me. I am trying to upload a pdf file created by DOMFILE to my server, but I get 500 error.
I have looked in my access_log and error_log but there is nothing. I have also checked permissions through Filezilla, and they are correct (please, tell me if I have done it well):
I have made my code very simple just to focus on this problem:
<?php
require_once "dompdf/autoload.inc.php";
$html =
'<html><body>'.
'<p>Some data to be transformed to pdf.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper( "a4" , "portrait");
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents("test.pdf", $pdf);
?>
I need your help to fix this:
My current code is this:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$html = 'Insert full HTML content';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codexworld",array("Attachment"=>0));
?>
This is the error a get:
Why if I have exactly the same code like the basic example, am I getting this error? What am I missing? I don't find in my folder the Autoloader.php, where do I have to get that file?
Well apparently it's an issue after domPdf has moved to Github. It seems that php-font-lib library doesn't exist. So one solution is to manually download it:
Go to https://github.com/PhenX/php-font-lib and download the library.
In the zip file, take the contents of the src/FontLib/ folder and paste that into the folder lib/php-font-lib.
or you can check this answer here
iam in the middle of development. i have to create a PDF file from the result. please review my code, the PDF is created but its in empty.
Code:
case 'pre-test-pdf':
ob_start();
require_once("includes/dompdf/dompdf_config.inc.php");
$smarty->display('default/content/pdf/pdf-view-pre-test.tpl');
$html = ob_get_contents();
ob_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
break;
The Result of this page has to be a PDF
$smarty->display('default/content/pdf/pdf-view-pre-test.tpl');
Expecting a solution soon.
You can write $html = $smarty->fetch('default/content/pdf/pdf-view-pre-test.tpl'); This will give the html layout of your file which is stored in $html and that can be used to create PDF.
I try to convert dynamic php database file to pdf.
I try with DOMPDF, but I have a problem with defining the string. I'll explain:
Here is a 'hello world' script for dompdf:
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello_world.pdf");
The thing is that instead of a simple
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>'
I have a long php file full of functions and sql queries. because of it I have inside it many ",',; e.t.c signs. I also have a javascript dynamic chart (jqplot) in this page.
So instead of pdf file I get errors errors errors...
Does anyone has a solution for this?
I will really appreciate any answer, and will be really really thankful for a solution...
First you'll need to generate HTML from your PHP, then pass it to DOMPDF:
<?php
require_once("dompdf_config.inc.php");
ob_start();
require_once("path/to/input/file.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("file.pdf");
?>
You can also do a regular HTTP request:
<?php
require_once("dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html_file('http://example.com/file.php');
$dompdf->render();
$dompdf->stream("file.pdf");
?>
If you need JavaScript support, try wkhtmltopdf, it's based on Webkit and does it's work perfectly.
You can use ob_start and ob_get_contents to run PHP code and capture the output as a string.
For the JavaScript chart, though, you're out of luck. DOMPDF is pretty smart, but it's not that smart. You'll need to either use a non-JavaScript chart solution, do without the charts, or use a web browser to generate the PDF.
I am not sure why you need to generate HTML to build a PDF in the first place but as others have suggested, build out your PHP script and then use something like FPDF or TCPDF.
They both build PDFs just fine and can take HTML input.
Try This ...
<?php
ob_start();
require_once("dompdf_config.inc.php");
$file = file_get_contents('http://example.com/file.php');
$dompdf = new DOMPDF();
$dompdf->load_html($file);
$dompdf->render();
$dompdf->stream("filename.pdf");
?>