DomPDF setting landscape orientation - codeigniter

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 :)

Related

Put JavaScript variable into PHP with AJAX?

Here's a some of my JavaScript
function myFunction() {
var finalMove = "a1";
$.post("index.php", {postFinalMove:finalMove});
];
Here is my PHP code. The commented lines are just two of the things I've tried.
<?php
session_start();
$db = mysqli_connect("localhost","myUsername","myPassword","abraDB");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//if (isset($_POST['postFinalMove']))
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
//if ($_POST['postFinalMove'])
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
if ($_POST['logout'])
{
session_start();
$_SESSION = array();
session_destroy();
}
mysqli_close($db);
?>
As soon as I uncomment some of the code I've tried, my entire webpage is blank with no errors and no source code, which makes this hard for me to debug. Javascript function fires just fine, its my index.php page that crashes. PHP code was working great until this. Thanks in advance
You should be able to do the following in your PHP script:
if(isset($_POST['postFinalMove'])){
echo $_POST['postFinalMove'];
}
This line is probably causing the error:
// $turn = ($db, $_POST['postFinalMove']);
That statement does not make any sense. What are you trying to store in $turn? If you want to store the $_POST['postFinalMove'] you would have:
$turn = $_POST['postFinalMove'];
Take a look at the GIF image and you will see for yourself..!
Replace your code with this line as and you are good to go then :
$turn = $_POST['postFinalMove'];
echo $turn;
It's just a simple Syntax Issue nothing more than that Dear..! :D

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; ?>

how to open pdf file to another tab in browser using codeigniter

im making currently making my thesis about a record management of our university secretary.. in which all papers inside the office will be scanned and uploaded in the system.. i am using codeigniter..one of the feature in my system is to view the pdf file in other window of the browser. but my problem is, when i click the title. only blank page will be displayed in the other tab.. can you help me solve this one?? here is my code
controller:
function viewMinutesFile(){
if(isset($_GET['id'])){
$id = $_GET['id'];
$file = $this->minutes_model->getFile($id);
$fp= fopen($file->path, "r");
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$file->filename."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' .filesize($file->path));
ob_clean();
flush();
while (!feof($fp)){
$buff = fread($fp,1024);
print $buff;
}
exit;
}
}
code to open the file: this is my syntax to be clicked by the user so that pdf file will be open in the new tab
File
index.php/admin/viewMinutesFile?
id=" target="_tab">
try this one with a static url. no need any extra words for that.
Show My Pdf
New Update
if its work for you then fetch pdf name from database and put the name in the view like
Show My Pdf
now in the controller
$this->load->helper('download');
if($this->uri->segment(3))
{
$data = file_get_contents('./file_path/'.$this->uri->segment(3));
}
$name = $this->uri->segment(3);
force_download($name, $data);
well, you could add a link to file with target="_blank", like
<a href="<?php echo base_url(). 'your_controller/viewMinutesFile'; ?>" target="_blank">
View Pdf
</a>
and in controller function:
function viewMinutesFile(){
....
$file = $this->minutes_model->getFile($id);
$this->output
->set_content_type('application/pdf')
->set_output(file_get_contents($your_pdf_file));
}
you can try this on your view :
Filename
and on your controller, you can try this, because this is works for me :
function viewfile(){
$fname = $this->uri->segment(3);
$tofile= realpath("uploaddir/".$fname);
header('Content-Type: application/pdf');
readfile($tofile);
}
hope this might help you...
Just create a link to a blank page and use this code in your controller:
public function myPdfPage(){
$url = base_url('assets/your.pdf');
$html = '<iframe src="'.$url.'" style="border:none; width: 100%; height: 100%"></iframe>';
echo $html;
}
Enjoy!
There is no any problem with your code you can open easily on next tab, like other pages only difference you have to change header description and it is make sure on your browser pdf reader add-ons are available, otherwise it will give you option to download.
You may just follow this.
<?php echo form_open_multipart('your_controller/your_function','target="_blank"') ;?>
//other input fields
<?php form_close();?>

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

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.

Resources