Webpage/URL to PDF API#

Headless-Render-API has been in business since 2016. No credit card needed. Free tier includes 500 req/month, then it's $9/month for 20,000 req

Get a PDF of a URL, it's as simple as:

// module/import syntax shown here with async/await
// see the next example for commonjs/require syntax and promises
// npm install prerendercloud
import prerendercloud from "prerendercloud";
import fs from "fs";

(async () => {
  const buf = await prerendercloud.pdf("https://example.com");
  fs.writeFileSync("out.pdf", buf, { encoding: null });
})();

See all options in this Node.js snippet and more examples after it:

Node.js Example Usage with all options#

// https://github.com/sanfrancesco/prerendercloud-nodejs#pdfs
// npm install prerendercloud
const prerendercloud = require("prerendercloud");

// set your API key via env var PRERENDER_TOKEN or the following line:
// prerendercloud.set('prerenderToken', 'mySecretToken')

// take PDF of example.com with default options
prerendercloud
  .pdf("http://example.com")
  .then((pdfBuffer) =>
    fs.writeFileSync("out.pdf", pdfBuffer, { encoding: null })
  );

// take PDF of example.com with noPageBreaks, printBackground, and screen emulatedMedia
// all other options commented out shown for reference
const pdfBuffer = await prerendercloud.pdf("https://example.com/", {
  // Note: using noPageBreaks forces the following
  //  - pageRanges: "1",
  //  - preferCssPageSize: true
  noPageBreaks: true,
  printBackground: true,

  // emulatedMedia options: (screen, print, braille, embossed, handheld, projection, speech, tty, tv)
  emulatedMedia: "screen",

  // pageRanges: "1-3",
  // scale: 1.5,  // Default: 1, min: 0.1, max: 2.0
  // preferCssPageSize: true,
  // printBackground: true,
  // landscape: true,
  // marginTop: 1.75,
  // marginRight: 0.5,
  // marginBottom: 1.75,
  // marginLeft: 0.5,
  // paperWidth: 8.5,
  // paperHeight: 11,
});

fs.writeFileSync("out.pdf", pdfBuffer, { encoding: null });

Example Usage#

An HTTP "print to PDF" API for converting websites to PDF.

See cURL and Node.js examples below, but since it's a simple GET request to a standard HTTP API, you can use any language that can make HTTP requests

cURL Auth#

curl --header "X-Prerender-Token: secret-token

Node.js Auth#

Either use an env var (best practice):

PRERENDER_TOKEN=secret-token node app.js

or hard-code it (typically not a best practice to store secrets in code);

const prerendercloud = require("prerendercloud");
prerendercloud.set("prerenderToken", "mySecretToken");

cURL Examples#

Note: --compressed is a best practice for cURL, it tells cURL to use gzip compression, which is supported by the API (and virtually all HTTP servers)

cURL Website to PDF

curl --compressed \
  https://service.headless-render-api.com/pdf/https://example.com > out.pdf

Setting options in cURL via HTTP header

Set PDF options via HTTP headers, with cURL this would look like:

curl --compressed \
  --header 'Prerender-Emulated-Media: print' \
  https://service.headless-render-api.com/pdf/https://example.com

Note: http headers are case-insensitive

all PDF options as HTTP headers:

  • prerender-pdf-no-page-breaks
  • prerender-pdf-page-ranges
  • prerender-pdf-scale
  • prerender-pdf-prefer-css-page-size
  • prerender-pdf-print-background
  • prerender-pdf-landscape
  • prerender-pdf-margin-top
  • prerender-pdf-margin-right
  • prerender-pdf-margin-bottom
  • prerender-pdf-margin-left
  • prerender-pdf-paper-width
  • prerender-pdf-paper-height
  • prerender-emulated-media
  • prerender-wait-extra-long
  • prerender-dont-wait-for-web-sockets
  • prerender-block-cookies
  • origin-header-whitelist
  • Go here for full docs on these options