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');
}
Related
I here trying to create a pdf with fpdf and want to display and rename the web title with Output(name) it worked fine before, but after I added Image() it didn't work, did I do something wrong?
require('../libs/fpdf/fpdf.php');
$no_ref = 'TJ100001';
$dir = 'https://www.vuien.my.id/assets/img/logo.png';
$pdf = new FPDF('l','mm','A5');
$pdf->AddPage();
$pdf->Image($dir,10,7,10);
$filename = 'Invoice '.$no_ref.'.pdf';
$pdf->Output($filename,'I', true);
with add Image() :
screenshot
With no add Image() :
screenshot
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 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();
}
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 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);
}