Dompdf generated pdf is 0 bytes - codeigniter

I am trying to save a pdf to disk.
I am using Dompdf and Codeigniter as below:
$this->load->helper('dompdf','file');
$this->load->helper('file');
$saveTo = './labels';
$data['id'] = $this -> uri -> segment(3);
$data['no_of_prints'] = $this -> uri -> segment(4);
$labelname = "Label". $data['id'] ."-". $data['no_of_prints'] . ".pdf";
$html = $this->load->view('label_view', $data, TRUE);
file_put_contents($saveTo."/".$labelname, pdf_create($html, $labelname));
While the labelname gets generated correctly. The file saved to disk reads 0 bytes and as expected I get an Adobe Reader cannot open this file ... error.
I am also using fancybox 2.0 to popup the pdf and when I right click on the popup,(the pdf does not show, i'm guessing pdf plugin issues) I am able to Save as a readable pdf that is not 0 bytes.
It is important for the application to save pdf to disk automatically. How do i save a readable pdf to disk?

I have found some old CI code (pre version 2) that used DomPDF, I had an extra parameter to write as a file.
Is this much different from yours?
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=FALSE, $orientation='portrait')
{
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper( array(0,0, 2.175 * 72, 3.375 * 72), "landscape" ); // credit card size
$dompdf->load_html($html);
$dompdf->render();
if ($stream)
{
$dompdf->stream($filename.".pdf");
}
else
{
$CI =& get_instance();
$CI->load->helper('file');
write_file("./uploads/$filename.pdf", $dompdf->output());
}
}
?>

Related

Converting html to pdf after clicking on download button in codeigniter

I have created a resume builder application.User enters all required details and i am storing all details in database. Then i am fetching those records and displaying it in different templates. I have created 3 templates. Now I want to give download as pdf and download as docx two download buttons. Once user clicks on any of buttons file will be downloaded in their chosen format. Either pdf or docx.
Is it possible to achieve in codeigniter?
i used Tcpdf
Controller Code is
the code written in controller download $this - > load - > library("Pdf");
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set auto page breaks
$pdf - > SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf - > setImageScale(PDF_IMAGE_SCALE_RATIO);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf - > AddPage();
// Set some content to print
$html = $this - > load - > view('resume/res_template1_view', array('left_sidebar' => 'sidebar/left_sidebar', ' right_sidebar' => 'sidebar/right_sidebar'), $data, TRUE);
// Print text using writeHTMLCell()
$pdf - > writeHTML($html, 0, 0, '', '', 0, 1, 0, true, '', true);
//$pdf->writeHTML($html, true, false, true, false, '');
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf - > Output('resume.pdf', 'D');
Probably you need to use some library in CI to achieve this. Please check below.
you can use Html2Pdf for CI3 library. https://github.com/shyamshingadiya/HTML2PDF-CI3
How to Use
There are 4 options you a required to set in order to create a PDF; folder, filename, paper and HTML
//Load the library
$this->load->library('html2pdf');
//Set folder to save PDF to
$this->html2pdf->folder('./assets/pdfs/');
//Set the filename to save/download as
$this->html2pdf->filename('test.pdf');
//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');
//Load html view
$this->html2pdf->html(<h1>Some Title</h1><p>Some content in here</p>);
The create function has two options 'save' and 'download'. Save will just write the PDF to the folder you choose in the settings. Download will force the PDF to download in the clients browser.
//Create the PDF
$this->html2pdf->create('save');
Advanced usage
Theres no reason why you can't build and pass a view to the html() function. Also note the create() function will return the saved path if you choose the 'save' option.
$data = array(
'title' => 'PDF Created',
'message' => 'Hello World!'
);
//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));
if($path = $this->html2pdf->create('save')) {
//PDF was successfully saved or downloaded
echo 'PDF saved to: ' . $path;
}
Please check above mentioned solution. Let me know if it not works.

mPDF: Script does not work for codeIgniter

I am trying to create pdf files using mPDF and CodeIgneter.
In the controllor I have following script:
$pdfFilePath = FCPATH."/pdf/report/test.pdf";
$data['page_title'] = 'Hello world'; // pass data to the view
if (file_exists($pdfFilePath) == FALSE) {
ini_set('memory_limit','32M');
$html = $this->load->view('pdf_output', $data, true); // render the view into HTML
$this->load->library('m_pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F');
}
I am getting this eror message and I cannot understand the reason.
Message: Undefined property: Welcome::$pdf
Put mpdf60 folder in Library.
In controller put following code.
public function doprint($book_id,$pdf=false)
{
$this->load->library('parser');
$page_data['sold_book'] = "Hello world data";
$output = $this->parser->parse('sold_book_detail_print',$page_data,true);
if ($pdf=='print')
$this->_gen_pdf($output);
else
$this->output->set_output($output);
}
//GENRATE PDF FILE
public function _gen_pdf($html,$paper='A4')
{
//this the the PDF filename that user will get to download
$pdfFilePath = "output_pdf_name.pdf";
//load mPDF library
$this->load->library('mpdf60/mpdf');
$mpdf=new mPDF('utf-8',$paper);
//generate the PDF from the given html
$mpdf->WriteHTML($html);
$mpdf->Output();
}
Put following code in view.
print shipping Detail
And create file.php in view.and write html in file.php
Ex:
<?php echo $sold_book; ?>

