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);
Related
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();
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)
My s3 url is https://s3-us-west-2.amazonaws.com/bucket_name/test.png
its working fine on browser and showing image not found after including it on pdf file.
$pdf = new \DOMPDFModule\View\Model\PdfModel();
$pdf->setOption("filename", $filename);
$pdf->setOption("paperSize", "a4");
$pdf->setOption("paperOrientation", "landscape");
$dompdf = new \DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdfCode = $dompdf->output();
file_put_contents('path_to_pdf/test.pdf', $pdfCode);
If you use Laravel, you need to add this to your blade.
#php
use Illuminate\Support\Facades\Storage;
$content = Storage::get('vinylmaster.png');
$imageData = base64_encode($content);
$src = 'data:image/png;base64,' . $imageData;
#endphp
<img src="{{$src}}">
if you are not on Laravel, try Embedding images in a PDF file with PHP and 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();
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;