PHP SDKFirst PDF · 5 min

PHP SDK quickstart

Create the SDK explicitly, pass it final HTML, and choose synchronous bytes or an asynchronous stored render.

Create the client

The core never reads .env. Read your secret with your application's configuration system and pass it to the SDK.

render.php
php
 1use BladePDF\BladePdf;
 2
 3$bladePdf = BladePdf::create($applicationConfig['bladepdf_api_key']);

Render raw HTML

Synchronous render
php
 1$result = $bladePdf
 2    ->fromHtml('<h1>Invoice INV-42</h1>')
 3    ->format('A4')
 4    ->showBackground()
 5    ->render();
 6
 7$result->save(__DIR__.'/invoice.pdf');
 8
 9echo $result->requestId();

Use Twig, Latte, or another engine

Render local templates in your application. BladePDF receives the completed HTML; it does not need to know which template engine produced it.

Pre-rendered template HTML
php
 1$html = $twig->render('invoice.html.twig', [
 2    'invoice' => $invoice,
 3]);
 4
 5$result = $bladePdf->fromHtml($html, __DIR__.'/public')->render();

Allow local assets

Without configured roots, automatic resolution does not read the filesystem. Recreate the SDK with explicit permissions when HTML references local files.

Filesystem permissions
php
 1use BladePDF\Assets\AssetResolverOptions;
 2
 3$bladePdf = BladePdf::create(
 4    apiKey: $applicationConfig['bladepdf_api_key'],
 5    assetOptions: new AssetResolverOptions(
 6        documentRoot: __DIR__.'/public',
 7        searchRoots: [__DIR__.'/public', __DIR__.'/var/pdf-assets'],
 8        localHosts: ['localhost', 'app.example.test'],
 9    ),
10);

Submit an asynchronous stored render

Asynchronous render
php
 1$submission = $bladePdf
 2    ->fromTemplate('invoice.standard', ['invoice' => $invoice])
 3    ->reference('INV-42')
 4    ->storePdf()
 5    ->webhook('https://example.test/webhooks/bladepdf', $webhookSecret)
 6    ->async();
 7
 8echo $submission->requestId;
JavaScript and SVG scope

A file referenced by <script src> or an external SVG URL is uploaded. The resolver does not inspect JavaScript imports, dynamic imports, fetch(), runtime URLs, SVG contents, or dependencies embedded in SVG.