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.
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 want to preview and download an order invoice pdf using this code :
public function generatePDFByIdOrder()
{
$order = new Order(1); //I want to download the invoice PDF of $order_id '1'
if (!Validate::isLoadedObject($order)) {
throw new PrestaShopException('Can\'t load Order object');
}
$order_invoice_collection = $order->getInvoicesCollection();
$this->generatePDF($order_invoice_collection, PDF::TEMPLATE_DELIVERY_SLIP);
}
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->render();
}
And calling it with the following code :
$order = new order();
echo $order->generatePDFByIdOrder();
I have the pdf's data printed on the browser console but not downloaded .
How can I manipulate that data to download a pdf file ?
PrestaShop use TCPDF.
Edit generatePDF in this way:
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->Output('name.pdf', 'I');
}
I guess you only have to set the proper headers before rendering the PDF with TCPDF like so :
header("Content-type:application/pdf");
But "downloading" a PDF will depend on what the user's browser settings. It might download them (in which case you'd have to set another header called Content-Disposition:attachment) or display it inside the browser.
We recommend you, to create a separate controller to render the PDF file and to always open that controller in a new tab. It will help you to have separate logic using DOMPDF library.
Invoice controller will be as follows (invoice.php)
include_once(_PS_MODULE_DIR_.'supercehckout/libraries/dompdf/dompdf_config.inc.php');
class SuperCheckoutInvoiceModuleFrontController extends ModuleFrontController
{
public function initContent()
{
parent::initContent();
$this->generateInvoice(ORDER_ID);
}
}
Note: SuperCheckout is the example module name.
generateInvoice() function will be as follows:
function generateInvoice($order_id)
{
$dompdf = new DOMPDF();
$html = utf8_decode(INVOICE_HTML);
$dompdf->load_html(INVOICE_HTML);
$dompdf->render();
}
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);
}
}
Just a quick question. Anyone know if its possible to load multiple views from codeigniter to mpdf controller and convert it to pdf?
my controller :
`
<?php
class C_Pdf extends CI_Controller {
private $params = array();
function __construct() {
parent::__construct();
$this->params['set_tag'] = array();
$this->params['set_css'] = array();
$this->params['set_js'] = array();
$this->params['title_auto'] = true;
$this->params['title'] = '';
}
public function index(){
//this data will be passed on to the view
$data['the_content']='mPDF and CodeIgniter are cool!';
$data['other']="siema heniu cos przeszlo";
//load the view, pass the variable and do not show it but "save" the output into $html variable
$this->load->view('common/default_header',$this->params);
$html=$this->load->view('reservation/complete', $data,true);
$this->load->view('common/default_footer');
//this the the PDF filename that user will get to download
//load mPDF library
$this->load->library('m_pdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->m_pdf->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();
}
}?>
i want to add footer and header from other view.
Just prefetch the header and footer templates into variables and pass their parsed strings to the main view ,like this:
$data['header'] = $this->load->view('common/default_header',$this->params, true);
$data['footer'] = $this->load->view('common/default_footer', true);
$html = $this->load->view('reservation/complete', $data, true);
Notice the third parameter to true so that you get the view in a string instead of sending it to output (most commonly the client's browser).
Then in your main template, place the $header and $footer variables wherever you need.
I want to ask how can I convert excel to pdf?
I want to put 5 excel files and after I click upload in convert to 1 pdf?
for now I use in this code:
/**
* Example: DOMPDF
*
* Documentation:
* http://code.google.com/p/dompdf/wiki/Usage
*
*/
public function index() {
// Load all views as normal
$this->load->view('test');
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("welcome.pdf");
}
the library:
class Dompdf_gen {
public function __construct() {
require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}
first of things , you have to dump all excel to php array or var , after that you can create pdf as you want , this is simple php script for convert excel to pdf , you can use it and convert it to subclass of codeigniter as well
please check this url
PHP read excel file
when you use that class your excel data will be with you so you can use another class for pdf and this class will help you
codeigniter create PDF file
hopeful this is helpful for you .
I do not understand the problem.
like in this code:
$this->load->helper(array('dompdf', 'file'));
// page info here, db calls, etc.
$html = $this->load->view('home', $data, true);
pdf_create($html, 'filename')
or
$data = pdf_create($html, '', false);
write_file('name', $data);
how i put after i upload excel file and cover it to pdf?
this function is only for HTML