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.
Related
I am currently generating some pdfs with the dompdf library but every time a pdf is being generated my temp folder is being filled with this file and the images associated with it. I am trying to unlink or delete the pdf and any temp files generated with it but the following code is not giving the desired result. Does anyone know what else I can do in this situation?
I am using PHP and have installed the library with composer.
pdf code:
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//$dompdf->loadHtml('hello world');
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', true);
$dompdf -> loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
unlink($dompdf);
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
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);
...
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'm using a Google project API to convert HTML to PDF. Works great and is amazing. You can get it here if you want:
http://code.google.com/p/dompdf/
My question is as follows, the plugin does it's magic and I obtain something like this:
file_put_contents("nameOfPdf.pdf", $pdf);
How do I go from this to saving it to my server? I'm new to saving info to server, so please detail a bit.
Thanks.
MORE OF THE CODE HERE
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper( "a4" , "portrait");
$dompdf->render();
$dompdf->stream("xxxxx.pdf", array("Attachment" => false));
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it
// to the client.
file_put_contents("xxxxx.pdf", $pdf);
If I read the docs correctly, you need to remove this line:
$dompdf->stream("xxxxx.pdf", array("Attachment" => false));
It will then save it to disk without any output.
To specify a path, add it where you show the file name in the file_put_contents call.