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).
Related
How can I upload file from dompdf to database?
If I have controller like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sertifikat extends CI_Controller {
public function Sertifikat_pdf(){
$this->load->library('session');
$this->load->model('model_users');
$data['userData'] = $this->model_users->fetchUserData($this->session->userdata('user_id'));
$this->load->library('pdf');
ini_set('max_execution_time', 300);
$this->pdf->setPaper('A4', 'landscape');
$this->pdf->filename = "Sertifikat_magang.pdf";
$this->pdf->load_view('view_sertifikat', $data);
}
}
In that controller I just show file of pdf, but how can I upload the file of pdf to database?
This might do the trick
$dompdf->render();
$pdf = $dompdf->output();
$file_location = $_SERVER['DOCUMENT_ROOT']."App_folder_name/reports/".$pdf_name.".pdf";
file_put_contents($file_location,$pdf)
where reports is folder name where all PDF will be saved as output.
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 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);
}
}
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.!
I'm trying to customize the logging functionality of codeigniter. I found this forum thread which describes exactly what I need:
http://codeigniter.com/forums/viewthread/139997/
But I want to do that without altering core files/classes.
I"m trying to alter the log_message function. I've already extended the CI_Log class and that is working well, but now I'm trying to alter log_message() which resides in system/core/Common.php. It isn't actually a class so I can't extend it, it just appears to be a collection of useful functions. I tried redeclaring log_message() and placing it in application/core/Common.php, but that doesn't appear to work. Any ideas how I can go about this?
I believe that the function you actually want to alter is write_log(), correct me if I'm wrong. You can do this by extending the function within application\libraries\MY_Log.php.
I have the following script in my MY_Log.php which emails me any time an error is thrown:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Extends the logging class to send an email when an error is triggered */
class MY_Log extends CI_Log {
function __construct()
{
parent::__construct();
}
function write_log($level = 'error', $msg, $php_error = FALSE, $additional, $additional2)
{
if ($this->_enabled === FALSE)
{
return FALSE;
}
$level = strtoupper($level);
if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
{
return FALSE;
}
$filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
$message = '';
if ( ! file_exists($filepath))
{
$message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
}
if ( ! $fp = #fopen($filepath, FOPEN_WRITE_CREATE))
{
return FALSE;
}
$message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
#chmod($filepath, FILE_WRITE_MODE);
return TRUE;
}
}
If you're not looking to extend the write function, you will need to extend one of the other log functions.
A helper function could work here. You'd have to use it instead of log_message().
Here's what I did:
in application/helpers I created 'new_log_helper.php' (helpers have to end in _helper.php)
I wanted this to add log entries into the database and to track by process (that way I know where to look in the code).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Custom Logging Interface
*
* Add log entries to a log table on the db and track process name
*
* #access public
* #return void
*/
if ( ! function_exists('new_log')) {
function new_log($level = 'error', $message, $process, $php_error = FALSE)
{
if (config_item('log_threshold') == 0) {
return;
}
$CI =& get_instance();
$CI->load->model('new_log','logger', TRUE);
$CI->logger->add_event($process, $level, $message);
log_message($level, $process.': '.$message, $php_error);
}
}
Then when you need it use it like this:
$this->load->helper('new_log');
...
new_log('debug','Something happened','Stack overflow answer');
Anyway I know your issue was years ago but I was looking so maybe someone else is too.