pdf-api-io / pdfapi-laravel
This is my package pdfapi-laravel
Fund package maintenance!
pdf-api-io
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.0
- illuminate/contracts: ^10.0||^11.0
- illuminate/http: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
This package provides a Laravel integration for PDF-API.io. PDF-API.io is a service that allows you to design your PDF templates in a drag-and-drop editor and render them using a simple API.
Installation
You can install the package via composer:
composer require pdf-api-io/pdfapi-laravel
You can publish the config file with:
php artisan vendor:publish --tag="pdfapi-laravel-config"
This is the contents of the published config file:
return [ 'api_key' => env('PDF_API_KEY'), ];
Create an access token if you don't have one yet. You can do this visiting the PDF-API.io API token page. Add the following line to your .env
file:
PDF_API_KEY=your-api-key
Usage
You can read the full documentation on the PDF-API.io website.
List available templates
To list all available templates, you can use the getTemplates
method on the PdfApi
facade.
use Pdfapiio\PdfapiLaravel\Facades\PdfApi; $templates = PdfApi::getTemplates();
Render a PDF
To render a PDF, you can use the render
method.
use Pdfapiio\PdfapiLaravel\Facades\PdfApi; $pdf = PdfApi::render('your-template-id', [ 'some-variable' => 'some-value', ]); echo $pdf; // Output: "%PDF-1.7 %���� 6 0 obj << /Type /Page /Parent 1 0 R..."
By default, the render
method will return the content of the PDF as a string. If you want to get a JSON response instead, you can use the asJson
method. When JSON response is requested, the content of the PDF will be base64 encoded.
use Pdfapiio\PdfapiLaravel\Facades\PdfApi; $pdf = PdfApi::asJson()->render('your-template-id', [ 'some-variable' => 'some-value', ]); $content = base64_decode($pdf['data']);
You can control the output by calling the output
method:
use Pdfapiio\PdfapiLaravel\Facades\PdfApi; PdfApi::output(ApiOutputType::PDF)->render('your-template', []); // Returns the PDF as a string PdfApi::output(ApiOutputType::URL)->render('your-template', []); // Returns the URL to the rendered PDF
Merge templates
If you have multiple templates and you want to merge them into a single PDF, you can use the merge
method.
use Pdfapiio\PdfapiLaravel\Facades\PdfApi; $pdf = PdfApi::merge([ [ 'id' => 'your-template-id', 'data' => [ 'some-variable' => ' ], ], [ 'id' => 'your-template-id', 'data' => [ 'some-variable' => ' ], ], ]);
You can also use the asJson
and output
methods with the merge
method.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.