PHP

Generate PDFs in Laravel

Generate a PDF from a Laravel controller using the built-in HTTP client — no DomPDF or Snappy.

Laravel PDF packages like DomPDF and Snappy each bring their own rendering quirks and binaries. With PDFgen you use Laravel’s built-in Http client: POST your HTML and data, then return the PDF as a response.

Render a Blade view to a string and pass it as the html field, or send Handlebars HTML directly as shown below.

Generate a PDF in Laravel

POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.

<?php
use Illuminate\Support\Facades\Http;
$response = Http::withToken(config('services.pdfgen.key'))
->post('https://pdfgen.com/api/v1/generate', [
'html' => '<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>',
'engine' => 'handlebars',
'format' => 'A4',
'data' => ['number' => 'INV-001', 'client' => 'Acme Corp', 'total' => '$2,400.00'],
]);
return response($response->body(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="invoice.pdf"',
]);

Convert existing HTML to PDF in Laravel

Already have finished HTML? Skip templating — render a Blade view to HTML, then convert it and send it with engine: "legacy", which tells the API your markup is final.

<?php
use Illuminate\Support\Facades\Http;
// Render an existing Blade view to an HTML string.
$html = view('invoice', ['number' => 'INV-001'])->render();
$response = Http::withToken(config('services.pdfgen.key'))
->post('https://pdfgen.com/api/v1/generate', [
'html' => $html,
'engine' => 'legacy',
'format' => 'A4',
]);
return response($response->body(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="invoice.pdf"',
]);

How it works

  1. 1

    Send your HTML + data

    POST an HTML template (with optional Handlebars tokens) and a data object to the API.

  2. 2

    We render the PDF

    PDFgen renders it server-side — no headless browser for you to run or scale.

  3. 3

    Get the PDF back

    Receive the PDF binary (or a hosted link with export_type:"url") and serve it from Laravel.

Good to know

  • The Http facade ships with Laravel — nothing to install.
  • Add a pdfgen entry to config/services.php and set the key in your .env.
  • Use view("invoice")->render() to turn a Blade template into the html field.

Frequently asked questions

Do I need DomPDF or Snappy in Laravel?
No. Laravel’s built-in Http client plus the PDFgen API replaces the DomPDF/Snappy + wkhtmltopdf stack.
Can I render a Blade view into the PDF?
Yes — call view("name", $data)->render() to get HTML, then pass it as the html field (use engine "html" if you don’t need Handlebars).
Where do I keep the API key?
In config/services.php read from an .env variable, then access it with config("services.pdfgen.key").

Generate PDFs in other languages

Your first PDF from Laravel in five minutes

Grab an API key, paste the snippet above, and render a PDF — no headless browsers to babysit. Build a reusable template with AI when you're ready.