# LangChain → PDF with makesPDF

> For AI agents: a documentation index is available at https://makespdf.com/llms.txt — every docs page also has a markdown twin (append .md to the page URL), including this one: https://makespdf.com/docs/integrations/langchain.md

The `makespdf[langchain]` extra exposes the
[makesPDF](https://makespdf.com/?ref=langchain) hosted API as two LangChain
tools: `makespdf_render_md` for Markdown and `makespdf_render` for a stored
template or inline DSL. Both write a temporary `.pdf` and return its path.

## Install

```bash
pip install "makespdf[langchain]"
export MAKESPDF_API_KEY=... # mint at https://makespdf.com/settings/api-keys?ref=langchain
```

The LangChain import happens inside the factory, so `import makespdf` never
imports a framework. A missing extra raises `ImportError` naming the pip extra.

## The two tools

| Tool | Endpoint | Arguments |
| --- | --- | --- |
| `makespdf_render_md` | `POST /api/v1/md` | `markdown` |
| `makespdf_render` | `POST /api/v1/render` | `template_id` or `dsl`, plus `data_json` |

`data_json` is a JSON object string for `{{vars}}` — here `{"name": "Ada"}`
overrides the script's `sampleData`. Pass `template_id="invoice-classic"`
instead of `dsl` to render the stored library template. Both tools return the
path of a temp `.pdf`.

## Example

```python
from makespdf import MakesPDFClient
from makespdf.integrations.demo import MARKDOWN_DEMO, DSL_DEMO, DATA_DEMO
from makespdf.integrations.langchain import get_langchain_tools

tools = get_langchain_tools(MakesPDFClient())  # [makespdf_render_md, makespdf_render]
print(tools[0].invoke({"markdown": MARKDOWN_DEMO}))  # markdown -> PDF path
print(tools[1].invoke({"dsl": DSL_DEMO, "data_json": '{"name": "Ada"}'}))  # DSL -> PDF path
```

`get_langchain_tools()` takes an optional `MakesPDFClient` (a key from the
environment is the default) and returns `[makespdf_render_md,
makespdf_render]`. LangGraph agents consume the same tools directly — see the
[LangGraph guide](https://makespdf.com/docs/integrations/langgraph).

## Demo data

Every framework page here renders the same shared demo values, defined once in
`makespdf.integrations.demo`, so the examples stay directly comparable:

```python
MARKDOWN_DEMO = "# Invoice INV-2026-001\n\nHello **Ada** — total **$7,095.00**.\n"

DSL_DEMO = (
    'const template = doc({ size: "A4" }, page(col(s("Hello {{name}}"))));\n'
    'const sampleData = { name: "World" };\n'
)

DATA_DEMO = {"name": "Ada"}

TEMPLATE_ID_DEMO = "invoice-classic"
```

## Billing and limits

Billing matches the underlying endpoint: both `/md` and `/render` cost 1 credit
per 10 pages on success, and every failure path deducts zero. Markdown input is
capped at 200KB of text — inline `data:` images are excluded from that budget,
the request body is capped at 15MB — and inline DSL at 512KB. Rate limit:
10,000 renders/hour per caller.
