Skip to content

PDF

Generate, merge, split, OCR and watermark PDFs.

Overview

Base path: /v1/pdf
Python namespace: infra.pdf
text
# Python
from infrai import infra

# TypeScript
import { InfraiClient } from "@infrai/sdk";
const client = new InfraiClient({ apiKey: process.env.INFRAI_API_KEY! });
// → client.pdf

Methods

pdf.generate

POST /v1/pdf/generate

Generate a PDF from HTML, a URL or a template.

Python

python
infra.pdf.generate(html=None, url=None, template_id=None, format="A4", orientation="portrait", idempotency_key=None)

TypeScript

typescript
client.pdf.generate(opts: PdfGenerateOptions): Promise<PdfResult>

Parameters

NameTypeRequiredDescription
htmlstringOptionalHTML source to render.
urlstringOptionalSource URL to render or operate on.
template_idstringOptionalTemplate id to render instead of a body.
format"A4" | "Letter" | "Legal"OptionalPage size.
orientation"portrait" | "landscape"OptionalPage orientation.
idempotency_keystringOptionalOptional dedup key; identical retries return the same result.

Returns

PdfResult { pdf_url, bytes, pages }

Example

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

# Pass exactly one source: inline html, a remote url, or a template_id.
doc = infra.pdf.generate(
    html="<h1>Invoice #1042</h1><p>Total due: $42.00</p>",
    format="A4",
    orientation="portrait",
)
print("pdf url:", doc.get("url") or doc.get("pdf_url"))

pdf.merge

POST /v1/pdf/merge

Merge several PDFs into one.

Python

python
infra.pdf.merge(urls=[...])

TypeScript

typescript
client.pdf.merge(opts: { urls }): Promise<PdfResult>

Parameters

NameTypeRequiredDescription
urlsstring[]
Required
List of PDF URLs to merge.

Returns

PdfResult

Example

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

out = infra.pdf.merge(urls=["https://ex.com/a.pdf", "https://ex.com/b.pdf"])
print("merged url:", out.get("url") or out.get("pdf_url"))

pdf.split

POST /v1/pdf/split

Split a PDF by page ranges.

Python

python
infra.pdf.split(url=..., ranges=[...])

TypeScript

typescript
client.pdf.split(opts: { url, ranges }): Promise<{ parts }>

Parameters

NameTypeRequiredDescription
urlstring
Required
Source URL to render or operate on.
rangesstring[]
Required
Page ranges, e.g. 1-3.

Returns

{ parts: PdfResult[] }

Example

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

res = infra.pdf.split(url="https://ex.com/doc.pdf", ranges=["1-3", "4-"])
print("parts:", len(res.get("parts", [])))

pdf.ocr

POST /v1/pdf/ocr

Extract text from a PDF via OCR.

Python

python
infra.pdf.ocr(url=..., language=None)

TypeScript

typescript
client.pdf.ocr(opts: { url, language? }): Promise<{ text, pages }>

Parameters

NameTypeRequiredDescription
urlstring
Required
Source URL to render or operate on.
languagestringOptionalLanguage hint, e.g. en or zh.

Returns

{ text, pages: Array<{ text }> }

Example

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

res = infra.pdf.ocr(url="https://ex.com/scan.pdf", language="en")
print(res.get("text", "")[:200])

pdf.watermark

POST /v1/pdf/watermark

Apply a text or image watermark to a PDF.

Python

python
infra.pdf.watermark(url=..., text=None, image_url=None, opacity=None)

TypeScript

typescript
client.pdf.watermark(opts): Promise<PdfResult>

Parameters

NameTypeRequiredDescription
urlstring
Required
Source URL to render or operate on.
textstringOptionalWatermark text.
opacitynumberOptionalWatermark opacity from 0 to 1.

Returns

PdfResult

Example

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

out = infra.pdf.watermark(url="https://ex.com/doc.pdf", text="CONFIDENTIAL", opacity=0.2)
print("watermarked url:", out.get("url") or out.get("pdf_url"))