Send generated PDF with DomPdf as attachment with PHPMailer - php

I have been able to use dompdf to stream output but how can I make it send via email
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$image = "../img/logo2.png";
$html = '<h1>Hello World</h1>' ;
$dompdf->load_html($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
$domdpf->stream();
how can I make it send via PHPMailer
Thanks

Change your last line to $pdf = $dompdf->output();
Then you can use the addStringAttachment function in PHPMailer (https://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#method_addStringAttachment)

Related

Merge two pdfs with Ilovepdf and dompdf

I have a code to convert an html text into pdf and another to merge this pdf with a pdf that the user uploads, but I can't merge the two together, it downloads the converted pdf and not the merged one.
When I put just to merge with two files that the user uploads it works.
My code:
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$arquivo = $dompdf->stream();
$ilovepdf = new Ilovepdf('iLovePdfKey', 'iLovePdfKey');
// Create a new task
$myTaskMerge = $ilovepdf->newTask('merge');
// Add files to task for upload
$arquivo = $this->convertHello();
$file1 = $myTaskMerge->addFile('path to the file that the user upload');
$file2 = $myTaskMerge->addFile($arquivo);
// Execute the task
$myTaskMerge->execute();
// Download the package files
$myTaskMerge->download();
$dompdf->stream() sends the rendered PDF to the browser. As such you can't access the generated PDF that way. You have to capture the output and save to a file.
Based on your sample code, something like this:
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->render();
$arquivo = $dompdf->output();
$tmp = tempnam(sys_get_temp_dir(), "pdf")
file_put_contents($tmp, $arquivo);
$ilovepdf = new Ilovepdf('iLovePdfKey', 'iLovePdfKey');
$myTaskMerge = $ilovepdf->newTask('merge');
$file1 = $myTaskMerge->addFile('path to the file that the user upload');
$file2 = $myTaskMerge->addFile($tmp);
$myTaskMerge->execute();
unset($tmp);
$myTaskMerge->download();

Unable to downlaod pdf using DOMPDF

I checked plenty of resources, but my issue could not be resolved. I tried to generate to PDF by including PHP File. But now I stuck in "Unable to stream pdf: headers already sent" error. I also compress my code and also remove white spaces.
Here is my code.
<?php
//ob_start();
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$return = include 'download_pdf.php';
$return = stripslashes($return);
$dompdf->loadHtml($return);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', '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));
?>
Try to using output buffering
replace
$return = include 'download_pdf.php';
with
ob_start();
include 'download_pdf.php';
$return = ob_get_contents();
ob_end_clean();

Print button for generated dompdf output

HTML
<p style="color:red;font-size: 30px;">sfsdfsdfdsfsdfdsfdsf</p>sdgfhgfhgfhg
PHP
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
//to put same file html
/*$html1 =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';*/
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//to put other html file
$html = file_get_contents('index.html');
$dompdf->loadHtml($html);
//$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Legal', '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));
//$output = $dompdf->output();
//file_put_contents("pdfs/file.pdf", $output);
?>
I am using dompdf to convert my html to pdf, what I want to do here is a Print button in index.html page, when I click on that print button the generated pdf should download to user's system.
How can I achieve that
This shoud work:
<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
if (isset($_GET['action']) && $_GET['action'] == 'download') {
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//to put other html file
$html = file_get_contents('index.html');
$html .= '<style type="text/css">.hideforpdf { display: none; }</style>';
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Legal', 'Landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>1));
}
?>
<a class="hideforpdf" href="generatepdf.php?action=download" target="_blank">Download PDF</a>
Edit: moved the use-part to the top of the code.
Edit 2: added a class to hide the download button.

dompdf render pdf doesnt work on codeigniter

This code works but the resulting file is being opened like HTML for me not as a pdf or download pdf. How can I fix that?
<?php
require_once("dompdf/dompdf_config.inc.php");
//var_dump("dompdf/dompdf_config.inc.php");
$tiket = $_GET["tiket"];
$file = $this->load->view('admin/report/fm-it-01',$tiket);
$dompdf = new DOMPDF();
//var_dump($dompdf->load_html($file));
$dompdf->load_html_file($file);
$dompdf->render();
$dompdf->stream("sample.pdf"); ?>
Try $dompdf->load_html($file); instead of $dompdf->load_html_file($file);

unable to load pdf in browser dompdf

We are trying to create html to pdf using DOMPDF. When we are saving this to our browser it is created correctly. For creation we are using this function
function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){
$path='dompdf/';
/*$rndNumber=rand();
$filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
$dompdf=new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($path.$filename, $output);
return $filename;
}
But when we are trying to show pdf in browser without saving on server its showing "Failed to load PDF".
for this we are using this code
try{
$dompdf=new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
//$output = $dompdf->output();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);
}catch(exception $e){
print_r($e);}
Please tell us whats wrong in second code?
Try this and download the latest stable version of dompdf from github
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isRemoteEnabled', TRUE);
// instantiate and use the dompdf class
$dompdf = new Dompdf($options);
$context = stream_context_create([
'ssl' => [
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed'=> TRUE
]
]);
$dompdf->setHttpContext($context);
$html = file_get_contents("html.html");
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', '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("codexworld",array("Attachment"=>0));
?>

Categories