iwasherefirst2/pretty-pdf

Create a beautiful invoice with PHP

v2.1.0 2022-05-01 17:30 UTC

This package is auto-updated.

Last update: 2024-10-29 06:12:34 UTC


README

This package helps you to create invoices and letters with PHP with UTF8 support.

The invoice looks like this: image

The best thing about this package is that the invoices look absolutly fantastic and in addition, the address of your letter/invoice fully fits your envelope window! 20210326_173723

The default format is A4. If you need a different format, just the FPDF standard methods.

Each section of the invoice relates to a method: image

Required PHP Version

Installation

Install via composer:

composer require iwasherefirst2/pretty-pdf

Example

    $item = new \PrettyPdf\Partials\Invoice\Data\Item();

    $item->description = 'A new currency';
    $item->quantity = 5;
    $item->name = 'Bitcoin';
    $item->unitPrice = 2031.23;

    $paymentInfoDate = new \PrettyPdf\Partials\Invoice\Data\PaymentInfo();

    $paymentInfoDate->title = 'A really good title';
    $paymentInfoDate->description = 'A long description comes in here';
    $paymentInfoDate->bank = 'ING';
    $paymentInfoDate->bic = 'BICXXX';
    $paymentInfoDate->iban = 'DE42 4242 4242 4242 4242 24';
    $paymentInfoDate->name = 'Beauty Bill Creator';

    $bill = new \PrettyPdf\PrettyPdf();

    $bill->logo('/path/to/your/logo.png')
            ->headerInfoBox(['1600 Pennsylvania Ave NW', 'Washington', 'DC 20500', 'United States', 'Beauty Bill Package', 'info@drnielsen.de'])
            ->returnAddress('Dr. Schwadam, Schwinterfeldschraße 99, 10777 Berlin, Germany')
            ->receiverAddress(['Michael Jackson', 'Colorado Hippo Zoo', '5225 Figueroa Mountain Rd', 'Los Olivos', 'CA 93441', 'United States'])
            ->invoiceBox(['Date' => 'Today', 'Invoice' => 'I 2020-03-22', 'Tax-Number' => '18/455/12345'])
            ->items([$item], 19)
            ->paymentInfo($paymentInfoDate)
            ->additionalNote('Optioanl note. Nothing important here.')
            ->output('/path/where/you/want/to/store/file.pdf');
            

Methods

Most important function is the output function which renders the pdf, either by saving it as a file or by viewing the PDF in the browser, or save it as a string.

You can call all other functions from FPDF.

In addition, the following functions are available:

How can I add my own styling?

Simply create a class that extends PrettyPdf\Partials\Drawable.

<?php 

namespace MyApp\Styles;

class MyOwnStyle extends \PrettyPdf\Partials\Drawable
{
    public function draw(): void 
    {
        // Here you can use all functions from http://www.fpdf.org/ 
        // to draw on your pdf.
        $this->MultiCell(....);
        $this->Cell(...);
        $this->Rect(..);
        
    }
}

Next, you need to register your class or classes to PrettyPdf:

$prettyPdf = new \PrettyPdf\PrettyPdf();

$prettyPdf->addCustomPartials(['\MyApp\Styles\MyOwnStyle']);

Now you can use the method by simply calling the classname on $prettyPdf:

$prettyPdf->myOwnStyle()
          ->output('/path/to/file.pdf');   

How does testing work?

A pdf will be created during the test and compared to an existing pdf. Since comparing pdf documents is not feasible, since the file itself contain meta-data that are always distinct, our tests convert the pdf to an image and compare the images.

Thus, we require for testing the ImageMagic and GD library. To make your life easier, this package ships with a docker file.

You may use it like this:

docker-compose up -d --build

Next, go inside the container to install composer and run phpunit:

docker-compose exec app bash
composer install
php vendor/bin/phpunit

Create a test class that extends from BeautyBillTestCase.

Here is an example:

<?php

namespace Tests;

use BeautyBill\Partials\Body\Item;

class BeautyBillBodyTest extends BeautyBillTestCase
{
    public function test_body_basic()
    {
        $this->storeOnly = true;
        
        $item = new Item();
        
        $item->description = 'A new currency';
        $item->quantity = 5;
        $item->title = 'Bitcoin';
        $item->unitPrice = 2031.23;

        $this->bill->items([$item]);
        
        $this->assertEqualPDFs('Test.pdf');
    }
}

When storeOnly is set to true, it will not compare pdfs but just store the pdf in Test.pdf or whatever name you put in the method assertEqualPDFs. Once the pdf looks as you want to have it, you may remove the storeOnly line.