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));
?>
Related
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)
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();
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.
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);
I'm making a download of PDF with DOMPDF. Everything is working fine. The PDF is downloading and opens well in Adobe Reader. But at closing the window, Adobe Reader is asking me if I want to save the PDF file where I didn't change anything in. What is the possible problem that cause this problem?
$filename = $naam.' Factsheet.pdf';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
file_put_contents('pdf/'.$filename, $dompdf->output( array("compress" => 0) ));
if ( !headers_sent() ) {
$dompdf->stream($filename, $options);
}
Can please anybody help me with this problem?
Sorry for grave digging, but i had the same issue, however i fixed my code but i dont know how, here is the working code
$filename = $sm_number . "_" . date('Ymd') .'_OK_CALCOOLING.pdf';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($content);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$array = array(
"compress" => 0,
"Attachment" => 0
);
$dompdf->stream($filename, $array);
Make sure you add ob_get_clean() when declaring your $html variable.
$html = ob_get_clean(); //ADD THIS LINE WHEN DECLARING HTML VARIABLE
$html .= 'My content here';
$dompdf = new Dompdf();
$dompdf->getOptions()->setIsFontSubsettingEnabled(true);
$dompdf->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("filename.pdf", array("compress" => 0, "Attachment" => false));
exit;