CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE Videos New
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

How to generate a PDF from html with TCPDF in laravel


{
    "require": {
        "elibyy/tcpdf-laravel": "5.2.*"
    }
}

composer require elibyy/tcpdf-laravel

'providers' => [
    //...
    Elibyy\TCPDF\ServiceProvider::class,
]

'aliases' => [
    //...
    'PDF' => Elibyy\TCPDF\Facades\TCPDF::class
]

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use PDF;

class DefaultController extends Controller
{
    public function index()
    {
        $html = '<h1>Hello World</h1>';
        
        PDF::SetTitle('Hello World');
        PDF::AddPage();
        PDF::writeHTML($html, true, false, true, false, '');

        PDF::Output('hello_world.pdf');
    }
}

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use PDF;

class DefaultController extends Controller
{
    public function index()
    {    
        for ($i = 0; $i < 5; $i++) {
            $html = '<h1>Hello World '.$i.'</h1>';
            PDF::SetTitle('Hello World'.$i);
            PDF::AddPage();
            PDF::Write(0, 'Hello World'.$i);
            // Write the file instead of throw it in the browser
            PDF::Output(public_path('hello_world' . $i . '.pdf'), 'F');
            PDF::reset();
        }
    }
}

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Elibyy\TCPDF\Facades\TCPDF;

class DefaultController extends Controller
{
    public function index()
    {
        $html = '<h1>Hello world</h1>';
        $pdf = new TCPDF();
        $pdf::SetTitle('Hello World');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');
        $pdf::Output('hello_world.pdf');
    }
}

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Elibyy\TCPDF\Facades\TCPDF;

class DefaultController extends Controller
{
    public function index()
    {
        $view = \View::make('myview_name');
        $html = $view->render();
        
        $pdf = new TCPDF();
        $pdf::SetTitle('Hello World');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');
        $pdf::Output('hello_world.pdf');
    }
}