dompdf generates a pdf empty / corrupted - php

I have a problem with dompdf. I'm trying to generate a PDF file through the values of a form.
A user enters the website, fills out the fields and clicks submit. In my email I must attach a PDF with all the form values.
I was able to integrate it into my code, but there is something wrong.
To submit, I get the mail with the PDF, but it is damaged. (Of 0BYTE.)
The code I've entered in includes / ajax.php.
This is the code:
// Generate PDF here
require_once("../dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->load_html($message);
$dompdf->render();
$pdf_content = $dompdf->output();
file_put_contents('../dompdf/ordine.pdf',$pdfoutput);
data users write in the form are in $message
this is my ajax.php

Try this
// Generate PDF here
require_once("../dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->load_html($message);
$dompdf->render();
$pdf_content = $dompdf->output();
file_put_contents('../dompdf/ordine.pdf',$pdf_content); //there was a typo here...
You pdf was correctly 0 bytes because you write an undefined variable to it.

Related

PDFs generated with Dompdf library filling up tempfolder

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

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.

Generate pdf with dompdf and send it as an attachment in email in cakephp

I am developing an application in cakephp in which i need to generate a pdf and send it as an email attachment. I have been able to create a pdf view using dompdf but I dont know how to save it as a file and send it as an attachment in email. Please tell me how to achieve this.
You can find how to save a file here: how to save DOMPDF generated content to file?
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('Brochure.pdf', $output);
Sending email is very straight forward with CakeEmailL. Take a look here:
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments
$Email = new CakeEmail();
$Email->from(array('me#example.com' => 'My Site'));
$Email->to('you#example.com');
$Email->subject('About');
$Email->attachments('/full/file/path/Brochure.pdf');
$Email->send('My message');

how to open print window after creating pdf in dompdf?

i converted my html to Pdf using dompdf and i want to print it. My problem is how to open my pdf file in print window instead of opening the download dialog?
include('dompdf/dompdf_config.inc.php');
$savein = 'pdfdirectory/';
$html = " my htmlcode here "
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents(($savein.'file.pdf'), $pdf);
$dompdf->stream('file.pdf');
You can't force the user to open the file in their browser, but you can specify options for the stream() method that hint to the browser whether or not to download the file. The default is to tell the browser that the file is an "attachment" which usually translates to a download. But if you modify the last line of your code as follows most browsers should display the PDF in the browser:
$dompdf->stream( 'file.pdf' , array( 'Attachment'=>0 ) );

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.

Categories