> ## Documentation Index
> Fetch the complete documentation index at: https://docs.markpdf.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Ejemplos

> Real use cases for Bun SDK: uploads, large PDFs, BYOS, RAG and ZIPs.

# Ejemplos

## Convert a user upload (`Bun.serve`)

```ts theme={null}
import { MarkpdfClient } from "@markpdf/bun";

const client = new MarkpdfClient({ apiKey: Bun.env.MARKPDF_API_KEY! });

Bun.serve({
  port: 3000,
  async fetch(req) {
    if (req.method !== "POST") return new Response("Method not allowed", { status: 405 });

    const form = await req.formData();
    const file = form.get("file") as File;

    const markdown = await client.convertBytes(await file.arrayBuffer(), file.name, {
      mode: "fast",
    });
    return new Response(markdown, { headers: { "content-type": "text/markdown" } });
  },
});
```

## Convert a batch of files to disk

```ts theme={null}
import { readdir } from "node:fs/promises";
import { MarkpdfClient } from "@markpdf/bun";

const client = new MarkpdfClient({ apiKey: Bun.env.MARKPDF_API_KEY! });
const files = (await readdir("./pdfs")).filter((f) => f.endsWith(".pdf"));

for (const file of files) {
  const markdown = await client.convertFile(`./pdfs/${file}`, { mode: "fast" });
  await Bun.write(`./out/${file.replace(".pdf", ".md")}`, markdown as string);
  console.log(`Convertido: ${file}`);
}
```

`Bun.write` complements `Bun.file`: the entire pipeline (reading PDF, sending to API, writing the result) avoids unnecessary buffering.

## PDF large with page range

```ts theme={null}
const markdown = await client.convertFromUrl(
  "https://bucket.example.com/manual-800-papages.pdf?sig=...",
  "manual.pdf",
  { pages: "120-145", mode: "fast" }
);
```

Combine it with [`pdfIndex`](/docs/sdks/bun/reference#pdfindex) to know what range to order without downloading the entire PDF client-side.

## BYOS: upload the result directly to your storage

```ts theme={null}
const result = await client.convertFromUrl(pdfUrl, "report.pdf", {
  responseFormat: "json",
  outputUrl: presignedPutUrl,     // pre-signed PUT to your bucket
  outputHeadUrl: presignedHeadUrl, // optional: detect cache hits
});

console.log(result.cached);     // true si ya existía y no se reprocesó
console.log(result.outputUrl);  // URL final del Markdown subido
```

With `outputUrl`, the API uploads the Markdown directly to your storage and returns a lightweight JSON instead of the full body. See [Compression](/docs/concepts/compression) for `outputEncoding`.

## Pipeline RAG with `pdfIndex`

```ts theme={null}
import { MarkpdfClient } from "@markpdf/bun";

const client = new MarkpdfClient({ apiKey: "YOUR_API_KEY" });
const PDF = "https://bucket.example.com/informe-anual.pdf?sig=...";

// 1. Index without converting the entire document
const spine = await client.pdfIndex(PDF);
console.log(`${spine.pageCount} pápages, ~${spine.estimatedTokensFull} full tokens`);

// 2. Choose the section of interest
const target = spine.sections.find((s) => s.text.includes("Resultados"))!;
const following = spine.sections.find((s) => s.page > target.page);
const endPage = following ? following.page - 1 : spine.pageCount;

// 3. Bring Markdown only from that section
const markdown = await client.convertFromUrl(PDF, "informe-anual.pdf", {
  pages: `${target.page}-${endPage}`,
  mode: "fast",
});
```

See [PDF Index for AI agents](/docs/concepts/pdf-index-for-ai-agents).

## Process a ZIP

```ts theme={null}
const result = await client.convertFile("./documents.zip", {
  inputFormat: "zip",
  responseFormat: "json",
});

console.log(result.markdown); // Markdown concatenated from the supported documents inside the ZIP
```

<Warning>
  The file size and quantity limits within ZIP are in [Limits](/docs/concepts/limits). An out-of-range ZIP returns `413`, which SDK converts to `MarkpdfPayloadTooLargeError`.
</Warning>
