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.!
Related
I have a function that uses DOMPDF to generate a .pdf of whatever I set for the $html argument.
function generate_pdf($html){
//DOMPDF stuff
}
This works, but the problem I'm running into is that when I call this function from a page that already has HTML content, it fails.
Fails...
<?php
require_once '../header.php'; //This has HTML content in it.
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
This also fails...
<?php
echo 'stuff';
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Works...
<?php
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Is there any way around this?
Contents of function generate_pdf($html)
function generate_pdf($html){
//Get the necessary dompdf files
include_once DOMPDF_PATH . '/autoload.inc.php';
// instantiate and use the dompdf class
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($html);
// Render the HTML as PDF
$dompdf->render();
//Output the PDF
$dompdf->stream();
}
Note that the file in which this function lives has a namespace, so that's why $dompdf = new \Dompdf\Dompdf(); might appear wrong, but that line is working fine.
CHANGED ANSWER after full code was added to question:
I have the complete HTML content in an included file, no extra HTML before or after it (also no php echoing).
I made a mistake before - I mixed up the inclusion of the dompdf autoload and the content - but see below how it works in my case. This is the content of a php file, there is no function:
<?php
require_once '../../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->set_option('isPhpEnabled', true);
ob_start();
//Here I am getting a few variables (from the URL via GET) which I use in the included php file
include_once "your_content_in_one_file.php";
$html = ob_get_contents();
ob_end_clean();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($pdf_name);
?>
It ended up being related to the fact that ob_start(); had already been called somewhere else in the system. Using...
if (ob_get_level()){
ob_end_clean();
}
fixed my issue. Hope it helps someone else.
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 am working with DOMPDF currently, and I trying to work out why my PDF's are not being created correctly.
When I run the following code,
<?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->set_base_path($_SERVER['DOCUMENT_ROOT']);
$dompdf->output_html();
//$dompdf->load_html($html);
//$dompdf->set_paper("a4", "portrait" );
//$dompdf->render();
//$dompdf->stream($filename . ".pdf");
}
?>
In my browser I see the PDF content that I expect to be generated, however if I run this code,
<?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->set_base_path($_SERVER['DOCUMENT_ROOT']);
//$dompdf->output_html();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait" );
$dompdf->render();
$dompdf->stream($filename . ".pdf");
}
?>
I get no repsonse, where I would be expecting to get a download window. Is there a reason for this, I wonder if poorly formatted HTML could the problem?
Your CI plugin code should work on 1.7.x. Here's mind for your reference:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=true) {
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename);
} else {
$CI =& get_instance();
$CI->load->helper('file');
write_file($filename, $dompdf->output());
}
}