mPDF Landscape printing in codeigniter - php

I've been trying to print a PDF using mPDF and codeigniter. This PDF as well as all the other ones in the application print fine, but I can't get it to print landscape.
I've tried all the combinations of parameters as suggested in other questions, but none of them has had any effect on the printing. Therefore it has to be something wrong with the way I'm implementing the mPDF library.
My current controller:
$filename = $order_number;
$pdfFilePath = FCPATH."/downloads/orders/$filename.pdf";
$data['page_title'] = 'Order';
if (file_exists($pdfFilePath) == TRUE)
{
unlink($pdfFilePath);
}
if (file_exists($pdfFilePath) == FALSE)
{
ini_set('memory_limit','128M'); // boost the memory limit
$html = $this->load->view('generate_pdf', $data, true); // render the view into HTML
$this->load->library('pdf2');
$pdf2 = $this->pdf2->load();
$pdf2->WriteHTML($html); // write the HTML into the PDF
$pdf2->Output($pdfFilePath, 'F'); // save to file because we can
}
The mPDF library:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pdf2 {
function pdf2()
{
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}
function load($param=NULL)
{
include_once APPPATH.'/third_party/mpdf/mpdf.php';
if ($params == NULL)
{
$param = '"en-GB-x","A4-L","","",10,10,10,10,6,3';
//$param = '"en-GB-x","A4","","",10,10,10,10,6,3',L;
//$param = '"en-GB","A4-L"';
//$param = '"L"';
//$param = '\'en-GB-x\',\'A4-L\',\'\',\'\',10,10,10,10,6,3,L';
}
//echo $param;
return new mPDF($param);
}
}
Is mPDF ignoring the parameters or do I have it wrong?

Related

Image not displaying when generating a html page as pdf using mpdf library

I am generating a html page as pdf in codeigniter.. the image in html page is not displaying in pdf document
public function nmms_halltickets($categ = 1) {
// by kowshiga
try {
$school_id = $this->session->userdata('udise');
$name = date("Y") . 'NMMS Halltickets.pdf';
$this->db->where('exam', $categ)
->where('school_code',$school_id);
$result = $this->db->get('halltickets')->result();
foreach ($result as $ht) {
$data['halltickets'] = $this->Admin_Model->gethalltickets($ht->candidate_name);
$html[]['content']= $this->load->view('hall_ticket', $data, true);
// print_r($html);
// exit();
}
$this->common->generate_pdf($html, $name, $output_type = 'D', null, 10, null, 0, $orientation = 'P');
} catch (Exception $e) {
log_message('error', $e->getMessage());
}
}
Make sure your image path is correct first

How to export mySQL data to PDF in Codeigniter?

I'm new to Codeigniter and I need to export the mySQL data into a PDF file.
How can I do that?
Download library from here
Library explanation
After download extract folder You have you will find two files namely class.ezpdf.php/cezpdf.php and class.pdf.php. Now put these two .php files inside application/libraries. To make these work within CI you will have to a modification in the cezpdf.php/class.ezpdf.php. The modification is to be done in the include statement :
include_once(APPPATH . 'libraries/class.pdf.php');
Now go to your controller folder and there make a new file name generate.php and pdf_helper.php.
pdf_helper.php :
<?php
function prep_pdf($orientation = 'portrait')
{
$CI = & get_instance();
$CI->cezpdf->selectFont(base_url() . '/fonts');
$all = $CI->cezpdf->openObject();
$CI->cezpdf->saveState();
$CI->cezpdf->setStrokeColor(0,0,0,1);
if($orientation == 'portrait') {
$CI->cezpdf->ezSetMargins(50,70,50,50);
$CI->cezpdf->ezStartPageNumbers(500,28,8,'','{PAGENUM}',1);
$CI->cezpdf->line(20,40,578,40);
$CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));
$CI->cezpdf->addText(50,22,8,'PDF Tutorial');
}
else {
$CI->cezpdf->ezStartPageNumbers(750,28,8,'','{PAGENUM}',1);
$CI->cezpdf->line(20,40,800,40);
$CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));
$CI->cezpdf->addText(50,22,8,'PDF Tutorial');
}
$CI->cezpdf->restoreState();
$CI->cezpdf->closeObject();
$CI->cezpdf->addObject($all,'all');
}
?>
generate.php :
<?php
class Generate extends CI_Controller
{
function Generate()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
}
function create()
{
$this->load->library('cezpdf');
$this->cezpdf->ezText('PDF REPORT OF LOGIN TABLE', 12, array('justification' => 'center'));
$this->cezpdf->ezSetDy(-10);
$i=1;
$content="";
$fname="";
$query = $this->db->query('SELECT * FROM table_name');
$num = $query->num_fields();
$farr=array();
while($i <= $num){
$test = $i;
$value = $this->input->post($test);
if($value != ''){
$fname= $fname." ".$value;
array_push($farr, $value);
}
$i++;
}
$fname = trim($fname);
$fname=str_replace(' ', ',', $fname);
$this->db->select($fname);
$query = $this->db->get('table_name');
$result = $query->result();
foreach ($farr as $j)
{
$content= strtoupper($j)."\n\n";
foreach($result as $res){
$content = $content.$res->$j."\n";
}
$this->cezpdf->ezText($content, 10);
$this->cezpdf->ezStream();
}
}
In the above, first thing we do is load the R&OS library for use. Next we use the ezText() function to create a title for our document. This function takes the text it will display as the first argument, the size of that text and an optional array of additional configuration options.
After the whites pace we put the rest of the content for the document in a variable called $content and add it to our document using the ezText() function again. Finally, we create our document using the ezStream() function which actually creates the document and sends it to the users which prompts them to view/download the generated PDF document.

