---
title: REST API – Retrieve a Document’s PDF
description: "This page describes how to retrieve, from a third-party application, the PDF of a business document (invoice, credit note, quote, order, delivery note, payslip…) exactly as it is generated by Kafinea, using the print template of your choice. By default,…"
url: "https://www.kafinea.com/en/help/api/rest-api-retrieve-a-documents-pdf/"
updated: "2026-11-22"
category: API
---

# REST API – Retrieve a Document’s PDF

This page describes how to retrieve, from a third-party application, the PDF of a business document (invoice, credit note, quote, order, delivery note, payslip…) exactly as it is generated by Kafinea, using the print template of your choice.

By default, when the document is an **invoice** or a **credit note** and the conditions are met, the returned PDF automatically contains the **Factur-X** file: no additional parameter is required (see the "Electronic invoicing" section below).

A single operation is required:

| Operation | Method | Description |
| --- | --- | --- |
| `document_pdf` | GET | Retrieve a document's PDF |

It requires a valid session (see [Authentication](https://www.kafinea.com/en/help/api/rest-api-authentication/)).

> **Name change:** this operation used to be called `mds_document_pdf`. Both names work
>  until **November 30, 2026**, with exactly the same parameters and response: simply
>  replace the value of `operation` in your calls.

---

## document_pdf – Retrieve the PDF

Generates the PDF of a record and returns it, either base64-encoded in the JSON response, or directly as a PDF file.

> **GET** `https://apps.kafinea.com/{instance}/webservice.php`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `operation` | string | yes | Must be `document_pdf` |
| `sessionName` | string | yes | Session identifier |
| `id` | string | yes | Webservice ID of the record (`6x54325`), as returned by `query` or `retrieve` |
| `templateId` | string | no | Webservice ID of the print template (`57x1088`). If absent, the module's default template is used |
| `download` | string | no | `1` to receive the PDF file directly instead of the JSON response |

> **ID convention:** as with all API operations, `id` and `templateId` expect the **full Webservice ID** (`6x54325`), the one returned by `query`, `retrieve` or `describe`. A raw identifier (`54325`) is rejected with code `INVALID_ID_ATTRIBUTE`.

### JSON response (default)

```bash
curl "https://apps.kafinea.com/YourKafinea/webservice.php?operation=document_pdf&sessionName=YOUR_SESSION_ID&id=6x54325&templateId=57x1088"
```

```json
{
  "success": true,
  "result": {
    "id": "6x54325",
    "module": "Invoice",
    "templateId": "57x1088",
    "filename": "Facture - 0053 - 2026-06-29.pdf",
    "mime": "application/pdf",
    "size": 970618,
    "base64": "JVBERi0xLjQKJeLjz9MK..."
  }
}
```

The `base64` field contains the PDF bytes. Decode it to reconstruct the file:

```php
$pdfBytes = base64_decode($response['result']['base64']);
file_put_contents($response['result']['filename'], $pdfBytes);
```

### Direct file download

With `download=1`, the response is no longer JSON: the PDF file is returned as is, with the `Content-Type: application/pdf` and `Content-Disposition: attachment` headers.

```bash
curl -o facture.pdf "https://apps.kafinea.com/YourKafinea/webservice.php?operation=document_pdf&sessionName=YOUR_SESSION_ID&id=6x54325&download=1"
```

> **Tip:** The `download=1` mode avoids the overhead of base64 encoding (about +33% in size): use it when archiving or forwarding the file as is.

---

## Finding a print template's ID

The `templateId` parameter is optional: without it, Kafinea uses the module's default template. If you want to enforce a specific template, retrieve its ID.

> **Reminder:** Print templates are a full-fledged Kafinea module, so they are **accessible via the API's generic operations** (`describe`, `query`, `retrieve`…). No dedicated operation is required.

```bash
curl --get "https://apps.kafinea.com/YourKafinea/webservice.php" 
  --data-urlencode "operation=query" 
  --data-urlencode "sessionName=YOUR_SESSION_ID" 
  --data-urlencode "query=SELECT id, templatename, basemodule, active FROM MdsPrintTemplate WHERE basemodule = 'Invoice';"
```

```json
{
  "success": true,
  "result": [
    { "id": "57x1088", "templatename": "Facture client", "basemodule": "Invoice", "active": "1" },
    { "id": "57x1093", "templatename": "Facture d'avancement client", "basemodule": "Invoice", "active": "1" }
  ]
}
```

The `basemodule` field indicates the module the template applies to. The returned ID (`57x1088`) can be used as is in the `templateId` parameter.

An empty result means the module has no print template: PDF export is then not possible for that module.

---

## Affected modules

The operation works for **any module with at least one print template**, including:

- business management documents: quotes, customer orders, invoices, credit notes, supplier orders;
- logistics documents: delivery notes, receiving notes;
- payslips;
- any module for which you have created your own print templates.

To check whether a module is eligible, query the print templates associated with that module (see above): if at least one exists, PDF export is available.

---

## Electronic invoicing (Factur-X)

The returned PDF is **identical to the one produced from the interface**: it therefore automatically contains, with no parameter to add to your call, the `factur-x.xml` file when all the conditions below are met.

| Condition | Detail |
| --- | --- |
| Document type | Customer invoice or customer credit note |
| Locking | The document must be permanently locked (final document) |
| Instance configuration | The electronic invoicing format must be set to Factur-X |

The resulting file is then a PDF/A-3: readable like an ordinary PDF, it embeds as an attachment the standardized XML file that can be processed by the recipient's accounting system.

> **Note:** An unlocked document is considered a draft: its PDF carries a watermark and does not contain a Factur-X file. This is expected behavior – a non-final invoice has no legal value.

**Checking for the presence of the Factur-X file:**

```bash
curl -o facture.pdf "https://apps.kafinea.com/YourKafinea/webservice.php?operation=document_pdf&sessionName=YOUR_SESSION_ID&id=6x54325&download=1"
strings facture.pdf | grep factur-x.xml
```

---

## Access rights

The operation applies **the rights of the integration account's user**: if this user does not have permission to view the document, the call is denied. Create a user dedicated to the integration and assign it a profile granting access only to the necessary modules.

---

## Error codes

| Code | Meaning |
| --- | --- |
| `INVALID_ID_ATTRIBUTE` | `id` or `templateId` parameter missing, malformed, or not matching the correct module |
| `ACCESS_DENIED` | The logged-in user does not have permission to view this document (or the document does not exist) |
| `RECORD_NOT_FOUND` | The document does not exist (or has been deleted) |
| `PDF_NOT_SUPPORTED` | The module has no print template |
| `INVALID_TEMPLATE` | The requested template does not belong to the document's module |
| `PDF_GENERATION_FAILED` | PDF generation failed (the message specifies the cause) |

**Example error response:**

```json
{
  "success": false,
  "error": {
    "message": "Print template 57x1093 does not belong to module "Invoice".",
    "code": "INVALID_TEMPLATE"
  }
}
```

---

## Frequently asked questions

**How can I get an invoice's ID from its number?**

Use the `query` operation (see [Requests and querying](https://www.kafinea.com/en/help/api/rest-api-requests-and-querying/)):

```sql
SELECT id FROM Invoice WHERE invoice_no = 'FA-2026-0053';
```

The returned ID (`6x54325`) can be used directly as the `id` parameter.

> **Note:** The Webservice ID prefix (the number before the `x`) is specific to each instance: never hard-code it, always retrieve it via `query` or `describe`.

**Can I retrieve several PDFs in a single request?**

No: one call returns one document. Chain calls using the same session – since generating a PDF is resource-intensive, plan for sequential processing rather than massive parallelization.

**The returned PDF has a "Draft" watermark. Why?**

The document is not permanently locked. Lock it in Kafinea, then run the call again.

**Can I retrieve only the XML file of the electronic invoice?**

Yes, the `invoice_facturx` operation returns the XML alone for a locked invoice:

```bash
curl "https://apps.kafinea.com/YourKafinea/webservice.php?operation=invoice_facturx&sessionName=YOUR_SESSION_ID&id=6x54325"
```

This operation used to be called `mds_invoice_facturx`: both names work until
 November 30, 2026, with identical parameters and response.

---

## Related references 🔗

- [Introduction](https://www.kafinea.com/en/help/api/rest-api-introduction/)
- [Authentication](https://www.kafinea.com/en/help/api/rest-api-authentication/)
- [Requests and querying](https://www.kafinea.com/en/help/api/rest-api-requests-and-querying/)
- [File management](https://www.kafinea.com/en/help/api/rest-api-file-management/)
