Generate report PDFs in PHP

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

Report template example

Any HTML works as a template. Use {{ placeholders }} for the report title, period, summary metrics, and a data table:

<article style="font-family: sans-serif;">
  <header>
    <h1>{{ title }}</h1>
    <p>Reporting period: {{ period }}</p>
  </header>

  <section style="display:flex; gap:32px; margin:20px 0;">
    <div>
      <div style="font-size:12px; color:#666;">Revenue</div>
      <div style="font-size:20px; font-weight:bold;">{{ revenue }}</div>
    </div>
    <div>
      <div style="font-size:12px; color:#666;">New customers</div>
      <div style="font-size:20px; font-weight:bold;">{{ new_customers }}</div>
    </div>
    <div>
      <div style="font-size:12px; color:#666;">Churn rate</div>
      <div style="font-size:20px; font-weight:bold;">{{ churn_rate }}</div>
    </div>
  </section>

  <h2>Summary</h2>
  <p>{{ summary }}</p>

  <h2>Detail</h2>
  <table style="width:100%; border-collapse:collapse;">
    <thead>
      <tr>
        <th style="text-align:left; border-bottom:1px solid #ccc;">Metric</th>
        <th style="text-align:right; border-bottom:1px solid #ccc;">This period</th>
        <th style="text-align:right; border-bottom:1px solid #ccc;">Last period</th>
      </tr>
    </thead>
    <tbody>
      {{{ rows }}}
    </tbody>
  </table>
</article>

Generate the report 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

$rowsHtml = "";
foreach ($report["metrics"] as $metric) {
    $rowsHtml .= sprintf(
        "<tr><td>%s</td><td style=\"text-align:right;\">%s</td>"
        . "<td style=\"text-align:right;\">%s</td></tr>",
        $metric["name"],
        $metric["current"],
        $metric["previous"]
    );
}

$template = <<<HTML
<article style="font-family: sans-serif;">
  <header>
    <h1>{{ title }}</h1>
    <p>Reporting period: {{ period }}</p>
  </header>
  <p>{{ summary }}</p>
  <table style="width:100%; border-collapse:collapse;">
    <tbody>{{{ rows }}}</tbody>
  </table>
</article>
HTML;

$payload = [
    "source" => [
        "type" => "template",
        "template" => $template,
        "data" => [
            "title" => "Q2 Sales Report",
            "period" => "Apr 1 - Jun 30, 2026",
            "revenue" => "$482,300",
            "new_customers" => "1,204",
            "churn_rate" => "2.1%",
            "summary" => "Revenue grew 18% quarter over quarter, led by the new self-serve plan.",
            "rows" => $rowsHtml,
        ],
    ],
    "idempotencyKey" => "report-2026-q2-sales",
];

$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__ . "/q2-sales-report.pdf", $pdfBytes);

echo "Saved q2-sales-report.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>{{ title }}</h1><p>{{ period }}</p><p>{{ summary }}</p>",
    data: { title: "Q2 Sales Report", period: "Apr 1 - Jun 30, 2026", summary: "Revenue grew 18% quarter over quarter." }
  },
  outputPath: "/tmp/q2-sales-report.pdf"
})

FAQ

How do I generate a report PDF in PHP?

POST an HTML report template and a data object to https://docweave.dev/api/v1/pdf with a source of type "template". Docweave binds your title, period, summary metrics, and detail rows 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 report 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 add a data table with variable row counts to a report PDF?

Build the row markup in PHP (one <tr> per metric or line) and pass the resulting HTML string as a single field, like rows, using triple braces ({{{ rows }}}) in the template so it's inserted unescaped instead of HTML-encoded.

How is report PDF generation billed?

Per document, not per page. A one-page summary and a multi-page report with a long data table are billed the same, and passing an idempotencyKey (like a report id and period) means a retried request never re-renders or re-bills the same report twice.

Related

Ready to generate report PDFs from your PHP app?

Get an API key