Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gocanto/laravel-simple-pdf
Simple laravel PDF generator.
https://github.com/gocanto/laravel-simple-pdf
generator laravel pdf php
Last synced: about 2 months ago
JSON representation
Simple laravel PDF generator.
- Host: GitHub
- URL: https://github.com/gocanto/laravel-simple-pdf
- Owner: gocanto
- License: mit
- Created: 2019-06-11T07:14:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-26T04:22:29.000Z (about 4 years ago)
- Last Synced: 2024-05-02T03:40:05.847Z (8 months ago)
- Topics: generator, laravel, pdf, php
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 22
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## About it
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](https://github.com/gocanto/laravel-simple-pdf/blob/master/resources/views/templates/default.blade.php)
## Installation
This library uses [Composer](https://getcomposer.org) 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:
```php
use Gocanto\SimplePDF\Builder;
use Gocanto\SimplePDF\TemplateContext;Route::get('default-template', function (Builder $builder) {
$context = TemplateContext::make([
'title' => 'foo',
'name' => 'bar',
'content' => 'Some amazing content!
',
]);$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](https://github.com/gocanto/laravel-simple-pdf/blob/master/src/TemplateContext.php)- Lastly, we invoke the `make` method passing in our context object and finish by returning with the `render` 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:
```php
use Gocanto\SimplePDF\Builder;
use Gocanto\SimplePDF\TemplateContext;Route::get('custom-template', function (Builder $builder) {
$context = TemplateContext::make([
'title' => 'foo',
'name' => 'bar',
'content' => 'Some amazing content!
',
]);$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.
## Features
- Multi templates ability.
- The state is immutable, meaning all the withers as `withStream`, `withTemplate` and `withHeaders` methods will return a new instance of the `Builder`.
- 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:
```php
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](https://github.com/gocanto/laravel-simple-pdf/blob/master/LICENSE.md) 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](https://twitter.com/gocanto)!
Thanks!
Gustavo Ocanto.