PodPDF logo

PodPDF

Project
0
by · 7 items · 6 scripts · 1 resource misc

Generate PDF

Script podpdf

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);
}

README

PodPDF

Turn HTML, Markdown, URLs and templates into PDFs with the PodPDF API.

Setup

  1. Sign up at app.podpdf.com. New accounts get 10 free PDFs; after that PDFs cost $0.01 each, from a credit pack or monthly plan.
  2. Create an API key under Settings → API Keys.
  3. In Windmill, add a podpdf resource and paste the key into api_key.
  4. Run Test Connection to check the key. It returns your account email.

Scripts

Every script takes the podpdf resource as its first argument.

ScriptWhat it does
Generate PDFConverts HTML, Markdown or a public URL to a PDF (up to 25 pages, 30 s). Returns a download link valid for 1 hour.
Render TemplateFills a PodPDF template with your data and renders it to a PDF. Returns a download link.
Start Long JobQueues a large HTML or Markdown document (up to 100 pages). Can wait up to about 3 minutes and return the download link.
Get JobReturns the status of a job.
Get Download LinkReturns a fresh 1-hour download link for a finished job.
Test ConnectionChecks 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]