CakePHP 1.3 how to render views before displaying content - php

I'm using CakePHP 1.3. I want to get all html content (table,div..) of my page (.ctp) with CSS to concatinate with other file pdf,
I tried to use something like:
$view = new View(null, false);
$view->set('timecard','masque');
$view->viewPath = 'pdf';
$output = $view->render('pdf', 'pdf');
OR
$view = new View($this, false);
$view->set(compact('timecard', 'masque'));
$html = $view->render('view_name');
thank's for helping ..

Please test this code:
/* 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 the view actually outputting! */
$view_output = $view->render('box');

Related

How to add watermark to every page using mpdf in laravel

I'm using niklasravnsborg/laravel-pdf package in laravel to generate a pdf. In this code i added watermark but that watermark is coming only on the last page though i wanted that to be in every page.
$pdf = PDFm::loadHtml($result->document);
$pdf->mpdf->SetWatermarkText('DRAFT');
$pdf->mpdf->showWatermarkText = true;
return $pdf->download('hdtuto.pdf');
From the documentation, it should work. Could you however, give the following a try?
$config = ['instanceConfigurator' => function($mpdf) {
$mpdf->SetWatermarkText('DRAFT');
$mpdf->showWatermarkText = true;
}]
PDF::loadHtml($result->document, $config)->download('hdtuto.pdf');
In this case, we are initialising the PDF instance with the right configuration instance, instead of doing it after loading the HTML.
mPDF doc states that
The watermark will be added to each page when the Footer is printed if the variable $showWatermark is set to 1 or true.
Try to add header/footer to your PDF and set corresponding flags
If any one trying to use blade file instead of direct html document, you can use view function which will return html content to the loadHtml method.
$config = ['instanceConfigurator' => function($mpdf) {
$mpdf->SetWatermarkText('DRAFT');
$mpdf->showWatermarkText = true;
}]
$pdf = PDF::loadHtml(view('path.to.blade_file', $blade_data), $config);
return $pdf->stream('DocumentName.pdf');
if you want to use an image as watermark
$config = ['instanceConfigurator' => function ($mpdf) {
$mpdf->SetWatermarkImage(asset('path/to/image_file'));
$mpdf->showWatermarkImage = true;
// $mpdf->watermarkImageAlpha = 0.2; // image opacity
// dd($mpdf) // show all attributes
}];
$pdf = PDF::loadHtml(view('path.to.blade_file', $blade_data), $config);
return $pdf->stream('DocumentName.pdf');

How to load html file inside cakephp using dompdf

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

How to load multiple views to mpdf

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.

Render joomla component inside same view

So i'm working with com_content component view,
and i need to load related items related to
the one that's beeing viewed.
Is it possible to do that?
This is what i have so far:
$com = JPATH_SITE.DS.'components'.DS.'com_content';
if (!class_exists('ContentController')) require($com.DS.'controller.php');
$config['base_path'] = $com;
$cont = new ContentController($config);
JRequest::setVar('view', 'categories');
$lang =& JFactory::getLanguage();
$lang->load('com_content', JPATH_SITE);
$cont->display();
I don't know if this is exactly how its supposed to be, but wait for any help if possible.
Thanks
Here is the solution, for anyone in my situation:
// Path to wanted component
$com = JPATH_SITE.DS.'components'.DS.'com_content';
// Include controller, if its not already include
if (!class_exists('ContentController')) require($com.DS.'controller.php');
// Inform controller of its base path
$config['base_path'] = $com;
// Init controller
$cont = new ContentController($config);
// Get wanted view
$view =& $cont->getView('category', 'html');
// Add path to wanted template
$view->addTemplatePath(JPATH_SITE.DS.'templates'.DS.'ja_wall'.DS.'html'.DS.'com_content'.DS.'category');
// Adding needed params to load info
JRequest::setVar('view', 'category');
JRequest::setVar('layout', 'blog');
JRequest::setVar('Itemid', '102');
JRequest::setVar('id', $this->item->catid);
// Get and load language
$lang =& JFactory::getLanguage();
$lang->load('com_content', JPATH_SITE);
// Render page
$cont->display();

How to capture view content in symfony for pdf output

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?

Categories