How to write a string in pdf file on specific path.. DOMPDF

I have a function which takes two parameters data(html),name. In the function i am trying to save the data in PDF file on my localhost/abc-folder. Unfortunately it runs fine but don't write in specific file or so..
Here is my code.
<?php
function pdf($data, $name) {
if (count($name) > 1) {
$name = "Orders";
} else {
$name = 'Order_'.$name[0]['order_id'];
}
$pdf = new DOMPDF;
$pdf->load_html($data);
$pdf->render();
$str=$pdf->output();
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/aabcd.pdf","wb");
fwrite($fp,$str);
fclose($fp);
}
?>

phppowerpoint : setPath() - Issue while having space in image name

I am using following code and getting issue if there is an space in image name. And the issue is basically file is not loading at popwerpoint slide.
like:
$shape->setPath("C:/image/abc1.jpg"); // Working fine
$shape->setPath("C:/image/abc 1.jpg"); // Not working due to space in filename
I'm using the PHPPowerPoint class for generating powerpoint slides.
How do I get this to work?
EDIT
For the benefit of roine
public function setPath($pValue = '', $pVerifyFile = true) {
if ($pVerifyFile) {
if (file_exists($pValue)) {
$this->_path = $pValue;
if ($this->_width == 0 && $this->_height == 0) {
// Get width/height
list($this->_width, $this->_height) = getimagesize($pValue);
}
} else {
throw new Exception("File $pValue not found!");
}
} else {
$this->_path = $pValue;
}
return $this;
}
Try:
$shape->setPath("C:/image/abc%201.jpg");
If that works, you can use a simple string replace.
Try
$file_path = "C:/image/abc 1.jpg";
$clean_file_path = str_replace(" ", "%20", "$file_path");
$shape->setPath($clean_file_path);

PHP - Loop through files on server and run script for each file

