Generate a PDF in Laravel

The simplest way to generate a PDF in Laravel is to call the Docweave API with the built-in Http facade — no headless browser to install, configure, or keep running on your server.

Generate a PDF in Laravel with the Http facade

POST HTML to /api/v1/pdf using Laravel's Http facade (Guzzle under the hood), then write the response body to disk with Storage:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\Response;

class PdfController extends Controller
{
    public function generate(Request $request): Response
    {
        $response = Http::withToken(config('services.docweave.key'))
            ->withHeaders(['Accept' => 'application/pdf'])
            ->post('https://docweave.dev/api/v1/pdf', [
                'source' => [
                    'type' => 'html',
                    'html' => '<h1>Hello from Laravel</h1><p>Rendered by Docweave.</p>',
                ],
                'options' => ['format' => 'A4'],
                'idempotencyKey' => 'laravel-example-001',
            ]);

        if ($response->failed()) {
            abort(502, 'Docweave request failed: ' . $response->status());
        }

        Storage::disk('local')->put('pdfs/hello.pdf', $response->body());

        return response($response->body(), 200)
            ->header('Content-Type', 'application/pdf');
    }
}

Register the API key in config/services.php

Keep the key out of source control by reading it from env() through the standard services config, the same way Laravel wires up Stripe or Mailgun:

// config/services.php

return [
    // ...existing entries

    'docweave' => [
        'key' => env('DOCWEAVE_API_KEY'),
    ],
];

…or from an AI agent

The same request works without any Laravel code at all. Run npx @docweave/mcp and an AI agent gets a generate_pdf tool that wraps this same orchestration:

// From an AI agent over MCP — no HTTP client, no Laravel app required:
generate_pdf({
  source: {
    type: "html",
    html: "<h1>Hello from Laravel</h1><p>Rendered by Docweave.</p>"
  },
  outputPath: "/tmp/hello.pdf"
})

FAQ

How do I generate a PDF in Laravel without installing a headless browser?

Call the Docweave API instead of running Chromium/Dompdf/wkhtmltopdf yourself. POST your HTML to https://docweave.dev/api/v1/pdf using Laravel's Http facade (built on Guzzle), and the response body is the finished PDF — no browser binary to install or maintain on your server.

Can I use the Http facade to convert HTML to PDF in Laravel?

Yes. Call Http::withToken($key)->post('https://docweave.dev/api/v1/pdf', [...]) with a source of type "html" containing your markup. The response body is PDF bytes, which you can save with Storage::disk('local')->put() or stream straight back in a controller response.

Where should I store the Docweave API key in a Laravel app?

Add DOCWEAVE_API_KEY to your .env file, register it under a docweave key in config/services.php, and read it with config('services.docweave.key') — the same pattern Laravel uses for Stripe, Mailgun, and other service credentials. Never commit the key or hardcode it in a controller.

How is PDF generation billed when called from a Laravel app?

Per document, not per page. A single POST from an artisan command, queued job, or controller that renders a one-page receipt or a fifty-page report is billed the same, and passing an idempotencyKey means a retried job never generates or bills the same document twice.

Ready to generate PDFs from your Laravel app?

Get an API key