How to Convert PDF to PostScript with JavaScript in NodeJS

Convert PDF documents to PostScript with JavaScript in NodeJS and configure page, scaling, annotation, and output settings.
Share this page

This NodeJS walkthrough uses JavaScript, Axios, and multipart form data. It converts a PDF into PostScript through /postscript with visible output controls for a production workflow. The complete sample is included below so JavaScript in NodeJS developers can see the file handling, request construction, and response path together.

Why PDF to PostScript with JavaScript in NodeJS?

A NodeJS integration can treat PostScript as a generated output for the systems that still require it, while keeping PDF as the format handled by upstream users and web applications. The sample sends exactly one PDF input and returns a new PostScript resource.

For a managed print queue, a worker can set page selection and output options before delivering the PostScript result to a specialized renderer. That avoids relying on browser print behavior or manually configured desktop clients.

The response object is useful beyond display: its output ID is the durable handle for download, cleanup, or a following conversion step while the resource remains retained.

JavaScript in NodeJS Code Example for PDF to PostScript

var axios = require("axios");
var fs = require("fs");
var FormData = require("form-data");

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

/* This sample converts PDF to PostScript through multipart /postscript.
 * Pairing this endpoint with /pdf creates a PDF -> PostScript -> PDF workflow commonly
 * called PDF refrying. Some print and prepress workflows use it to rebuild, flatten, or
 * normalize page content, but the lossy roundtrip can discard PDF-specific features.
 * These settings request Level 3, text-safe output, all pages at original scale,
 * shrink-to-fit without rotation, and printable annotations.
 */
var inputPath = "/path/to/sample.pdf";
var form = new FormData();
form.append("file", fs.createReadStream(inputPath), { contentType: "application/pdf" });
form.append("ps_level", "3");
form.append("page_range", "all");
form.append("binary_output", "false");
form.append("scale", "1");
form.append("rotate", "false");
form.append("shrink_to_fit", "true");
form.append("print_annotations", "true");
form.append("output", "postscript_from_pdf");

axios.post(apiUrl + "/postscript", form, { headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", ...form.getHeaders() }, maxBodyLength: Infinity })
.then((response) => { console.log(JSON.stringify(response.data, null, 2)); })
.catch((error) => { console.error(error.response ? error.response.data : error.message); process.exitCode = 1; });

Source: View the sample on GitHub

Breaking Down the Code

Configure the service and input

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

For this PDF to PostScript example, JavaScript in NodeJS sends the multipart request to /postscript. The NodeJS samples use axios for HTTP, form-data for multipart encoding, and Node’s file-system module for local inputs. Install the packages listed in the repository’s JavaScript package manifest when adapting the example. The sample’s placeholder path and API key make the moving pieces visible; replace them with a real local path and protected runtime credential before using the request.

Select PostScript Output Settings

var inputPath = "/path/to/sample.pdf";
var form = new FormData();
form.append("file", fs.createReadStream(inputPath), { contentType: "application/pdf" });
form.append("ps_level", "3");
form.append("page_range", "all");
form.append("binary_output", "false");
form.append("scale", "1");
form.append("rotate", "false");
form.append("shrink_to_fit", "true");
form.append("print_annotations", "true");
form.append("output", "postscript_from_pdf");

The JavaScript in NodeJS sample explicitly requests ps_level=3, all pages, binary output, no rotation, unit scale, shrink-to-fit, and printable annotations. Those choices describe the new PostScript result; they do not edit or replace the uploaded PDF.

Build the multipart request

var axios = require("axios");
var fs = require("fs");
var FormData = require("form-data");

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

For the PDF to PostScript form, FormData appends a readable file stream and the text options. Its generated headers include the multipart boundary, so the request combines form.getHeaders() with the Api-Key header rather than setting a boundary by hand.

Read the output resource

var axios = require("axios");
var fs = require("fs");
var FormData = require("form-data");

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work

After the PDF to PostScript call, axios resolves successful responses to a JSON object and sends API failures to the catch block. The sample prints either the output-resource details or the returned error payload, which is a useful starting point for production logging.

Beyond the Tutorial

This JavaScript in NodeJS example submitted a PDF to /postscript and selected the controls that shape the resulting PostScript file. Retain the returned resource ID whenever the output needs to be downloaded, monitored, or used by a following workflow step.

Try representative pdf to postscript inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the pdfRest API Reference Guide. The same request pattern can then be adapted to your JavaScript in NodeJS application’s error handling, retention policy, and delivery workflow.

For JavaScript in NodeJS applications that already hold the source as a managed resource, the repository includes a JSON-payload PDF-to-PostScript example. It uploads the PDF separately and sends the returned ID with the PostScript options. View the JSON-payload sample on GitHub.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.