mPDF: Script does not work for codeIgniter - 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; ?>

Related

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 generated pdf is 0 bytes

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());
}
}
?>

Cannot display returned json data in CodeIgniter using jQuery autocomplete

I'm trying to move some AJAX code from a standalone file into a function in my controller and can't seem to get it to display the JSON data in the autocomplete function in the view. I've verified that the function does return JSON encoded data by visiting the function URL directly.
Here's my JavaScript from the head of my view:
<script type="text/javascript">
$(document).ready(function(){
var ac_config = {
source: <?php echo base_url() . 'admin/lookup_tmdb_movie_titles'; ?>
select: function(event, ui){
$("#title").val(ui.item.title);
$("#year").val(ui.item.year);
$("#imdb_link").val(ui.item.imdb_link);
},
minLength:2,
position: {
my: "left top",
at: "left bottom",
collision: "none",
of: "#title.ui-autocomplete-input.ui-autocomplete-loading"
}
};
$("#title").autocomplete(ac_config);
});
</script>
Here's the function in the admin controller (I'm just using hard-coded test data until I get this working correctly):
function lookup_tmdb_movie_titles()
{
$term = 'test';
// TEST DATA
$title['title'] = 'test';
$title['label'] = 'test (2012)';
$title['value'] = 'test';
$title['year'] = '2012';
$title['imdb_link'] = 'testlink';
$matches[] = $title;
// convert into JSON format and output
$matches = array_slice($matches, 0, 5);
$this->output->set_output( json_encode($matches) );
}
I've also tried outputting the JSON the following two ways, all of which work if I go to the function directly via the URL, but none of which work in the view itself.
print json_encode($matches);
and
$data['json'] = json_encode($matches);
$this->load->view('admin/json_view', $data);
I've looked at a lot of posts on StackOverflow and via Google (hence the different output methods above) but nothing has seemed to solve the issue yet.
first you should try this:
source: "<?php echo site_url(admin/lookup_tmdb_movie_titles); ?>"
then this could be:
function lookup_tmdb_movie_titles()
{
$term = 'test';
// TEST DATA
$title['title'] = 'test';
$title['label'] = 'test (2012)';
$title['value'] = 'test';
$title['year'] = '2012';
$title['imdb_link'] = 'testlink';
$matches[] = $title;
// convert into JSON format and output
$matches = array_slice($matches, 0, 5);
var_dump($matches); //comment this if everythings ok
// echo json_encode($matches); ->uncomment this if everythings ok
}
then open firebug launch the ajax request and check the response in console.

html is pass using jquery ajax but pdf is not generating in mpdf for codeigniter

I want to create a PDF in codeigniter using mPDF. My html is passed to the controller using jQuery AJAX. Data is coming to the $html But it is not working. It works fine when html is hard coded. Can any one help me please?
public function pdf($paper='A4')
{
$html = '';
$html = $this->input->POST('content');
$this->load->library('mpdf54/mpdf');
$CI->mpdf = new mPDF('utf-8',$paper);
$mpdf->debug = true;
$this->mpdf->WriteHTML($html);
$this->mpdf->Output();
exit;
}
Try grabbing all the POST vars by using
$html = $this->input->POST();
then echo those out to yourself before moving farther to be sure they are getting set.
public function pdf($paper='A4')
{
print_r($this->input->POST());
return;
}
This of course is only for testing but might help you to see why your $html var isn't getting set. Try that out and give us the results.

Magento, show image on custom frontend block

I've a module up and running. On the backend side I can upload images with several attributes and I'm saving image path and other information on a custom table. On frontend I already managed to detect when I need to show these images. I've used an Observer that adds a new layout handle when the displayed product is found on the custom created table. Then on that layout handle I call a phtml template file and from here I call a function inside a block that will be in charge of doing all the checks to be sure wich image to show.
My problem is that I can not find how to show these images. Everything I found references how to add image tag on a phtml file doing some extra verifications on the inserted php code. Buy on my case everything is on php code outside any phtml file.
My phtml file code:
<div>
<h3><?php $this->showCampaign(); ?></h3>
</div>
My block code for now:
<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
/**
* Shows a campaign banner according to the current selected product
* Seeks on main banners table to see if it is an image/html/product alone campaign
* Once knows the campaign type search on needed table the needed fields
*/
public function showCampaign() {
//Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
$product = Mage::registry('current_product');
$currentProdID = $product->getId();
if (Mage::registry('campaign_data') && Mage::registry('campaign_data')->getId()) {
$currentCampaign = Mage::registry('campaign_data');
} else {
return;
}
// get campaign type and show needed information
switch ($currentCampaign->getbanner_type()) {
case 1: // image
$myImgCollection = Mage::getModel('banners/bannersimg')->getCollection();
$myImgCollection->addfieldtofilter('bannerid',$currentCampaign->getID());
$currentImg = $myImgCollection->getFirstItem();
$currentImgPath = $currentImg->getimg_path();
//{{block type="catalog/product_new" product_id="16" template="catalog/product/view/your_new_page.phtml"}}
break;
case 2: // html
echo "html";
break;
case 3: // plain product url
echo "plain product url";
break;
}
}
}
As usual, couple of hours searching for an answer and when I post the question, found the answer. Just in case someone needs it: is as easy as creating an image html tag inside the code and return it to the template file. Sample code:
<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
public function showCampaign() {
...
$currentImgPath = $currentImg->getimg_path();
//build html to output to the template
$html .= "<div class=\"visual\">";
$html .= "<img src=\"" . $currentImgPath . "\" alt=\"\" />";
$html .= "</div>";
....
return $html;
}
And the phtml file code:
<div class="visual">
<?php echo $this->showCampaign(); ?>
</div>

Resources