Development2026-04-08

Automate PDF to PNG Conversion with the API: cron, Node.js, and Python Examples

If new PDFs land in your S3 bucket every morning, or statements arrive over email every hour, the conversion step should be automated too. The moment a human has to drag and drop, you've started paying operational cost. This article walks through wiring the PDF to PNG Converter API into cron-driven Node.js and Python pipelines, with the pitfalls you'll actually hit.

End-to-end flow

  1. Issue an API key from the dashboard (format ptp_live_...)
  2. Call POST /api/v1/convert-token to get a single-use upload token
  3. POST your PDF to the returned upload_url
  4. Stream progress from the returned ws_url (or poll)
  5. Download PNG results once conversion is finished

One credit is consumed per successful conversion; failures auto-refund. Credits are ₩9,900 (~$7) for 1,000 conversions, no expiry.

Node.js example

// convert.mjs
import fs from "node:fs/promises";
import { Buffer } from "node:buffer";

const API_KEY = process.env.PDF2PNG_API_KEY;
const APP_BASE = "https://www.pdf2png.xyz";

async function convert(pdfPath) {
  // 1) Issue convert token
  const tokenRes = await fetch(`${APP_BASE}/api/v1/convert-token`, {
    method: "POST",
    headers: { Authorization: `Bearer ${API_KEY}` },
  });
  if (!tokenRes.ok) throw new Error(`token: ${tokenRes.status}`);
  const { convert_token, upload_url } = await tokenRes.json();

  // 2) Upload PDF
  const fileBytes = await fs.readFile(pdfPath);
  const form = new FormData();
  form.append("file", new Blob([fileBytes]), "input.pdf");

  const uploadRes = await fetch(upload_url, {
    method: "POST",
    headers: { Authorization: `Bearer ${convert_token}` },
    body: form,
  });
  if (!uploadRes.ok) throw new Error(`upload: ${uploadRes.status}`);
  const { conversion_id } = await uploadRes.json();

  // 3) Poll or stream from ws_url
  return conversion_id;
}

const id = await convert("./report.pdf");
console.log("conversion id:", id);

In production you typically wrap steps (3)–(4) into the same function so the caller can await the final ZIP URL and push it to S3 or notify Slack.

Python example

# convert.py
import os, requests

API_KEY = os.environ["PDF2PNG_API_KEY"]
APP_BASE = "https://www.pdf2png.xyz"

def convert(pdf_path: str) -> str:
    # 1) Token
    r = requests.post(
        f"{APP_BASE}/api/v1/convert-token",
        headers={"Authorization": f"Bearer {API_KEY}"},
        timeout=10,
    )
    r.raise_for_status()
    payload = r.json()
    convert_token = payload["convert_token"]
    upload_url = payload["upload_url"]

    # 2) Upload
    with open(pdf_path, "rb") as fp:
        up = requests.post(
            upload_url,
            headers={"Authorization": f"Bearer {convert_token}"},
            files={"file": ("input.pdf", fp, "application/pdf")},
            timeout=120,
        )
    up.raise_for_status()
    return up.json()["conversion_id"]

if __name__ == "__main__":
    print(convert("report.pdf"))

Wire it into cron

A single crontab line will do. Run at 03:00 daily, glob yesterday's inbox, log to disk:

# crontab -e
# Daily 03:00, append all output to /var/log/pdf-convert.log
0 3 * * * /usr/local/bin/node /opt/jobs/convert-yesterday.mjs >> /var/log/pdf-convert.log 2>&1

convert-yesterday.mjs globs ~/inbox/YYYY-MM-DD/*.pdf and calls convert() per file. The same pattern moves trivially to Vercel cron, GitHub Actions, or AWS EventBridge.

Common pitfalls

  • Never hardcode the API key — environment variable or secret manager. If it leaks, rotate it from the dashboard immediately.
  • convert_token is single-use — one PDF per token. Reusing returns 401.
  • Out-of-credit returns 402 — make your cron alert on 402 so you don't silently drop jobs. Top up via the dashboard in seconds.
  • Large PDFs (100 MB) take time to upload — bump Node.js timeouts or use a streaming upload.

Issue an API key

Sign up, issue a key from the dashboard, top up ₩9,900 (~$7) for 1,000 conversions. Credits never expire.

Developer docs →
Automate PDF to PNG Conversion with the API: cron, Node.js, and Python Examples | PDF to PNG Converter