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
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
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 the Template Context
objects; you can think about them as the manager to generate your PDF files. For instance, the Builder
is the one on charge of creating the Stream 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 the render
functionality.
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
new Builder
instance as result.
Lastly, we will call the make
and render
method in the same way we did with the default template above.
withStream
, withTemplate
and withHeaders
methods will return a new instance of the Builder
.use Psr\Http\Message\StreamInterface;
$new->render(function (StreamInterface $stream) {
//do something amazing with the stream
echo $stream->getContents();
});
TemplateContext
content field.Please feel free to fork this package and contribute by submitting a pull request to enhance its functionality.
The MIT License (MIT). Please see License File for more information.
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.