Generate invoice PDFs in PHP

The simplest way to generate invoice PDFs in PHP is to POST an invoice template and your billing JSON to the Docweave API — no headless browser to install, configure, or keep running on your server.

Invoice template example

Any HTML works as a template. Use {{ placeholders }} for the invoice number, customer, line items, subtotal, tax, total, and due date:

<article style="font-family: sans-serif;">
  <header style="display:flex; justify-content:space-between;">
    <h1>Invoice {{ number }}</h1>
    <div>Due {{ due_date }}</div>
  </header>

  <p>Billed to: {{ customer }}</p>

  <table style="width:100%; border-collapse:collapse;">
    <thead>
      <tr>
        <th style="text-align:left; border-bottom:1px solid #ccc;">Description</th>
        <th style="text-align:right; border-bottom:1px solid #ccc;">Qty</th>
        <th style="text-align:right; border-bottom:1px solid #ccc;">Price</th>
        <th style="text-align:right; border-bottom:1px solid #ccc;">Amount</th>
      </tr>
    </thead>
    <tbody>
      {{{ line_items }}}
    </tbody>
  </table>

  <p style="text-align:right;">Subtotal: {{ subtotal }}</p>
  <p style="text-align:right;">Tax: {{ tax }}</p>
  <p style="text-align:right;"><strong>Total due: {{ total }}</strong></p>
</article>

Generate the invoice PDF in PHP with cURL

POST the template and data to /api/v1/pdf using PHP's built-in cURL extension, then write the raw response body to disk:

<?php

$lineItemsHtml = "";
foreach ($invoice["line_items"] as $item) {
    $lineItemsHtml .= sprintf(
        "<tr><td>%s</td><td style=\"text-align:right;\">%d</td>"
        . "<td style=\"text-align:right;\">%s</td><td style=\"text-align:right;\">%s</td></tr>",
        $item["description"],
        $item["qty"],
        $item["price"],
        $item["amount"]
    );
}

$template = <<<HTML
<article style="font-family: sans-serif;">
  <header style="display:flex; justify-content:space-between;">
    <h1>Invoice {{ number }}</h1>
    <div>Due {{ due_date }}</div>
  </header>
  <p>Billed to: {{ customer }}</p>
  <table style="width:100%; border-collapse:collapse;">
    <tbody>{{{ line_items }}}</tbody>
  </table>
  <p style="text-align:right;">Subtotal: {{ subtotal }}</p>
  <p style="text-align:right;">Tax: {{ tax }}</p>
  <p style="text-align:right;"><strong>Total due: {{ total }}</strong></p>
</article>
HTML;

$payload = [
    "source" => [
        "type" => "template",
        "template" => $template,
        "data" => [
            "number" => "INV-1042",
            "customer" => "Acme Inc.",
            "due_date" => "2026-08-14",
            "line_items" => $lineItemsHtml,
            "subtotal" => "$1,200.00",
            "tax" => "$96.00",
            "total" => "$1,296.00",
        ],
    ],
    "idempotencyKey" => "inv-1042",
];

$ch = curl_init("https://docweave.dev/api/v1/pdf");

curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer dw_live_your_key",
        "Content-Type: application/json",
        "Accept: application/pdf",
    ],
    CURLOPT_POSTFIELDS => json_encode($payload),
]);

$pdfBytes = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($statusCode !== 200 || $pdfBytes === false) {
    throw new RuntimeException("Docweave request failed with status {$statusCode}");
}

file_put_contents(__DIR__ . "/invoice-1042.pdf", $pdfBytes);

echo "Saved invoice-1042.pdf" . PHP_EOL;

…or from an AI agent

The same request works without any PHP 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 PHP required:
generate_pdf({
  source: {
    type: "template",
    template: "<h1>Invoice {{ number }}</h1><p>Billed to {{ customer }}</p><p>Total due: {{ total }}</p>",
    data: { number: "INV-1042", customer: "Acme Inc.", total: "$1,296.00" }
  },
  outputPath: "/tmp/invoice-1042.pdf"
})

FAQ

How do I generate an invoice PDF in PHP?

POST an HTML invoice template and a data object to https://docweave.dev/api/v1/pdf with a source of type "template". Docweave binds your invoice number, customer, line items, and totals into the {{ placeholders }} and the response body is the finished PDF — no PHP PDF library or headless browser required.

Can I use cURL in PHP to call the invoice PDF API?

Yes. Build the JSON payload with curl_init()/curl_setopt_array(), set the Authorization: Bearer, Content-Type, and Accept: application/pdf headers, and set CURLOPT_RETURNTRANSFER so curl_exec() returns the raw PDF bytes to write with file_put_contents().

How do I keep the same invoice layout but change the data per request?

Keep the HTML template constant across requests and only change the data object — invoice number, customer, line items, subtotal, tax, total, and due date. The layout stays identical while each call produces a different invoice.

How is invoice PDF generation billed?

Per document, not per page. A one-page invoice and a long itemized invoice with many line items are billed the same, and passing an idempotencyKey (like the invoice number) means a retried request never generates or bills the same invoice twice.

Related

Ready to generate invoice PDFs from your PHP app?

Get an API key