Print Webpage to PDF API#

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 \ > 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' \

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

Node.js Website to PDF

const prerendercloud = require("prerendercloud");

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

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

// take PDF of with noPageBreaks, printBackground, and screen emulatedMedia
// all other options commented out shown for reference
const pdfBuffer = await prerendercloud.pdf("", {
  // 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 });