aryatama045/laravel-fpdf

Laravel package to include Fpdf. It ships with Fpdf 1.84.

dev-main 2022-12-15 01:44 UTC

This package is auto-updated.

Last update: 2024-06-15 04:37:01 UTC


README

Laravel FPDF

install

1) Composer

composer require aryatama045/laravel-fpdf

2) Optional: Laravel 5.4 and below

// config/app.php

'providers' => [
  // ...
  Aryatama045\Fpdf\FpdfServiceProvider::class,
],

'aliases' => [
  // ...
  'Fpdf' => Aryatama045\Fpdf\Facades\Fpdf::class,
],

Usage

(Method 1) Facade

use App\Http\Controllers\Controller;
use Aryatama045\Fpdf\Facades\Fpdf;

class MyController extends Controller
{
    public function index()
    {
        Fpdf::AddPage();
        Fpdf::SetFont('Courier', 'B', 18);
        Fpdf::Cell(50, 25, 'Hello World!');
        Fpdf::Output('I', "MyPdf.pdf", true);

        exit;
    }
}

(Method 2) Extends class

create your pdf class

namespace App\Pdf;

use Aryatama045\Fpdf\Fpdf;

class MyPdf extends Fpdf
{
    private $data;

    public function __construct($data)
    {
        $this->data = $data;
        parent::__construct('P', 'mm', 'A4');
        $this->SetA4();
        $this->SetTitle('My pdf title', true);
        $this->SetAuthor('Aryatama', true);
        $this->AddPage('L');
        $this->Body();
    }

    public function Header()
    {
        // fixed all pages
    }

    public function Body()
    {
        $this->SetFont('Arial', 'B', '24');
        $this->Cell(50, 25, $this->data->title);

        $this->SetFont('Arial', '', '14');
        $this->Cell(50, 25, $this->data->content);
    }

    public function Footer()
    {
        // fixed all pages
    }
}

In your controll

use App\Http\Controllers\Controller;
use App\Pdf\MyPdf;

class MyController extends Controller
{
    public function index()
    {
        $data = MyModel::all();

        $myPdf = new MyPdf($data);

        $myPdf->Output('I', "MyPdf.pdf", true);

        exit;
    }
}

FPDF official docs

http://www.fpdf.org