How to Convert Email to PDF with JavaScript in NodeJS

Convert Email (.eml) files to PDF with JavaScript in NodeJS using a multipart pdfRest API request.
Share this page

This NodeJS walkthrough uses JavaScript, Axios, and multipart form data. It converts an Email (.eml) file into a PDF through the pdfRest Convert to PDF API Tool. The complete sample is included below so JavaScript in NodeJS developers can see the file handling, request construction, and response path together.

Why Email to PDF with JavaScript in NodeJS?

NodeJS services frequently collect messages from webhooks, support tooling, and customer-upload flows. Converting an .eml input to PDF lets the service expose a stable document attachment without depending on the recipient’s email client.

A customer-success application could receive a mailed escalation, create the PDF through its Node worker, and make the resulting resource available with the associated support record. Reviewers gain a consistent document while the original mail data remains available to the application.

The asynchronous Axios flow surfaces the resource JSON through the normal promise path, so an application can queue a download, attach the ID to a case, or continue into another PDF operation after the request resolves.

JavaScript in NodeJS Code Example for Email to PDF

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 an Email (.eml) file to PDF by sending it directly in a
// multipart /pdf request.
var inputPath = "/path/to/sample.eml";
var form = new FormData();
form.append("file", fs.createReadStream(inputPath), { contentType: "message/rfc822" });
form.append("output", "pdf_from_email");

axios.post(apiUrl + "/pdf", 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 Email to PDF example, JavaScript in NodeJS sends the multipart request to /pdf. 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.

Upload the Email File

// multipart /pdf request.
var inputPath = "/path/to/sample.eml";
var form = new FormData();
form.append("file", fs.createReadStream(inputPath), { contentType: "message/rfc822" });
form.append("output", "pdf_from_email");

axios.post(apiUrl + "/pdf", 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; });

The JavaScript in NodeJS request places the Email source in the multipart part named file. Retain its .eml filename extension because Convert to PDF uses the uploaded name to identify the source type, then use the optional output field to choose the generated resource name without a PDF extension.

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 Email to PDF 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 Email to PDF 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

In this JavaScript in NodeJS tutorial, you converted an Email file into a PDF resource with a direct multipart request. The pattern gives a message-based workflow a document output that can be reviewed, downloaded, or passed to another pdfRest operation.

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

The repository also provides a JSON-payload Email-to-PDF version for JavaScript in NodeJS. It uploads the message first and then posts its resource ID to /pdf, which is useful when an application stages inputs for several later operations. 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.