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.
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 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'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 new to working with CI and I have a question. More like a design method suggestion. I want to create a base template for static pages. Basically I want to load doc type, head, and the body but nothing else. I want the content to be loaded by whatever class/function I call via the URL. I know I can do this with a HTML template and str_replace() but is there a better way or some fancy CI method I'm not familiar with?
Here is what I have so far and it works but it's not the best.
class Sandbox extends CI_Controller {
public function __construct() {
parent::__construct();
//echo "hello world?";
}
public function load($what){
if ($what == 'something'){
if (!file_exists('application/views/content/'.$what.'.php')){
// Whoops, we don't have a page for that!
show_404();
}
$data = array();
$this->load->view('templates/header', $data);
$this->load->view('content/'.$what, $data);
$this->load->view('templates/footer', $data);
}
else { // Default load
$data = array();
$data['message'] = 'Hello World';
//$this->load->view('templates/header', $data);
$this->load->view('content/sandbox', $data);
//$this->load->view('templates/footer', $data);
}
}
}
When I load the view method it seems to work but I had to add the doctype and head to the template. I would prefer to load those seperately so do not have to create multiple headers. What I am looking for is a way to specifically load parts of the page and whatever scripts/css that are required for that given page.
Thanks for any advice.
Your function names in the controller are references to the url eg:
www.ci.com/sandbox/load
The above url would load function load() inside the sandbox controller.
To create a template you can just create a view with the basics that you want and then pass the content to that view eg:
function home(){
$data['content'] = $this->load->view('home_content',,TRUE);
this->load->view('template', $data)
}
By using TRUE in the 3rd argument you are returning the data rather than displaying it.
Then in the template file just echo $content;
You could also create separate head files and other files to include in your template using the same method ie:
function home(){
$data['content'] = $this->load->view('home_content',,TRUE);
$data['head'] = $this->load->view('head',,TRUE);
this->load->view('template', $data);
}
You can also pass the data variable to views that you are returning ie:
$data['content'] = $this->load->view('home_content',,TRUE);
$data['admin_script'] = $this->load->view('admin_script',,TRUE);
$data['head'] = $this->load->view('head',$data,TRUE);
I hope this helps! :)
ALSO
If you want to load different pages you should create separate functions for them and create a new routing rule : Codeigniter: URI ROUTING
I'm using Symfony and mPDF.
I'm trying to integrate both but am running into some problems.
I need to capture the content of a view but can't see how to do it.
public function executePDF(sfWebRequest $request)
{
$this->object = $this->getRoute()->getObject();
require_once 'mpdf.php';
/* Example code from mPDF site */
$mpdf=new mPDF('win-1252','A4','','',20,15,48,25,10,10);
$mpdf->useOnlyCoreFonts = true; // false is default
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("Acme Trading Co. - Invoice");
$mpdf->SetAuthor("Acme Trading Co.");
$mpdf->SetWatermarkText("Paid");
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');
$this->setLayout(false);
$html = $this->getResponse()->getContent();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
}
With the above example, $html is returned as an empty string. I have a view template relating to this action (PDFSuccess.php) which accesses the $object and has the HTML that mPDF will use.
Thanks for any help.
As it is just now, when accessing this action it does open a PDF correctly, but there is no content in it.
Thanks
Haven't done this in this specific context but you could try:
$html = $this->getPartial('moduleName/partialName');
... where the template is a partial (_partialName) inside a given module. As it's a partial, there's no need to switch off the layout.
You can also pass variables to it:
$html = $this->getPartial('moduleName/partialName', array('var' => 'something'));
...
If that doesn't work, here's a question relating to email templates that contains an alternative way of doing this (see the accepted answer):
Email body in Symfony 1.4 mailer?