gocanto / laravel-simple-pdf
Simple laravel PDF generator.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 22
Watchers: 2
Forks: 3
Open Issues: 0
Type:php-bundle
Requires
- php: ^7.2
- dompdf/dompdf: ^0.8.3
- guzzlehttp/guzzle: ^6.3
- illuminate/contracts: ^5.8
- illuminate/support: ^5.8
- illuminate/view: ^5.8
- psr/http-message: ^1.0
- symfony/http-foundation: ^4.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- infection/infection: ^0.11.4
- mockery/mockery: ^1.0.0
- phpmd/phpmd: ^2.6
- phpro/grumphp: ^0.14.2
- phpstan/phpstan: ^0.11
- phpstan/phpstan-mockery: ^0.11.0
- phpstan/phpstan-phpunit: ^0.11.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.3
- vimeo/psalm: ^3.4
This package is auto-updated.
Last update: 2024-11-26 16:39:50 UTC
README
This library is a minimalist on demand and immutable PDF generator
for Laravel. It aims to keep a small friction once you need to generate a printable file using a intuitive public API.
Simple PDF is shipped with a default template that you will be able to use to frame your next PDF files. You can see its layout here
Installation
This library uses Composer to manage its dependencies. So, before using it, make sure you have it installed in your machine. Once you have done this, you will be able to pull this library in by typing the following command in your terminal.
composer require gocanto/laravel-simple-pdf
Default template implementation
Using the default template is the easiest way to get started. Like so:
use Gocanto\SimplePDF\Builder; use Gocanto\SimplePDF\TemplateContext; Route::get('default-template', function (Builder $builder) { $context = TemplateContext::make([ 'title' => 'foo', 'name' => 'bar', 'content' => '<h1>Some amazing content!</h1>', ]); $builder->make($context); return $builder->render(); });
What's going on here ?
-
First of all, we imported the
Builder
and theTemplate Context
objects; you can think about them as the manager to generate your PDF files. For instance, theBuilder
is the one on charge of creating theStream File
we will be rendering on demand while the another one holds the data to be display within the default template. -
Second of all, we created the
TemplateContext
object with the desired data to be shown in our PDF file. The context object is a simple value object that holds some handy methods to manipulate the given array within our blade files. See more -
Lastly, we invoke the
make
method passing in our context object and finish by returning with therender
functionality.
Custom template implementation
This implementation is done out of the same API, but it has a bit of configuration on the top. Like so:
use Gocanto\SimplePDF\Builder; use Gocanto\SimplePDF\TemplateContext; Route::get('custom-template', function (Builder $builder) { $context = TemplateContext::make([ 'title' => 'foo', 'name' => 'bar', 'content' => '<h1>Some amazing content!</h1>', ]); $builder->addLocation(resource_path('views/home')); $new = $builder->withTemplate('home'); $new->make($context); return $new->render(); });
As you can see, the first three steps
are the same as the default implementation, so there is no need to explain further on this regard. Now, we need to clarify the differences on its
setup.
-
First of all, we tell the builder about our templates root folder by invoking the
addLocation
method. This way, we will be able to have a views root folder to keep our different templates. -
Second of all, we will register the custom templates by calling the
withTemplate
method. Please, do bear in account this is an immutable method, therefore, we will have a newBuilder
instance as result. -
Lastly, we will call the
make
andrender
method in the same way we did with the default template above.
Features
- Multi templates ability.
- The state is immutable, meaning all the withers as
withStream
,withTemplate
andwithHeaders
methods will return a new instance of theBuilder
. - The exported stream is expose for custom manipulations, meaning you can pass a call back to the render method to manipulate the given stream before rendering time. Like so:
use Psr\Http\Message\StreamInterface; $new->render(function (StreamInterface $stream) { //do something amazing with the stream echo $stream->getContents(); });
TO DO
- (Gus) Allow rendered HTML or Blades files template as part of the
TemplateContext
content field. - (interested?) Allow more engines like twig.
- (interested?) Make it framework agnostic.
Contributing
Please feel free to fork this package and contribute by submitting a pull request to enhance its functionality.
License
The MIT License (MIT). Please see License File for more information.
How can I thank you?
Why not star the github repo and share the link for this repository on Twitter?
Don't forget to follow me on twitter!
Thanks!
Gustavo Ocanto.