PHP code example of tobento / app-pdf

1. Go to this page and download the library: Download tobento/app-pdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / app-pdf example snippets


use Tobento\App\AppFactory;
use Tobento\App\Pdf\Download\DownloadHandlerInterface;
use Tobento\App\Pdf\FileStorage\FileStorageHandlerInterface;
use Tobento\App\Pdf\PdfHandlerInterface;
use Tobento\App\Pdf\Queue\QueueHandlerInterface;
use Tobento\App\Pdf\Webhook\WebhookHandlerInterface;
use Tobento\Service\Pdf\ParametersFactoryInterface;
use Tobento\Service\Pdf\PdfGeneratorFactoryInterface;
use Tobento\Service\Pdf\PdfGeneratorInterface;
use Tobento\Service\Pdf\PdfGeneratorsInterface;
use Tobento\Service\Pdf\PdfResponseFactoryInterface;
use Tobento\Service\Pdf\RendererInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Boot the PDF module
$app->boot(\Tobento\App\Pdf\Boot\Pdf::class);
$app->booting();

// App-PDF interfaces
$pdfHandler = $app->get(PdfHandlerInterface::class);
$downloadHandler = $app->get(DownloadHandlerInterface::class);
$fileStorageHandler = $app->get(FileStorageHandlerInterface::class);
$queueHandler = $app->get(QueueHandlerInterface::class);
$webhookHandler = $app->get(WebhookHandlerInterface::class);

// Service-PDF interfaces
$parametersFactory = $app->get(ParametersFactoryInterface::class);
$pdfGeneratorFactory = $app->get(PdfGeneratorFactoryInterface::class);
$pdfGenerator = $app->get(PdfGeneratorInterface::class);
$pdfGenerators = $app->get(PdfGeneratorsInterface::class);
$pdfResponseFactory = $app->get(PdfResponseFactoryInterface::class);
$renderer = $app->get(RendererInterface::class);

// Run the app
$app->run();

use Tobento\App\Pdf\Pdf;
use Tobento\App\Pdf\PdfHandlerInterface;

class SomeService
{
    public function store(PdfHandlerInterface $pdfHandler): void
    {
        $pdf = new Pdf()
            ->html('<p>Lorem Ipsum</p>')

            // Use a specific PDF generator (optional, must be defined in pdf.php config)
            ->generator(
                name: 'name',
            )
            
            // Store the generated PDF using the specified storage and path
            ->store(
                storage: 'uploads',
                path: 'folder/document.pdf',
            )

            // Send the PDF to a webhook endpoint
            ->webhook(
                url: 'https://example.com/pdf',

                // Additional payload data for the webhook request (optional)
                payload: [],

                // Whether the PDF should be attached to the webhook request (default: true)
                attachPdf: true,
            )

            // Queue the PDF for asynchronous processing (all parameters optional)
            ->queue(
                name: 'default',
                delay: 0,
                retry: 3,
                priority: 0,
                encrypt: false,
                renderTemplates: true,
            )

            // Immediate actions (executed after queue evaluation)
            ->download(
                filename: 'document.pdf',
                headers: [],   // optional
                inline: true,  // optional, default is false
            );

        // Process the PDF according to the defined parameters
        $pdfHandler->handle($pdf);
    }
}

use Tobento\App\Pdf\Pdf;

$pdf = new Pdf()
    ->html('<p>Invoice</p>')
    ->store(
        storage: 'uploads',
        path: 'invoices/invoice.pdf',
    );

use Tobento\App\Pdf\Event;

use Tobento\App\Pdf\Pdf;

$pdf = new Pdf()
    ->html('<p>Report</p>')
    ->download(
        filename: 'report.pdf',
        headers: [],   // optional
        inline: false, // optional, default is false
    );

use Tobento\App\Pdf\Event;

use Tobento\App\Pdf\Pdf;

$pdf = new Pdf()
    ->html('<p>Invoice</p>')
    ->webhook(
        url: 'https://example.com/pdf-endpoint',
        
        // Additional payload data for the webhook request (optional)
        payload: [],

        // Whether the PDF should be attached to the webhook request (default: true)
        attachPdf: true,
    );

use Tobento\App\Pdf\Event;

use Tobento\App\Pdf\Pdf;

$pdf = new Pdf()
    ->html('<p>Invoice</p>')
    ->queue(
        // The queue name or channel where the job should be pushed
        name: 'default',

        // Delay (in seconds) before the job becomes available for processing
        delay: 0,

        // Number of retry attempts if the job fails
        retry: 3,

        // Job priority (lower = processed earlier, depending on queue system)
        priority: 0,

        // Encrypt the PDF content before sending it to the queue worker
        encrypt: false,

        // Render templates before queueing (if using templated PDFs)
        renderTemplates: true,
    );

use Tobento\App\Pdf\Pdf;

$pdf = new Pdf()
    ->html('<p>Invoice</p>')
    ->store('uploads', 'invoices/invoice.pdf')
    ->webhook('https://example.com/endpoint')
    ->queue();

use Tobento\App\Pdf\Event;
app/config/pdf.php