Convert HTML, Markdown or a public URL to a PDF with PodPDF (POST /quickjob, up to 25 pages / 30 s). Returns `download_url` (valid 1 hour). $0.01 per PDF; a paid plan or credit pack is required.
// PodPDF: Generate PDF (POST /quickjob). Up to 25 pages / 30 s; use start_long_job for bigger documents.
type Podpdf = { api_key: string };
const PODPDF_BASE_URL = "https://api.podpdf.com";
async function podpdfRequest(podpdf: Podpdf, method: string, path: string, body?: unknown): Promise<any> {
if (!podpdf?.api_key) throw new Error("PodPDF resource is missing api_key");
const res = await fetch(PODPDF_BASE_URL + path, {
method,
headers: {
"X-API-Key": podpdf.api_key,
Accept: "application/json",
...(body === undefined ? {} : { "Content-Type": "application/json" }),
},
body: body === undefined ? undefined : JSON.stringify(body),
});
const text = await res.text();
let json: any = undefined;
try {
json = text ? JSON.parse(text) : undefined;
} catch {
// non-JSON body
}
if (!res.ok) {
const err = json?.error;
const code = err?.code ?? `HTTP_${res.status}`;
const message = err?.message ?? (text || res.statusText);
const details = err?.details ? ` ${JSON.stringify(err.details)}` : "";
throw new Error(`PodPDF ${res.status} ${code}: ${message}${details}`);
}
return json;
}
function buildOptions(o: {
format?: string;
landscape?: boolean;
margin?: string;
printBackground?: boolean;
scale?: number;
pageRanges?: string;
}): Record<string, unknown> | undefined {
const options: Record<string, unknown> = {};
if (o.format) options.format = o.format;
if (typeof o.landscape === "boolean") options.landscape = o.landscape;
if (o.margin && o.margin.trim()) {
const m = o.margin.trim();
options.margin = { top: m, right: m, bottom: m, left: m };
}
if (typeof o.printBackground === "boolean") options.printBackground = o.printBackground;
if (typeof o.scale === "number" && !Number.isNaN(o.scale)) options.scale = o.scale;
if (o.pageRanges && o.pageRanges.trim()) options.pageRanges = o.pageRanges.trim();
return Object.keys(options).length ? options : undefined;
}
export async function main(
podpdf: Podpdf,
input_type: "html" | "markdown" | "url",
html?: string,
markdown?: string,
url?: string,
format?: "A4" | "Letter" | "Legal" | "A3" | "A5" | "Tabloid",
landscape?: boolean,
margin?: string,
printBackground?: boolean,
scale?: number,
pageRanges?: string,
) {
const content = input_type === "html" ? html : input_type === "markdown" ? markdown : url;
if (!content) throw new Error(`"${input_type}" is required when input_type is "${input_type}"`);
const body: Record<string, unknown> = { input_type, [input_type]: content };
const options = buildOptions({ format, landscape, margin, printBackground, scale, pageRanges });
if (options) body.options = options;
body.store = true;
// -> { job_id, pages, truncated, download_url, download_url_expires_at }
return await podpdfRequest(podpdf, "POST", "/quickjob", body);
}
Turn HTML, Markdown, URLs and templates into PDFs with the PodPDF API.
podpdf resource and paste the key into api_key.Every script takes the podpdf resource as its first argument.
| Script | What it does |
|---|---|
| Generate PDF | Converts HTML, Markdown or a public URL to a PDF (up to 25 pages, 30 s). Returns a download link valid for 1 hour. |
| Render Template | Fills a PodPDF template with your data and renders it to a PDF. Returns a download link. |
| Start Long Job | Queues a large HTML or Markdown document (up to 100 pages). Can wait up to about 3 minutes and return the download link. |
| Get Job | Returns the status of a job. |
| Get Download Link | Returns a fresh 1-hour download link for a finished job. |
| Test Connection | Checks the API key and returns the account's id, email and name. |
Generate PDF and Start Long Job also take optional page settings: format, landscape, margin, printBackground, scale and pageRanges.
If Generate PDF times out (QUICKJOB_TIMEOUT), use Start Long Job instead. Errors come back as PodPDF <status> <code>: <message>.
API docs: apidocs.podpdf.com · Support: [email protected]