abcreche/print

Generate Print Ready Document

v2.7.3 2024-01-18 12:16 UTC

README

Latest Version on Packagist Software License

Installation

You can install this package via composer using this command:

composer require abcreche/print

The package will automatically register itself

Usage

This package uses "Print Document Template" classes to generate PDF that you can then print via the browser. These classes are filled with data that you position as you like. This is particularly helpful to fill in official document that rely on specific templates.

Make Print Templates

First, make a new print document:

php artisan make:print ReceiptTemplate

The file can be foudn in app/PrintTemplates:

.
├── app
│   ├── PrintTemplates
│   │   ├── ReceiptTemplate.php
│
└── composer.json

In your controller you can generate the PDF for this print template now:

<?php

namespace App\Http\Controllers;

use App\Receipt;
use ABCreche\Printer\Facades\Printer;
use App\PrintTemplates\ReceiptTemplate;

class ReceiptController extends Controller
{
    public function print(Receipt $receipt)
    {
        return Printer::download(new ReceiptTemplate($receipt), 'receipt.pdf');
    }
}

Configure Print Templates

This is how your Print Template look by default:

<?php

namespace App\PrintTemplates;

use App\Receipt;
use ABCreche\Printer\Printer;

class ReceiptTemplate implements PrintTemplate
{
    public function build()
    {
        $this->firstPage()->write('Hello There');
    }
}

Manage pages and content

Every content you will add on your document needs to be associated to a page.

Because we work with printed document, positioning elements in specific pages is very important.

You can add new pages like this:

public function build()
{
    $this->addPage();
}

You will then add content in the last page you add.

public function build()
{
    $this->lastPage()->write('I will appear on the first page');
    $this->addPage();
    $this->lastPage()->write('I will appear on the second page');
}

Write data

Let's write some data on the document:

public function build()
{
    $this->lastPage()
        ->write($receipt->number)
        ->top('10px')
        ->left('100px');

    $this->lastPage()
        ->write($receipt->total)
        ->bottom('35px')
        ->right('50%');

    return $this;
}

You can also chain methods

public function build()
{
    // Orders of direction is the same as in CSS for padding / margin properties
    return $this->lastPage()
        ->write($receipt->number, '10px', null, null, '100px')
        ->write($receipt->total, null, '35px', '50%', null);
}

Orientation

You can choose the orientation of the document by setting the orientation property.

// Default: portrait
// Options: 'portrait', 'landscape'
protected $orientation = 'portrait';

Page Margins

You can set the page margins.

// Default: 25mm
protected $pageMargin = '25mm';

Preview

You can preview any PrintTemplate by simply returning it from the controller

Route::get('printer', function () {
    $receipt = App\Receipt::find(1);

    return new App\PrintTemplates\ReceiptTemplate($receipt);
});

Then you can use chrome dev tools to render the print version of the page

Image from this stackoverflow post

Then switch the dev tools to device rendering and set the dimension of the page at

  • A4 : 595 x 842

Working with a preview like this will help you build your templates. When converting them to pdf, they will be exactly like the preview.

TODO

Support: