I have actually got very far but my output is gibberish and not a formatted pdf as I expected. Here is the relevant code:
JobsController:
public function viewpdf() {
App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->set('fpdf', new FPDF('P','mm','A4'));
$this->set('data', 'Hello, PDF world');
$this->render('pdf');
}
View/Layouts/pdf.ctp:
<?php
header('Content-Disposition: attachment; filename="downloaded.pdf"');
echo $content_for_layout;
?>
View/Jobs/pdf.ctp:
<?php
$fpdf->AddPage();
$fpdf->SetFont('Arial','B',16);
$fpdf->Cell(40,10,$data);
$fpdf->Output();
?>
with FPDF installed in the root/Vendors directory.
Change the response type in your controller function:
public function viewpdf() {
App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->response->type('pdf');
$this->set('fpdf', new FPDF('P','mm','A4'));
$this->set('data', 'Hello, PDF world');
$this->render('pdf');
}
In cake2 you can use a cleaner approach - with or without the new response object:
http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/
The headers will be set by cake itself from the controller.
It's not good coding (anymore) to set them in the layout (I used to do that, as well^^).
These days the CakePdf plugin has matured quite a bit into a very useful tool for PDF generation.
I recommend using that in Cake2.x.
See http://www.dereuromark.de/2014/04/08/generating-pdfs-with-cakephp
Related
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 am using HTML2PDF library in codeigniter.I am trying to generate bulk pdf using it.
In which i am facing issue like same content in every pdf or pdf have no content.I have already did my homework.Yeah but there is always showing perfect for generated first pdf (For account :3)
As per me there is must be issue of below code :
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
Issue : It works for first time but for second time it flush all the content of content variable and due to that duplicate PDF or without content PDF generate.
I have tried like below
1) create object in generatetemplate.php and pass to common.php
2) tried with include_once //getting same conent in every pdf and if i am doing echo then showing no content for 2nd and 3rd pdf
File structure :
application
controllers
generatetemplate.php
libraries
common.php
html2pdf
html2pdf.php
template.php
common.php :
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
generatetemplate.php
function __construct() {
parent::__construct();
$this->load->library("common");
$this->load->library('html2pdf');
}
function get_customer_data(){
$this->db->order_by("id","DESC");
$this->db->where('id IN (1,2,3)');
$query = $this->db->get("customers")->result_array();
foreach($query as $key=>$accountdata){
$this->common->print_content($accountdata);
}
}
Any help and ideas will be appreciated.
I have tried below code and its work for me.
Common.php
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf = new HTML2PDF('P','A4','en'); // Just added this line and its work for me.
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
i have my template just in string "<html>..</html>"
that were responded by an request;
now i want generate an pdf using DOMpdf
$pdf = new PdfModel();
$pdf->setOption('filename', 'monthly-report'); // Triggers PDF download, automatically appends ".pdf"
$pdf->setOption('paperSize', 'a4'); // Defaults to "8x11"
$pdf->setOption('paperOrientation', 'landscape'); // Defaults to "portrait"
// To set view variables
$pdf->setVariables(array(
'message' => 'Hello'
));
$pdf->setTemplate("path/to/something.html");
return $pdf;
instead of $pdf->setTemplate("path/to/something.html") how i can give to $pdf(ViewModel) my string template.
Looks like you need implement your own StringRenderer implementing Zend/View/Renderer/RendererInterface and also extend DOMPDFModule/Mvc/Service/ViewPdfRendererFactory to use your renderer.
This is because, PdfRenderer is hardcoded to use the default ViewManager Renderer and there is no StringRenderer in Zend.
Instead of returning a view model you could return a response:
$string = "<h1>hello</h1>";
$response = $this->getResponse();
$response->setContent($string)->setStatusCode(200);
$response->getHeaders()->addHeaderLine('Content-Type', 'text/html');
return $response;
You could use different template engine like mustache or twig ,compile php and then pass to loadHtml method from DOMpdf.
NOTE: UPDATED MY QUESTION
I am using zend.I have the file "stylesettings.php" under css folder. Having the following line to convert php file to css.
header("Content-type: text/css");
stylesetting.php is under application/css/stylesettings.php
Now i want to get the color code from my DB in stylesettings.php.Here i can write basic DB connection code to get values from DB. I guess there might be another way to get all DB values by using zend. How can we connect DB like "Zend_Db_Table_Abstract" in stylesettings file ?
Is it possible to use zend component in that file ? Kindly advice on this.
I hope you understand.
You are breaking the basic separated model assumed in a MVC framework. For such color code, if it's only used in one place, I would suggest that you output the color in the "style="color: <color>"" in-line style in order to keep the dynamic part in HTML and your CSS file static.
If you really want to do this, then you should consider output your dynamic stylesheet in a URL path other than css, and use the controller/views etc to generate the stylesheet.
i am using the following way to apply color settings in layout.phtml
Use below code in bootstrap file
<?php
require_once 'plugins/StyleController.php';
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initRouter() {
$front = Zend_Controller_Front::getInstance ();
$front->setControllerDirectory ( dirname ( __FILE__ ) . '/controllers' );
$router = $front->getRouter ();
$front->registerPlugin ( new StyleController ( $router ) );
}
}
Created folder plugins under application and create a new file called stylecontroller.php
<?php
class StyleController extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
/* code for getting color settings */
$this->settings = new Admin_Model_DbTable_Settings();
$view->colorsettings = $this->settings->getStyleSettings();
//print_obj($view->colorsettings);
}
}
?>
Also to get color code,
<?php
class Admin_Model_DbTable_Settings extends Zend_Db_Table_Abstract
{
public function getStyleSettings()
{
$select = $this->_db->select()
->from('style_settings');
$result = $this->getAdapter()->fetchAll($select);
return $result['0'];
}
}
?>
By using above code i can able to use $this->colorsettings in layout page.
This one fix my dynamic color in layout but not in other template files.
I am using dompdf to create a pdf file out of an html file that gets created on-the-fly for the sole purpose of it serving as input for the pdf generator, however I am having trouble doing this, I implemented the code in this thread and everything works fine (I could output a simple pdf) however when I try to give it a more specific url I get this error:
An Error Was Encountered Unable to
load the requested file
here's the code that has the problem:
function printPDF(){
//write_file() usa un helper (file)
$this->load->library('table');
$this->load->plugin('to_pdf');
// page info here, db calls, etc.
$query = $this->db->get('producto');
$data['table'] = $this->table->generate($query);
$path_url = base_url().'print/existencias.html';
write_file($path_url, $data['table']);
$html = $this->load->view($path_url, 'consulta', true);
pdf_create($html, 'consulta');
}
Not sure about the exact problem, but please check this:
1) as stated in CI's manual, load->view's second parameter should be an associative array or an objet, translated to vars via extract. That may generate some problem generating $html.
2) try making $path_url relative to application/views directory, as read in CI's manual.
you should use tcpdf to create a pdf.
//create controller for example :
public function create_pdf()
{
$this->load->library("Pdf");
$data['results'] = // your data
$this->load->view('pdfview',$data);
}
//pdfview is the page having tcpdf code and your pdf code.
You can try it like this
public function export_pdf() {
$filename = 'FILENAME.pdf';
header("Content-Description: Advertise Report");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/pdf; ");
$file = fopen('php://output', 'w');
$header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
fputpdf($file, $header);
fputpdf($file, 'YOUR DATA');
fclose($file);
exit;
}