Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 625
yogev levi Avatar asked May 28 '15 06:05

yogev levi


1 Answers

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');
    
}
like image 115
Bogdan Rusu Avatar answered Oct 19 '22 05:10

Bogdan Rusu