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();
}
Related
I am using dompdf with cakephp 3.6 to generate prints from HTML to PDF
I have a function name test inside drivercontroller and i want to load a ctp file which is created inside src\Template\DriverDetails and filename is testpdf.ctp
Can u plz help me to suggest how to pass complete file.
Below is my code
public function test() {
$html = file_get_contents("testpdf.ctp");
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("invoice.pdf", array("Attachment" =>0));
exit(0);
}
src\Template\DriverDetails\testpdf.ctp
<html>
<body>
<table>
<tr><td align="center"><?php echo $id; ?></td></tr>
<tr><td align="center">Demo Data</td></tr>
</table>
</body>
</html>
You can try like that way.
public function test()
{
$data = "This can be accessible in view file";
$builder = $this->viewBuilder();
$builder->autoLayout(false);
// set your template path [don't use .ctp]
$builder->template('Users/add');
//Pass data into view file
$view = $builder->build(['data' => $data]);
//Render view file content and store into variable
$viewContent = $view->render();
var_dump($viewContent);
}
You can try this. Put this inside controller method. $view_output is a variable where content of ctp file stored. "elements" is ctp file to render. In youur case it is testpdf
/* Make sure the controller doesn't auto render. */
$this->autoRender = false;
/* Set up new view that won't enter the ClassRegistry */
$view = new View($this, false);
$view->set('text', 'Hello World');
$view->viewPath = 'elements';
/* Grab output into variable without */
$view_output = $view->render('box');
Here’s an example of storing the html of an element called ‘box’ with the variable of ‘text’ without the view displaying or outputting
Here is an example
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'm having a problem when trying to render html to pdf using dompdf.
I have the code placed inside a class and after the procedure code I would like it to create a pdf of the html.
This is the code I have at the moment:
$templatefile = file_get_contents("templates/costreport.htm");
//fill headers
$templatefile = str_replace("%DATES%",stripslashes($startdate)." - ".stripslashes($enddate),$templatefile);
if ($siteid>0) {
$pdfname = "costreport-".$clientid.".pdf";
} else {
$pdfname = "costreport-".$clientid."-".$siteid.".pdf";
}
//insert into database
//Close and output PDF document
$pdfname = str_replace("/","-",$pdfname);
$pdfname = str_replace("\\","-",$pdfname);
//create pdf
// unregister Yii's autoloader
spl_autoload_unregister('my_autoloader');
// register dompdf's autoloader
require_once("../system/dompdf/dompdf_config.inc.php");
// register Yii's autoloader again
spl_autoload_register('my_autoloader');
$dompdf = new DOMPDF();
$dompdf->set_paper("A4","portrait");
$dompdf->load_html($templatefile);
//set_time_limit(240);
$dompdf->render();
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it
// to the folder.
file_put_contents('../tmp/'.$clientid.'/'.$pdfname, $pdf);
The code fails when the dompdf->render(); is in but once I take that line out the code works and the file is created but I cant open it if it hasn't rendered.
I've tried debugging the code and made the template is HTML valid but I'm at a loss now.
The error I am getting back is just boolen false when I run the script with the dompdf->render(); in it.
The problem was that I had this piece of code at the top of my class.
/set up path to new dir including dir to be created
chdir("../tmp/");
$newdirpath = getcwd()."/".$clientid;
//if an old invoice ticket pack exists unlink it so as not to get things confused with the new pack being created.
if (file_exists(getcwd()."/$clientid_$siteid_CostReport.pdf")) {
unlink(getcwd()."/$clientid_$siteid_CostReport.pdf");
}
//setup temp dir
if (!is_dir($newdirpath)) {
//create new dir if it doesn't already exist!
mkdir($newdirpath);
} else {
//dir already exists we need to empty it out first incase there's old stuff in there we don't want to duplicate data
//$this->removedirectory($newdirpath,false);
}
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
I've created a form that allows users to create a pdf that has an unlimited number of pages, I've got SetAutoPageBreak set so that it continues onto a second page however I cannot get the pages created after the page break to continue to use the original template file. The basic code can be seen below.
require('fpdf.php');
require('fpdi.php');
$pdf = new FPDI('P','mm','A4');
$pageCount = $pdf->setSourceFile("source_file.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$pdf->SetTextColor(63,76,89);
$pdf->SetMargins(5,39,5,20);
$pdf->SetAutoPageBreak(true,22); //page created doesn't have template attached
$pdf->SetDrawColor(225,225,225);
$pdf->SetFillColor(248,248,248);
$pdf->SetLineWidth(1);
$pdf->SetXY(82, 40);
$pdf->MultiCell(165,5,$company.$block,0,L,false);
$pdf->SetXY(19, 45);
$pdf->MultiCell(165,5,$date.$block,0,L,false);
$pdf->Output();
Having looked around, this question is the closest I can find however I'm not sure whether it is even relevant: FPDF/FPDI UseTemplate
Thanks
Just place the imported page in the Header method:
class PDF extends FPDI
{
protected $_tplIdx;
public function Header()
{
if (null === $this->_tplIdx) {
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
}
$pdf = new PDF('P','mm','A4');
$pdf->AddPage();
...
...and everything should work as expected.
in addition to #JanSlabon`s answer: (i dont have the needed reputation to write a comment, so i´ll post this here, hope that´s ok)
If you only want to use a certain template for the first page and a different one for all other pages, you can do so as follows:
class PDF extends FPDI
{
protected $_tplIdx;
public function Header()
{
if (null === $this->_tplIdx) {
$this->setSourceFile('paper1.pdf');
$this->_tplIdx = $this->importPage(1);
} else {
$this->setSourceFile('paper2.pdf');
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
}
i know its not exactly what #Searlee was looking for, but maybe it helps someone else.