PDF is one of the most commonly used document formats today. Most web applications include a feature to generate PDFs for invoices, receipts, reports, and more. This blog will guide you on generating PDF files using TCPDF in Laravel 11 and help you add this functionality to your future projects.
Table Of Content
1 Prerequisites
1.) PHP version of >= 8.2
2.) Composer
3.) Mysql
2 Introduction
TCPDF is a well-established library with comprehensive documentation. Here’s a tutorial on generating PDFs using TCPDF in Laravel 11.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('order', function (Blueprint $table) {
$table->id();
$table->string('order_ref');
$table->string('order_invoice');
$table->string('customer_first_name');
$table->string('customer_last_name');
$table->string('customer_address');
$table->string('customer_company');
$table->string('amount');
$table->string('order_status');
$table->timestamps();
});
Schema::create('orderitems', function (Blueprint $table) {
$table->id();
$table->string('order_id');
$table->string('product_name');
$table->string('item_price');
$table->string('quantity');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('order');
Schema::dropIfExists('orderitems');
}
};
Run the migration:
php artisan migrate
Create the Order model in app/Models/Order.php:
php artisan make:model Order
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'orders';
}
Create the OrderItem model in app/Models/Orderitem.php:
php artisan make:model Orderitem
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Orderitem extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'orderitems';
}
6 Create Controller ( PdfController )
Create Controller PdfController:
php artisan make:controller PdfController
Within this controller, implement the index() and invoice() methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\Orderitem;
use Elibyy\TCPDF\Facades\TCPDF;
class PdfController extends Controller
{
public function index()
{
$orders = Order::get();
return view('index',compact('orders'));
}
public function invoice($id)
{
$filename='invoice.pdf';
$order=new Order;
$orderitem=new Orderitem;
$data = [
'order' => $order->find($id),
'orderitems' => $orderitem->where("order_id",$id)->get(),
];
//return view('socialshare', compact('shareButtons', 'posts'));
$html = view()->make('invoice', $data)->render();
// $html = view('invoice',$data);
$pdf = new TCPDF('L', PDF_UNIT, 'A5', true, 'UTF-8', false);
//$pdf::SetCreator(PDF_CREATOR);
$pdf::SetAuthor('Get Sample Code');
$pdf::SetTitle('Invoice');
$pdf::SetSubject('Invoice');
$pdf::setPrintHeader(false);
$pdf::setPrintFooter(false);
$pdf::addPage();
// output the HTML content
$pdf::writeHTML($html, true, false, true, false, '');
//line ini penting
//$this->response->setContentType('application/pdf');
//Close and output PDF document
$pdf::Output(public_path($filename), 'I');
return response()->download(public_path($filename));
}
}
?>
7 Define a Route
Add the following routes in routes/web.php:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfController;
Route::get('/', function () {
return view('welcome');
});
Route::get('orders', [PdfController::class, 'index']);
Route::get('invoicepdf/{id}', [PdfController::class, 'invoice']);
This example illustrates how to integrate TCPDF with Laravel 11 to generate PDFs, using Generating PDF in Laravel 11 and How to Generate PDF Using TCPDF in Laravel 11 techniques.