# Document to Markdown & RAG Chunks (PDF, DOCX, PPTX, XLSX, OCR) (`zangado/document-to-markdown-rag`) Actor

Convert PDF, Word, PowerPoint, Excel, HTML and images to clean, LLM-ready Markdown and heading-aware chunks. OCR for scanned pages in English, Portuguese and Spanish.

- **URL**: https://apify.com/zangado/document-to-markdown-rag.md
- **Developed by:** [Josicleuton da Silva Braga](https://apify.com/zangado) (community)
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $1.50 / 1,000 page processeds

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Document to Markdown & RAG Chunks (PDF, DOCX, PPTX, XLSX, OCR)

Turn **PDF, Word (DOCX), PowerPoint (PPTX), Excel (XLSX), HTML, TXT/CSV and images** into clean, **LLM-ready Markdown** and **heading-aware chunks** for RAG, vector databases, AI agents and search — in one call.

- **Tables preserved** as Markdown tables (Word, Excel, HTML).
- **Smart OCR** for scanned PDFs and images (English, Portuguese, Spanish). In *Auto* mode only pages that are really scanned are OCR'd — you never pay OCR prices for normal text pages.
- **RAG-ready chunks** split on headings, with the heading path (`["Contract", "Clause 3"]`) and configurable size and overlap.
- **Page markers** (`<!-- page 3 -->`) in PDF output, so answers can cite pages.
- **Fair billing**: if no text can be extracted (e.g. a logo image), the document is **not charged**.
- **Fail-safe batches**: a broken link or unsupported file is reported in the dataset and the run continues.
- Works great from **AI agents via the Apify MCP server**, LangChain, LlamaIndex, n8n, Make and Zapier.

### Input

| Field | Description | Default |
|---|---|---|
| `startUrls` | Public URLs of documents | — |
| `outputFormat` | `both`, `markdown` or `chunks` | `both` |
| `ocr` | `auto` (scanned pages only), `always`, `never` | `auto` |
| `chunkSize` | Max characters per chunk (~4 chars per token) | 4000 |
| `chunkOverlap` | Characters repeated between chunks | 400 |
| `maxPagesPerDocument` | Skip longer documents to protect your budget | 500 |

```json
{
  "startUrls": [{ "url": "https://example.com/report.pdf" }],
  "outputFormat": "both",
  "ocr": "auto",
  "chunkSize": 4000,
  "chunkOverlap": 400
}
```

### Output (one item per document)

```json
{
  "url": "https://example.com/report.pdf",
  "fileName": "report.pdf",
  "kind": "pdf",
  "pages": 12,
  "ocrPages": 2,
  "chars": 31540,
  "markdown": "<!-- page 1 -->\n\n# Annual Report\n\n...",
  "chunks": [
    { "index": 0, "text": "# Annual Report\n\n...", "headings": ["Annual Report"], "chars": 3980 }
  ]
}
```

Failed items contain `url` and `error`. Items with no extractable text contain `warning` and are not charged.

### Pricing (pay per event)

- **Page** — each PDF page, image, or 3,000 characters of text from other formats.
- **OCR page** — a page that needed OCR (scanned PDF page or image).

You only pay for documents that produced text. Use `maxPagesPerDocument` and the run's maximum cost to stay in budget.

### Supported formats

PDF · DOCX · PPTX · XLSX · HTML/HTM · TXT · MD · CSV · PNG · JPG/JPEG · WEBP · TIFF · BMP · GIF

Files up to 50 MB. Password-protected PDFs are not supported.

### Use cases

- Build a **RAG knowledge base** from company PDFs, manuals and policies.
- Feed **AI agents** with documents found on the web.
- Convert **scanned invoices, receipts and contracts** to text.
- Migrate Office documents to Markdown for docs sites and wikis.

### Notes

Only process documents you have the right to use. Text is extracted as-is; complex layouts (multi-column magazines, forms) may need review.

# Actor input Schema

## `startUrls` (type: `array`):

Public URLs of PDF, DOCX, PPTX, XLSX, HTML, TXT/CSV/MD or image files (PNG, JPG, WEBP, TIFF).

## `outputFormat` (type: `string`):

Full Markdown, RAG-ready chunks, or both.

## `ocr` (type: `string`):

Auto: OCR only scanned PDF pages and images (recommended). Always: OCR every PDF page. Never: text layer only.

## `chunkSize` (type: `integer`):

Maximum characters per chunk (~4 characters per token).

## `chunkOverlap` (type: `integer`):

Characters repeated from the end of the previous chunk.

## `maxPagesPerDocument` (type: `integer`):

Skip documents longer than this (protects your budget).

## Actor input object example

```json
{
  "startUrls": [
    {
      "url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
    }
  ],
  "outputFormat": "both",
  "ocr": "auto",
  "chunkSize": 4000,
  "chunkOverlap": 400,
  "maxPagesPerDocument": 500
}
```

# Actor output Schema

## `documents` (type: `string`):

No description

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "startUrls": [
        {
            "url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
        }
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("zangado/document-to-markdown-rag").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "startUrls": [{ "url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" }] }

# Run the Actor and wait for it to finish
run = client.actor("zangado/document-to-markdown-rag").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "startUrls": [
    {
      "url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
    }
  ]
}' |
apify call zangado/document-to-markdown-rag --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,zangado/document-to-markdown-rag"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/KDVs9ZPqR8myJIAwI/builds/Ht68ZbaMp0WIGNHB7/openapi.json
