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

# PDF files

> Use PDF content for summarization, question answering, field extraction, table cleanup, and structured output.

PDF workflows are useful for contracts, papers, manuals, financial reports, product guides, and exported business documents. Extract the PDF content, pass the relevant context to a model, then ask for summaries, answers, or structured fields.

## Processing patterns

<Columns cols={2}>
  <Card title="Short documents" icon="file">
    Extract text and pass it directly as context when the document is short and well structured.
  </Card>

  <Card title="Long documents" icon="files">
    Split by section, page, or content chunk to make information easier to locate and reference in a conversation.
  </Card>
</Columns>

## Suggested workflow

<Steps>
  <Step title="Extract text and page references">
    Keep headings, page numbers, and table context so answers can cite their source.
  </Step>

  <Step title="Chunk long documents">
    Split large PDFs by topic or page range to avoid sending unrelated context.
  </Step>

  <Step title="Choose an output format">
    Use prose for summaries, JSON for extraction, and Markdown tables for table cleanup.
  </Step>
</Steps>

<Warning>
  Before uploading contracts, financial records, HR files, or customer data, confirm your data policy and use isolated API keys with quota and model restrictions.
</Warning>

## API example

For API calls, a common approach is to extract PDF text in your backend first, then include the relevant content in `messages.content`. The examples below read `contract.pdf`, summarize it, and extract key fields.

Put the PDF in the project root: the folder containing the `.py` or `.mjs` file and where you run the command. `PDF_PATH = "contract.pdf"` is the filename to read. If your file is named `invoice.pdf`, change it to `PDF_PATH = "invoice.pdf"`. The name, extension, and letter case must match the file exactly. Use the full local path when the file is outside the project root. You do not upload the PDF separately: the code reads the file and sends its extracted text in the request.

`PdfReader` is not built into Python. It is imported from the `pypdf` package. In the Node.js example, `PDFParse` is imported from `pdf-parse`. Python and Node.js use different package managers: run the following command in your project folder for Python; for Node.js, choose either `npm` or `pnpm`.

<CodeGroup>
  ```bash macOS / Linux theme={null}
  python3 -m pip install openai pypdf
  ```

  ```powershell Windows theme={null}
  py -m pip install openai pypdf
  ```
</CodeGroup>

<CodeGroup>
  ```bash npm theme={null}
  npm install openai pdf-parse
  ```

  ```bash pnpm theme={null}
  pnpm add openai pdf-parse
  ```
</CodeGroup>

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  # PdfReader comes from pypdf. It opens the PDF and lets you read page text.
  from pypdf import PdfReader

  client = OpenAI(
      api_key="sk-your-key",
      base_url="https://moxus.cloud/v1",
  )

  # Put contract.pdf in the project root. Replace it with your actual filename.
  PDF_PATH = "contract.pdf"
  reader = PdfReader(PDF_PATH)

  # page.extract_text() extracts text from one page. Empty pages fall back to an empty string.
  pdf_text = "\n\n".join(page.extract_text() or "" for page in reader.pages)

  response = client.chat.completions.create(
      model="gpt-5.4-mini",
      messages=[
          {
              "role": "user",
              "content": f"Summarize this PDF and extract the parties, amount, dates, and risk points:\n\nPDF file: {PDF_PATH}\n\n{pdf_text}",
          }
      ],
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  import OpenAI from "openai";
  import { readFile } from "node:fs/promises";

  // PDFParse converts a PDF buffer into extracted text.
  import { PDFParse } from "pdf-parse";

  const client = new OpenAI({
    apiKey: "sk-your-key",
    baseURL: "https://moxus.cloud/v1",
  });

  // Put contract.pdf in the project root. Replace it with your actual filename.
  const PDF_PATH = "contract.pdf";
  // readFile reads the local PDF. getText() returns the extracted text.
  const parser = new PDFParse({ data: await readFile(PDF_PATH) });
  const { text: pdfText } = await parser.getText();
  await parser.destroy();

  const response = await client.chat.completions.create({
    model: "gpt-5.4-mini",
    messages: [
      {
        role: "user",
        content: `Summarize this PDF and extract the parties, amount, dates, and risk points:\n\nPDF file: ${PDF_PATH}\n\n${pdfText}`,
      },
    ],
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

<Note>
  The Python example requires `openai` and `pypdf`. If `py` is unavailable on Windows, use `python -m pip install openai pypdf`. The Node.js example requires `openai` and `pdf-parse` installed through `npm` or `pnpm`. If the PDF is scanned, run OCR before sending the extracted text.
</Note>

## Next steps

* [Structured output](/en/guide/structured-output)
