Generating PDF invoices in PHP using traditional libraries like mPDF or Dompdf often leads to heavy server memory consumption, broken CSS layouts, and maintenance headaches.
By offloading the rendering engine to QuartzAPI, your PHP application only needs to perform a fast, lightweight HTTP POST request with a JSON payload containing the invoice data.
Prerequisites
- PHP 7.4+ or 8.x
- A QuartzAPI account and API Key
- An active Template ID created in the QuartzAPI Visual Builder (e.g., tpl_invoice_v1)
Step 1: Prepare Your JSON Payload Data
Whether you are fetching invoice data from Eloquent (Laravel) or Active Record (Yii2), structure your data as a clean associative array matching your template placeholders.
$invoiceData = [
'template_id' => 'tpl_invoice_v1',
'data' => [
'invoice_number' => 'INV-2026-0042',
'date' => date('Y-m-d'),
'customer' => [
'name' => 'Acme Corporation',
'vat' => 'US123456789',
'email' => 'billing@acme.com'
],
'items' => [
['description' => 'Web Development Services', 'quantity' => 10, 'price' => 85.00, 'total' => 850.00],
['description' => 'Cloud Hosting (Annual)', 'quantity' => 1, 'price' => 240.00, 'total' => 240.00]
],
'subtotal' => 1090.00,
'tax' => 239.80,
'grand_total' => 1329.80
]
];
Step 2: Implementation in Laravel
In Laravel, use the built-in HTTP Client (Illuminate\Support\Facades\Http) to send the payload and handle the binary response.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class InvoiceController extends Controller
{
public function downloadPdf($invoiceId)
{
$payload = [
'template_id' => 'tpl_invoice_v1',
'data' => [
'invoice_number' => 'INV-2026-0042',
'grand_total' => 1329.80
]];
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . config('services.quartzapi.key'),
'Content-Type' => 'application/json',
])->post('https://api.quartzapi.com/v1/render', $payload);
if ($response->failed()) {
return back()->with('error', 'Failed to generate PDF invoice.');
}
return response($response->body(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="Invoice-2026-0042.pdf"',
]);
}
}
Step 3: Implementation in Yii2
In Yii2, you can use the yii\httpclient\Client component or standard cURL inside a Controller action.
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\httpclient\Client;
class InvoiceController extends Controller
{
public function actionDownloadPdf($id)
{
$payload = [
'template_id' => 'tpl_invoice_v1',
'data' => [
'invoice_number' => 'INV-2026-0042',
'grand_total' => 1329.80
]
];
$client = new Client();
$response = $client->createRequest()
->setMethod('POST')
->setUrl('https://api.quartzapi.com/v1/render')
->addHeaders([
'Authorization' => 'Bearer ' . Yii::$app->params['quartzApiKey'],
'Content-Type' => 'application/json',
])
->setContent(json_encode($payload))
->send();
if (!$response->isOk) {
throw new \yii\web\ServerErrorHttpException('PDF rendering service unavailable.');
}
return Yii::$app->response->sendContentAsFile(
$response->content,
'Invoice-2026-0042.pdf',
['mimeType' => 'application/pdf', 'inline' => true]
);
}
}
Key Benefits over mPDF / Dompdf
- Zero RAM overhead: Server memory stays constant regardless of document size.
- No CSS workarounds: Layout is maintained in the visual drag-and-drop editor.
- Instant updates: Change design elements without pushing code changes or running deployments.
How do you currently manage PDF layout updates in your PHP projects? Let's discuss in the comments below!
Top comments (0)