DomPDF setting landscape orientation

I am having trouble setting DOMPDF to use landscape orientation. Could anyone shed some light on my problem in the code below?
function delivary_voucher($arr = null, $orientation = 'landscape') {
$this->load->library('dompdf_gen');
$this->load->library('email');
$this->load->model('Emailtemplatemodel');
I am having the same problem. After searching a lot I have finally understood how to solve the issue. I am answering this just after my coding. Hope it could help you and also for a reference of SO.
Here is how I solve the problem:
I am using dompdf as a helper, so I put the folder dompdf into system>helpers folder.
Then I have created a helper file named dompdf_helper.php into the same folder containing the following code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function get_pdf($html, $paper_size='a4', $orientation='portrait', $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
// THE FOLLOWING LINE OF CODE IS YOUR CONCERN
$dompdf->set_paper($paper_size, $orientation);
$dompdf->render();
// added by Siddiqui Noor on 29 Nov, 15
if( !empty($filename) && $stream ) {
$dompdf->stream($filename.".pdf"); // to Download PDF
} else if ($stream) {
$dompdf->stream($filename.".pdf",array('Attachment'=>0));// to open in a window
} else {
return $dompdf->output();
}
}
?>
My system folder looks like the this
Finally I call the get_pdf function in my controller like:
$data = 'Your result set goes here';
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view($view, $data, true);
get_pdf($html,'a4', 'landscape');
Happy coding as code is poetry :)

DOMPDF + Codeigniter, library upgraded, but now not working

I have been using the old version of dom pdf in codeigniter for pdf generation and recently I changed the core dompdf directory to newer version dompdf-0.5.2.zip.
Below is the code that I've been using to connect to dompdf class:
class Pdf{
var $_dompdf = NULL;
/**
* Constructor Method
**/
function __construct(){
ini_set("memory_limit", "1G");
require_once("Dompdf/dompdf_config.inc.php");
if(is_null($this->_dompdf)){
$this->_dompdf = new DOMPDF();
}
}
/**
* HTML to PDF Conversion method
**/
function convert_html_to_pdf($_html, $_filename = '', $_stream = false, $_orientation = "portrait"){
$this->_dompdf->set_paper("a4", $_orientation);
$this->_dompdf->load_html($_html);
$this->_dompdf->render();
if($_stream){
$this->_dompdf->stream($_filename);
} else{
file_put_contents($_filename, $this->_dompdf->output());
}
}
}
Now when I try to generate pdf, it gives me following error:
Fatal error: Class 'DOMPDF' not found in D:\xampp\htdocs\app\application\libraries\Pdf.php on line 21
any solution would be appreciated
Thanx
Please first try with this example. Just store this in a file under htdocs directory of your xampp or whatever server you are using.
<?php
require_once("dompdf_config.inc.php");
$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();
$dompdf->stream("sample.pdf");
?>
Just change the dompdf directory path as per your requirement.
I solved this myself by uploading the dompdf version to the latest version, though it is beta, all seems to work perfectly now.

PDF generation using dompdf

Using dompdf i generate pdf file. I am successfully generate it .
But after generating it when I open it in Foxit reader it's open very small size in 51.79% mode. When i select 'Fit width' or 125% it show properly.
When I open the pdf by default it open 125% mode can it be possible ?
I used following function:
function pdf_create($html, $filename)
{
ini_set("memory_limit", "50M");
//define pdf store path
$invoice_pdf_path = ABSOLUTE_PATH;
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait");
$dompdf->render();
//$dompdf->stream("abc_out.pdf");
$pdf = $dompdf->output();
#file_put_contents($invoice_pdf_path . $filename . ".pdf", $pdf);
}
With dompdf 0.6 beta you can set the default view of the PDF by adding a meta tag in your HTML source code:
<meta name="dompdf.view" content="FitH" />
Possible values are "XYZ", "Fit", "FitH", "FitV", "FitR", "FitB", "FitBH", "FitBV", described here

Resources