I want to use [mpdf][1] library in codeigniter framework .
now,I want to use utf-8 for persian language .
$this->load->library('pdf');
$pdf = $this->pdf->load();
$html = iconv("utf-8","UTF-8//IGNORE",$html);
$pdf->SetDirectionality('rtl');
$pdf->mirrorMargins = true;
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="😉" draggable="false" class="emoji">
$pdf->WriteHTML( $html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F'); // save to file because we can
the result of pdf file :
//libraries/pdf.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class pdf {
function pdf()
{
$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 = '"fa","A4","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}
Related
I tried to download a PDF file using mpdf in Codeigniter3.
This is my code which I have used for PDF generation
function download_PDF(){
//load mPDF library
$this->load->library('pdf');
$pdfFilePath ="statement-".time().".pdf";
$html=$this->load->view("transactionStatement", true);
$pdf = $this->pdf->load();
$stylesheet = '<style>'.file_get_contents('assets/css/style.css').'</style>';
$pdf->WriteHTML($stylesheet,1);
$pdf->WriteHTML($html,2);
$pdf->Output($pdfFilePath, "D");
exit;
}
This is my Library file
class Pdf {
function Pdf()
{
$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","","",10,10,10,10,6,3';
}
return new mPDF();
}
}
The above code working fine in local server but it is not working in live server.
in live server the url is like this file:///C:/Users/Nibs2/Downloads/statement-1556794021.pdf and it says 'Failed to load PDF document.'
I use PHP 5.6 in my localhost and this script for generating pdf is working well. But in the server which is using PHP 5.1 I got that error. Here is the script in application/libraries/pdf.php.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once (dirname(__FILE__)) . '/dompdf/dompdf_config.inc.php';
class Pdfgenerator {
function __construct() {
$this->ci =& get_instance();
$this->dompdf = new DOMPDF();
}
function generate($data){
$html = $this->ci->load->view($data['template'],$data,true);
$paper_size = isset($data['paper_size']) ? $data['paper_size'] : 'A4';
$orientation = isset($data['orientation']) ? $data['orientation'] : 'potrait';
$this->dompdf->set_paper($paper_size,$orientation);
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream($data['filename'].'.pdf',array('Attachment'=>0));
}
}
What should I change to make it works in PHP 5.1?
Can someone help me? I'm using dompdf in codeigniter, i was trying all solution and then no one can resolv my problem. When using localhost there was no problem, after hosting there was an error Unable to stream pdf: headers already sent please help me i'm still leraning.
Here's my controller Laporanpdf.php
`<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Laporanpdf extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_laporan');
$this->load->library('dompdf_gen');
}
public function index(){
$data['title'] = 'Cetak Laporan Kinerja'; //judul title
if($this->session->userdata('level')=='1-Admin'){
$data['pengguna'] = $this->model_laporan->pengguna_admin(); //query model
$data['kinerja'] = $this->model_laporan->kinerja_admin(); //query model
$data['awal'] = strtotime($this->input->get('awal'));
$data['jbtn_atasn'] = $this->input->get('jbtn_atasn');
$data['nip_atsn'] = $this->input->get('nipeg');
$data['pangkat_atsn'] = $this->input->get('pngkat');
$data['gol_atsn'] = $this->input->get('gol');
}else{
$data['pengguna'] = $this->model_laporan->pengguna_else(); //query model
$data['kinerja'] = $this->model_laporan->kinerja_else(); //query model
$data['jbtn_atasn'] = $this->input->get('jbtn_atasn');
$data['nip_atsn'] = $this->input->get('nipeg');
$data['pangkat_atsn'] = $this->input->get('pngkat');
$data['gol_atsn'] = $this->input->get('gol');
}
if($this->session->userdata('level')=='1-Admin'){
if ($this->model_laporan->kinerja_admin()==null){
//pesan error
echo "<script>alert('Kinerja dengan tanggal tersebut tidak tersedia. Silahkan masukan tanggal yang sesuai.');
window.location='/kinerja/index.php/kinerja';
</script>";
exit();
} else {
$this->load->view('v_cetak', $data, TRUE);
}
}else{
if ($this->model_laporan->kinerja_else()==null){
//pesan error
echo "<script>alert('Kinerja dengan tanggal tersebut tidak tersedia. Silahkan masukan tanggal yang sesuai.');
window.location='/kinerja/index.php/kinerja';
</script>";
exit();
} else {
$this->load->view('v_cetak', $data, TRUE);
}
}
$paper_size = 'A4'; //paper size
$orientation = 'potrait'; //tipe format kertas
$html = $this->output->get_output();
$this->dompdf->set_paper($paper_size, $orientation);
//Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("laporan.pdf", array('Attachment'=>0)); //preview pada browser
}
}`
Here's my Dompdf_gen.php I put in application/library
`<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dompdf_gen {
public function __construct() {
require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php'; // #version 0.5.1.htischer.20090507
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}`
Please check in your error logs. If any warnings or other output are generated before dompdf tries to render the output then it will fail. Address any errors you find in the logs and try again.
Other likely causes might be an empty line after a closing '?>' tag in one of your other classes. Avoid using the closing php tags in any of your models/controllers/libraries as they are unnecessary and can cause issues like this.
If you are not able to find the issue that is causing this, as a last ditch attempt you can try using output buffering to stop any leaks (See: http://php.net/manual/en/book.outcontrol.php).
I'm having some little trouble getting the Codeigniter mPDF library to work. Below is the class in 'application/libraries':
class mpdf {
function mpdf() {
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}
function load($param = NULL) {
include_once APPPATH . 'third_party/m_pdf/mpdf.php';
if ($params == NULL) {
$param = '"en-GB-x","A4","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}
while my function that is supposed to print out the pdf is as below:
//pdf
public function outputPDF() {
//this data will be passed on to the view
$data['the_content'] = 'mPDF and CodeIgniter are cool!';
//load the view, pass the variable and do not show it but "save" the output into $html variable
$html = $this->load->view('pdf_output', $data, true);
//this the the PDF filename that user will get to download
$pdfFilePath = "the_pdf_output.pdf";
//load mPDF library
$this->load->library('mpdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->mpdf->load();
//generate the PDF!
$pdf->WriteHTML($html);
//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output($pdfFilePath, "D");
}
Below is the error:
Fatal error: require_once(): Failed opening required
'X:/xampp/htdocs/.../application/third_party/m_pdf/config_cp.php'
(include_path='.;X:\xampp\php\PEAR') in
X:\xampp\htdocs...\application\third_party\m_pdf\mpdf.php on line 39
Kindly assist
I had the same problem with the library. Now I got it working on my CodeIgniter application.
In order to make it work, I had to put the library folder in the third_party folder (inside application folder). Then I made an autoloader. Inside libraries folder I created this file called Pdf.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pdf {
function Pdf()
{
$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","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}
Then, you can load the library as CodeIgniter expects:
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F');
$html is the string containing the html view you want to export.
$pdfFilePath is the string path where we are saving our pdf.
I have downloaded dompdf libraries from github https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf
Controller function
function pdf()
{
$this->load->helper('dompdf');
// page info here, db calls, etc.
$this->data['query'] = $this->my_model->dashboard_db();
$this->data['queryCmt'] = $this->my_model->dashboard_comment_task_db();
$html = $this->load->view('home',$this->data, true);
$fn = 'file_pdf';
$data = pdf_create($html, $fn, true);
write_file($fn, $data);
//if you want to write it to disk and/or send it as an attachment
}
Dom pdf Helper function
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf");
} else {
return $dompdf->output();
}
}
?>
This gives me a server error, but if i remove a pdf_create() in controller in generate one pdf file. Can anyone help one this
In your home.php (view) file,
Remove <thead> and <tbody> tags and remove space between <html><head> , </head><body> and </body></html>
It will works fine.!