How to add logo to stripe invoice pdf in laravel - php

How to add logo to stripe invoice pdf. I'm using laravel framework. Is there any way I can modify the blade file under vendor section? and commit the same.

Run php artisan vendor:publish.
This will create vendor/cashier/receipt.blade.php file in your views folder, which you can modify as per your requirement & commit.

Extend Cashier's Invoice - Laravel\Cashier\Invoice.
Add $options->setIsRemoteEnabled(true); to pdf method:
public function pdf(array $data)
{
if (! defined('DOMPDF_ENABLE_AUTOLOAD')) {
define('DOMPDF_ENABLE_AUTOLOAD', false);
}
$options = new Options;
$options->setChroot(base_path());
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->setPaper(config('cashier.paper', 'letter'));
$dompdf->loadHtml($this->view($data)->render());
$dompdf->render();
return $dompdf->output();
}

Related

PDF data displayed but file not downloaded

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();
}

PrestaShop libraries replacement

I use PrestaShop 1.6.1.4 and I want to change the library tcpdf with dompdf.
I use this form for creating invoices.
What are the best practices for a library exchange?
I created inside override the tools folder and I put us dompdf-master found here https://github.com/dompdf/dompdf.
Instead inside override/classes/pdf i copied PDFGenerator.php, there is in classes/pdf.
In PDFGenerator.php add:
require_once('/../override/tools/dompdf-master/dompdf/Dompdf.php');
require_once('/../override/tools/dompdf-master/autoload.inc.php');
include('/../override/tools/dompdf-master/dompdf/dompdf_config.inc.php');
use Dompdf\Dompdf;
use Dompdf\Options;
The class becomes:
class PDFGenerator extends DOMPDF
Eliminates the render() function and replace it with:
public function render($filename, $display = true)
{
if (empty($filename)) {
throw new PrestaShopException('Missing filename.');
}
$html = $this->header.$this->content.$this->footer;
//die($html);
$options = new Options();
$options->set('A4','potrait');
$options->set('enable_css_float',true);
$options->set('isHtml5ParserEnabled', true);
$dompdf = new DOMPDF($options);
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($filename);
}
Then i deleted cache/class_index.php

how to generate pdf using blade view with tcpdf laravel 5?

I'm trying to create a pdf using laravel 5 and https://github.com/elibyy/laravel-tcpdf
i have a form of 20 pages writing in laravel blade when my client fill the form and click submit i want to generate pdf for him and save it
i try to do it like this in my controller
public function createPDF()
{
$pdf = Input::get('pdf',null);
$company = Input::get('company',null);
$branch = Input::get('branch',null);
$sub_branch = Input::get('sub_branch',null);
$form_type = Input::get('form_type',null);
$form_name = Input::get('form_name',null);
$form_heb_name = Input::get('form_heb_name',null);
$sig_path=FormsController::getSignature_file();
$data=compact('company','branch','sub_branch','form_type','form_name','form_heb_name','sig_path');
Input::flash();
if ($pdf)
{
$pdf = new TCPDF();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
$filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
$pdf->output($filename, 'I');
return redirect('forms');
}
return view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name , $data);
}
bat its not working it's create 2 pdf page with All fields on top of each other
how to fix it?
In addition, I want to save the pdf in a way it can not be edited file how can i do it?
thanks.
I will try to give you a step by step answer (tested for laravel 5.1):
Before using it, be sure that you installed the TCPDF service provider correctly:
Installing TCPDF service provider
In composer.json, add this package:
"require": {
"elibyy/laravel-tcpdf": "0.*"
}
Run composer update
in config/app.php add this service provider
'providers' => [
//..
Elibyy\TCPDF\ServiceProvider::class,
]
Run php artisan vendor:publish , that will generate config/laravel-tcpdf.php in your files
Generate the pdf in your controller
public function createPDF()
{
[...]
//get default settings from config/laravel-tcpdf.php
$pdf_settings = \Config::get('laravel-tcpdf');
$pdf = new \Elibyy\TCPDF\TCPdf($pdf_settings['page_orientation'], $pdf_settings['page_units'], $pdf_settings['page_format'], true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
$filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
$pdf->output($filename, 'I');
return redirect('forms');
}

dompdf->render(); not working in api class

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);
}

How to create a pdf using Fpdf in cakephp 2.0?

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

Categories