The tutorial and the sample source code elaborate on generating the PDF file from email using PDF.co web APIs in JavaScript (Node js). The users can use this web API to convert the email files available on their local storage or the cloud, such as MSG or EML, into PDF files. Moreover, this API can extract the email attachments and embed them into resulting PDF files.

  1. Features of Generating PDF From Email Endpoint
  2. Endpoint Parameters
  3. Query Parameter
  4. Endpoint URL
  5. Generate PDF from Email using Javascript
  6. Generate PDF from Email using JS – Demo

Features of Generating PDF From Email Endpoint

The users can use this PDF from the Email endpoint of PDF.co web API to generate the PDF from an email file. This endpoint takes the URL link and other optional parameters to generate an output link to a PDF file containing the email attachments in various formats as per the users’ wishes. Moreover, the users can choose to download the file using various filing modules such as “fs” to store them on their local storage.

Endpoint Parameters

Following are the parameters of the PDF From Email endpoint:

1.    url

It is a required parameter, a string that contains the link to the source email file. The email file can be with an “EML or MSG” extension. Moreover, the users can also provide links to their other files hosted on online storage systems such as google drive, Dropbox, and PDF.co cloud storage. Also, they can upload their file temporarily before calling this API endpoint. The user can see more details on this topic in the “Upload and Manage Files” section of PDF.co.

2.    embedAttachments

It is an optional parameter, a string that takes boolean values, true or false, to embed attachments from the source email files into the output PDF file. Its value is set to “true” by default, and the users can explicitly make it “false” using this parameter to prevent it from embedding attachments.

3.    convertAttachments

It is an optional parameter, a string that takes boolean values, true or false, to convert the email attachments from the source email file. Its value is set to “true” by default, and the users can set this parameter to “false” to prevent the API from converting the email attachments and embedding them as the original files in the form of embedded pdf attachments. This API endpoint supports the commonly used attachments with “DOC, DOCX, HTML, PNG, JPG, etc.,” extensions and converts them into individual PDF files before merging them with the resulting PDF file. The API adds the unsupported file types as PDF attachments, and the users may require Adobe reader or any other document viewer to view those attachments.

4.    async

It is an optional parameter that the users can set to “true” to run the task asynchronously. It is mostly recommended for heavy documents.

5.    margins

It is an optional parameter containing the margins of the output PDF file. The users can set the margins as they do in CSS styling, such as they can write “2px, 2px, 2px, 2px” to adjust the top, right, bottom, and left margins.

6.    paperSize

It is an optional parameter to set the paper size of the output file. It is set to “Letter” by default. However, the users can set it to “Letter”, “Legal”, “Tabloid”, “Ledger”, “A0”, “A1”, “A2”, “A3”, “A4”, “A5”, “A6” or any custom size. The custom sizes can be in px (pixels), mm (millimeters), or in (inches). For instance, 100px, 200px to set width and height respectively.

7.    orientation

It is an optional parameter to define the orientation of the output file’s pages. The users can set it to “Portrait” or “Landscape”. By default, it is set to “Portrait.”

8.    profiles

This optional parameter must be a string. The users can use this parameter for setting additional configurations for tuning and extra options.

Note: The users can read more on the profiles and find their examples here.

9.    expiration

It is an optional parameter defining the output file’s link expiration in minutes. It is set to 60 minutes by default, and the users can set it to different periods depending on their subscription plan, and the files get automatically deleted from the cloud after this period. However, the users can permanently store them using the PDF.co built-in files storage.

Query Parameter

The query parameters are the parameters of the API endpoints that the users need to attach with the endpoint URLs. This endpoint does not require any query parameters.

Endpoint URL

Below is the endpoint Url to generate PDF from an Email file:

URL:  /v1/pdf/convert/from/email

Generate PDF from Email using Javascript

The following source code explains to users how to generate the PDF file from email files in EML and MSG format. This code converts the URL to an email file with the “EML” extension containing two attachments and the sample text to a PDF file. This code has both parameters “embedAttachments” and “convertAttachments” set to “true” to embed the attachments and also attach them as the original files. Moreover, the parameter “async” is set to “false” to run the task synchronously. Furthermore, this code explores the use of the “expiration” parameter to set the expiration limit of the resulting file. Finally, the users can see the output file’s size and URL in the terminal and use that URL to see the resulting PDF file or use the various filing modules such as “fs” to download the file contents and store them on their local storage.

Sample Code in JavaScript

Below is the sample to generate PDF from an email file:

 

var request = require('request');
var API_KEY = "********************";
var fileUrl = "https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/email-to-pdf/sample.eml";
var options = {
   'method': 'POST',
   'url': 'https://api.pdf.co/v1/pdf/convert/from/email',
   'headers': {
     'x-api-key': API_KEY
   },

   formData: {
    "url": fileUrl,
    "embedAttachments": "true",
    "convertAttachments": "true", 
    "name": "email-with-attachments",
    "async": "false",
    "encrypt": "false",
    "expiration" : "12"
   }

 };

 request(options, function (error, response) {
   if (error) throw new Error(error);
   console.log(response.body);
 });

Step-by-Step Guide to Generate PDF from Email

  1. Install the “request” module using “npm install request” and import it into the code.
  2. Set the package.json file to run and test the javascript file.
  3. Declare and initialize the “API_KEY” variable with the API key obtained from the PDF.co dashboard or sign up to get the API key. The users can create an env file to access the key in each file and hide it.
  4. Note: The users can visit here to sign into PDF.co. and use the available credits to call the API endpoints. Moreover, the users can sign in using their student accounts to get extra API credits.
  5. Declare and initialize the “fileUrl” variable with the source file URL. The users can then convert their email files by only changing this variable without going into the code and API details.
  6. Declare a variable named “options” and initialize it with the API options such as method, API endpoint URL, headers,  and the form data. The form data is an object that contains the endpoint parameter’s values. For instance, the “async” variable value is “false” in the sample code. The users can add the above parameters to this object and modify these variables according to their conversion needs.
  7. The final step is to call the imported request method with the above arguments and print the API response in the user’s terminal. The users can pass this response to file streams to download the resulting file contents.

Source File

Below is the screenshot of the source file taken from the API endpoint documentation:

Step-by-Step Guide to Generate PDF from Email

Output File

Below are the screenshots of the output file obtained in the API response:

Generate PDF in JavaScript

PDF Generation in JavaScript

How to Generate PDF from Email in JavaScript

Generate PDF from Email using JS – Demo

Below is the gif to demonstrate the working of the code: