pdf-api-io/pdfapi-laravel

This is my package pdfapi-laravel

v1.0.0 2024-07-21 11:56 UTC

This package is auto-updated.

Last update: 2024-10-22 14:01:42 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.