Guides · laravel invoice pdf without wkhtmltopdf
Laravel Invoice PDF Without wkhtmltopdf or Chrome
If you searched for Laravel invoice PDF without wkhtmltopdf, you are probably tired of one of these:
laravel-snappyneeding a wkhtmltopdf binary on every server — and wkhtmltopdf’s repository has been archived since 2023- Browsershot / Chrome-based packages pulling Node, Puppeteer, and a Chromium install into a PHP deploy
Allowed memory size exhaustedwhile a large invoice renders inside a web request- Fonts and remote images that work locally and break on the production box
This guide shows how to generate invoice PDFs from Laravel with one HTTP call — and, just as importantly, when you should not bother and keep rendering locally.
Be honest: laravel-dompdf may be all you need
DocuHook renders PDFs with Dompdf — the same engine behind barryvdh/laravel-dompdf. So the CSS support is the same class: tables and block layout work, flexbox and grid do not. DocuHook will not make your invoice look better than laravel-dompdf would.
Stay with local Dompdf when:
- You have a handful of invoices a day and they render fine in a web request
- Your servers already have the fonts and memory headroom you need
- Documents must never leave your infrastructure
- You do not want another API key or a monthly quota
That setup is two lines, and it is a good one:
use Barryvdh\DomPDF\Facade\Pdf;
return Pdf::loadView('invoices.show', ['invoice' => $invoice])->download('invoice.pdf');When an API is the simpler choice
Move the rendering out of your app when the operational side starts to hurt:
- Web workers stay lean. No PDF engine, font cache, or memory spikes in PHP-FPM; your request does one HTTP call.
- One template contract across stacks. The same
data+htmlcall works from Laravel, a Node webhook, a Python job, or WooCommerce. - Webhook bursts. Async mode answers
202with a job immediately, so a payment provider’s webhook never waits on rendering. - Fewer moving parts to patch. No Dompdf version, font directory, or remote-asset settings to maintain on each server.
If none of those apply, close this tab and use laravel-dompdf.
1. Keep the template out of Blade
DocuHook tokens look like {{invoice.number}} — which Blade would try to echo. Store the template as a plain file, for example resources/pdf/invoice.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: "DejaVu Sans", sans-serif; margin: 40px; font-size: 13px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { padding: 8px 0; border-bottom: 1px solid #ddd; text-align: left; }
.num { text-align: right; }
</style>
</head>
<body>
<h1>Invoice {{invoice.number}}</h1>
<p>{{invoice.date}} · {{customer.name}} · {{customer.email}}</p>
<table>
<thead><tr><th>Item</th><th class="num">Qty</th><th class="num">Total</th></tr></thead>
<tbody>
<!--ROWS-->
</tbody>
</table>
<p class="num"><strong>Total {{invoice.currency}} {{invoice.total}}</strong></p>
</body>
</html>(Prefer Blade? Render the full view yourself with view('invoices.show', …)->render() and send 'data' => []. DocuHook then only does the PDF step.)
2. A small service class
Templates have no loop syntax, so the service writes one row per line item using indexed tokens. DocuHook fills and HTML-escapes the values:
namespace App\Services;
use App\Models\Invoice;
use Illuminate\Support\Facades\Http;
class InvoicePdf
{
public function render(Invoice $invoice): string
{
$items = $invoice->items->values();
$rows = $items->keys()->map(fn (int $i) =>
"<tr><td>{{items.$i.name}}</td>"
. "<td class=\"num\">{{items.$i.qty}}</td>"
. "<td class=\"num\">{{invoice.currency}} {{items.$i.total}}</td></tr>"
)->implode("\n");
$html = str_replace('<!--ROWS-->', $rows, file_get_contents(resource_path('pdf/invoice.html')));
$response = Http::withToken(config('services.docuhook.key'))
->accept('application/pdf')
->timeout(30)
->post('https://docuhook.com/api/v1/render', [
'mode' => 'sync',
'data' => [
'invoice' => [
'number' => $invoice->number,
'date' => $invoice->issued_at->toDateString(),
'currency' => $invoice->currency,
'total' => number_format($invoice->total, 2),
],
'customer' => ['name' => $invoice->customer->name, 'email' => $invoice->customer->email],
'items' => $items->map(fn ($item) => [
'name' => $item->name,
'qty' => $item->quantity,
'total' => number_format($item->total, 2),
])->all(),
],
'html' => $html,
'filename' => "invoice-{$invoice->number}.pdf",
])
->throw();
return $response->body(); // raw application/pdf bytes
}
}Add the key to config/services.php ('docuhook' => ['key' => env('DOCUHOOK_API_KEY')]) and .env.
3. Download from a controller
use App\Models\Invoice;
use App\Services\InvoicePdf;
Route::get('/invoices/{invoice}/pdf', function (Invoice $invoice, InvoicePdf $pdf) {
return response($pdf->render($invoice), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="invoice-' . $invoice->number . '.pdf"',
]);
})->middleware('auth');PDF is the API’s default response. If you want the merged HTML instead (for an email preview), add 'format' => 'html' to the body.
4. Queue it after payment
For “email the invoice when the order is paid”, generate the PDF on Laravel’s queue so the request (or the payment webhook) is not held open:
namespace App\Jobs;
use App\Models\Invoice;
use App\Services\InvoicePdf;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
class GenerateInvoicePdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
public function __construct(public Invoice $invoice) {}
public function handle(InvoicePdf $pdf): void
{
Storage::put("invoices/{$this->invoice->number}.pdf", $pdf->render($this->invoice));
}
}
// in your payment webhook / listener:
GenerateInvoicePdf::dispatch($invoice);No queue worker? Use DocuHook’s async mode instead: send 'mode' => 'async' and you get 202 with job_id and result_url; later Http::withToken($key)->accept('application/pdf')->get($resultUrl) returns the PDF (202 while pending). The Stripe webhook guide walks through that flow.
What about the official PHP client?
composer require docuhook/docuhook installs DocuHook\Client. Its render(), hookRender(), jobStatus(), and jobResult() read JSON responses, so render() gives you the merged HTML in $result['html']. That is handy for HTML previews; for the PDF bytes, Laravel’s Http client as shown above is the shortest path.
Dompdf caveat
Same engine, same rules as laravel-dompdf: build invoices with tables, keep CSS simple, use the bundled DejaVu fonts or load fonts over HTTPS. Flexbox, grid, and modern CSS effects will not render as in Chrome. If you truly need Chrome output, that is a different tool (Browsershot, or 'format' => 'html' plus your own Chrome worker).
Checklist
- Template stored outside Blade (or escaped), with a
<!--ROWS-->marker - Line items expanded into indexed token rows
DOCUHOOK_API_KEYin.env, read throughconfig()- Heavy or webhook-triggered renders on the queue or in async mode
- Free plan: 100 PDFs/month with a watermark; Starter is 2,500/month at $19 — plans
Related guides
- WooCommerce webhook to PDF invoice
- Stripe webhook to PDF invoice
- Python (FastAPI / Flask) HTML to PDF without WeasyPrint or Playwright
- How to generate PDF from HTML in Next.js
The 3-line fix
$pdf = Http::withToken(config('services.docuhook.key'))->accept('application/pdf')
->post('https://docuhook.com/api/v1/render', ['data' => $data, 'html' => $html])->throw()->body();
Storage::put('invoices/invoice.pdf', $pdf); // no wkhtmltopdf, no Chrome on the serverTry a template in the live sandbox (no signup), read the API docs, or get an API key.