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.
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
You can read the full documentation on the PDF-API.io website.
To list all available templates, you can use the getTemplates
method on the PdfApi
facade.
use Pdfapiio\PdfapiLaravel\Facades\PdfApi;
$templates = PdfApi::getTemplates();
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
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.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.