I will try to explain as well as possible what I'm trying to do.
I have a folder on a server with about 100 xml files. These xml files are content pages with text and references to attachment filenames on the server that will be pushed to a wiki through an API.
It's all working fine 1 XML file at a time but I want to loop through each one and run my publish script on them.
I tried with opendir and readdir and although it doesn't error it only picks up the one file anyway.
Could someone give me an idea what I have to do. I'm very new to PHP, this is my first PHP project so my code is probably not very pretty!
Here's my code so far.
The functions that gets the XML content from the XML file:
<?php
function gettitle($file)
{
$xml = simplexml_load_file($file);
$xmltitle = $xml->xpath('//var[#name="HEADLINE"]/string');
return $xmltitle[0];
}
function getsummary($file)
{
$xml = simplexml_load_file($file);
$xmlsummary = $xml->xpath('//var[#name="summary"]/string');
return $xmlsummary[0];
}
function getsummarymore($file)
{
$xml = simplexml_load_file($file);
$xmlsummarymore = $xml->xpath('//var[#name="newslinetext"]/string');
return $xmlsummarymore[0];
}
function getattachments($file)
{
$xml = simplexml_load_file($file);
$xmlattachments = $xml->xpath('//var[#name="attachment"]/string');
return $xmlattachments[0];
}
?>
Here's the main publish script which pushes the content to the wiki:
<?php
// include required classes for the MindTouch API
include('../../deki/core/dream_plug.php');
include('../../deki/core/deki_result.php');
include('../../deki/core/deki_plug.php');
//Include the XML Variables
include('loadxmlfunctions.php');
//Path to the XML files on the server
$path = "/var/www/dekiwiki/skins/importscript/xmlfiles";
// Open the XML file folder
$dir_handle = #opendir($path) or die("Unable to open $path");
// Loop through the files
while ($xmlfile = readdir($dir_handle)) {
if($xmlfile == "." || $xmlfile == ".." || $xmlfile == "index.php" )
continue;
//Get XML content from the functions and put in the initial variables
$xmltitle = gettitle($xmlfile);
$xmlsummary = getsummary($xmlfile);
$xmlsummarymore = getsummarymore($xmlfile);
$xmlattachments = getattachments($xmlfile);
//Build the variables for the API from the XML content
//Create the page title - replace spaces with underscores
$pagetitle = str_replace(" ","_",$xmltitle);
//Create the page path variable
$pagepath = '%252f' . str_replace("'","%27",$pagetitle);
//Strip HTML from the $xmlsummary and xmlsummarymore
$summarystripped = strip_tags($xmlsummary . $xmlsummarymore, '<p><a>');
$pagecontent = $summarystripped;
//Split the attachments into an array
$attachments = explode("|", $xmlattachments);
//Create the variable with the filenames
$pagefilenames = '=' . $attachments;
$pagefilenamefull = $xmlattachments;
//Create the variable with the file URL - Replace the URL below to the correct one
$pagefileurl = 'http://domain/skins/importscript/xmlfiles/';
//authentication
$username = 'admin';
$password = 'password';
// connect via proxy
$Plug = new DreamPlug('http://domain/#api');
// setup the deki api location
$Plug = $Plug->At('deki');
//authenticate with the following details
$authResult = $Plug->At('users', 'authenticate')->WithCredentials($username, $password)->Get();
$authToken = $authResult['body'];
$Plug = $Plug->With('authtoken', $authToken);
// Upload the page content - http://developer.mindtouch.com/Deki/API_Reference/POST:pages//%7Bpageid%7D//contents
$Plug_page = $Plug->At('pages', '=Development%252f' . $pagetitle, 'contents')->SetHeader('Expect','')->Post($pagecontent);
// Upload the attachments - http://developer.mindtouch.com/MindTouch_Deki/API_Reference/PUT:pages//%7Bpageid%7D//files//%7Bfilename%7D
for($i = 0; $i < count($attachments); $i++){
$Plug_attachment = $Plug->At('pages', '=Development' . $pagepath, 'files', '=' . $attachments[$i])->SetHeader('Expect','')->Put($pagefileurl . $attachments[$i]);
}
}
//Close the XMl file folder
closedir($dir_handle);
?>
Thanks for any help!
To traverse a directory of XML files you can just do:
$files = glob("$path/*.xml");
foreach($files as $file)
{
$xml = simplexml_load_file($file);
$xmltitle = gettitle($xml);
$xmlsummary = getsummary($xml);
$xmlsummarymore = getsummarymore($xml);
$xmlattachments = getattachments($xml);
}
I also recommend you make a minor adjustment to your code so simplexml doesn't need to parse the same file four times to get the properties you need:
function gettitle($xml)
{
$xmltitle = $xml->xpath('//var[#name="HEADLINE"]/string');
return $xmltitle[0];
}
function getsummary($xml)
{
$xmlsummary = $xml->xpath('//var[#name="summary"]/string');
return $xmlsummary[0];
}
function getsummarymore($xml)
{
$xmlsummarymore = $xml->xpath('//var[#name="newslinetext"]/string');
return $xmlsummarymore[0];
}
function getattachments($xml)
{
$xmlattachments = $xml->xpath('//var[#name="attachment"]/string');
return $xmlattachments[0];
}
Try changing your while loop to and see if that helps out better:
while (false !== ($xmlfile = readdir($dir_handle)))
Let me know.
EDIT:
By using the old way, there could have been a directory name that could have evaluated to false and stopped the loop, the way I suggested is considered the right way to loop over a directory while using readdir taken from here

Categories