Generate receipt PDFs in PHP

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

Receipt template

Any HTML works as a template. Use {{ placeholders }} for the order id, items, amount paid, payment method, and date:

<article style="font-family: sans-serif; max-width: 420px;">
  <header style="text-align:center;">
    <h1 style="margin-bottom:0;">Receipt</h1>
    <p style="color:#666;">Order {{ order_id }} &middot; {{ date }}</p>
  </header>

  <table style="width:100%; border-collapse:collapse; margin-top:16px;">
    <thead>
      <tr>
        <th style="text-align:left; border-bottom:1px solid #ddd;">Item</th>
        <th style="text-align:right; border-bottom:1px solid #ddd;">Price</th>
      </tr>
    </thead>
    <tbody>
      {{{ items }}}
    </tbody>
  </table>

  <p style="text-align:right; margin-top:12px;"><strong>Amount paid: {{ amount_paid }}</strong></p>
  <p style="text-align:right; color:#666;">Paid via {{ payment_method }}</p>
</article>

Generate the receipt PDF with cURL

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

<?php

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

$payload = [
    "source" => [
        "type" => "template",
        "template" => '<article style="font-family: sans-serif; max-width: 420px;">
  <header style="text-align:center;">
    <h1 style="margin-bottom:0;">Receipt</h1>
    <p style="color:#666;">Order {{ order_id }} &middot; {{ date }}</p>
  </header>

  <table style="width:100%; border-collapse:collapse; margin-top:16px;">
    <thead>
      <tr>
        <th style="text-align:left; border-bottom:1px solid #ddd;">Item</th>
        <th style="text-align:right; border-bottom:1px solid #ddd;">Price</th>
      </tr>
    </thead>
    <tbody>
      {{{ items }}}
    </tbody>
  </table>

  <p style="text-align:right; margin-top:12px;"><strong>Amount paid: {{ amount_paid }}</strong></p>
  <p style="text-align:right; color:#666;">Paid via {{ payment_method }}</p>
</article>',
        "data" => [
            "order_id" => "ORD-4821",
            "date" => "2026-07-14",
            "items" => '<tr><td>Espresso</td><td style="text-align:right;">$4.50</td></tr>'
                . '<tr><td>Croissant</td><td style="text-align:right;">$3.25</td></tr>',
            "amount_paid" => '$7.75',
            "payment_method" => 'Visa •••• 4242',
        ],
    ],
    "idempotencyKey" => "receipt-ord-4821",
];

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__ . "/receipt-ord-4821.pdf", $pdfBytes);

echo "Saved receipt-ord-4821.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: "<article>…{{ order_id }}…{{ items }}…{{ amount_paid }}…</article>",
    data: {
      order_id: "ORD-4821",
      date: "2026-07-14",
      items: "<tr><td>Espresso</td><td>$4.50</td></tr>",
      amount_paid: "$7.75",
      payment_method: "Visa •••• 4242"
    }
  },
  outputPath: "/tmp/receipt-ord-4821.pdf"
})

FAQ

How do I generate a receipt PDF in PHP?

POST a source of type "template" to https://docweave.dev/api/v1/pdf with your receipt HTML (using {{ placeholders }} for the order id, items, amount paid, payment method, and date) plus a data object filling those placeholders in. The response body is the finished receipt PDF — no headless browser to run on your server.

Can I reuse one receipt template for every order in PHP?

Yes. Keep the template HTML constant across requests and only change the data object per order — the order id, items, amount paid, and payment method. Every receipt renders with the same layout, so your PHP code stays a single reusable function.

What headers does a PHP receipt PDF request need?

Set Authorization: Bearer dw_live_your_key, Content-Type: application/json, and Accept: application/pdf. The Accept header tells Docweave to return raw PDF bytes in the response body, which you write straight to a file with file_put_contents().

How do I avoid double-charging a customer for a duplicate receipt PDF?

Pass an idempotencyKey (for example the order id) with every request. A retried request with the same key returns the already-generated receipt instead of rendering and billing a new one, and pricing is per document, not per page.

Related

Ready to generate receipt PDFs from your PHP app?

Get an API key