Error 500 while trying to save .pdf in server using DOMPDF - php

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);
?>

Related

How to save generated pdf in server using dompdf php lib

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.

HOW TO: convert HTML to PDF with PHP/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

smarty template and dompdf

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);
...

smarty with DOMPDF issue the result is not imported into pdf file?

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.

From file_put_contents to saved file on server

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.

Categories