Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Email and Outlook Messages to PDF using Node.js
You can easily convert emails and Outlook messages to PDF documents on the cloud. As a Node.js developer, you can convert EML and MSG files to PDF documents programmatically on the cloud. Here, you will learn how to convert EML and MSG files to PDF documents using a REST API in Node.js.
The following code examples are covered here:
1. API Configurations
2. Upload EML File on the Cloud
3. Convert EML to PDF using a REST API in Node.js
4. Download the converted PDF
5. MSG to PDF Conversion using a REST API in Node.js
6. Convert Email Attachments to PDF using REST API in Node.js
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
// initialize convert api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
var settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "sample.msg"; // input file
settings.format = "pdf"; // output format
settings.outputPath = "output.pdf";
// define eml load options
var loadOptions = new groupdocs_conversion_cloud.MsgLoadOptions();
loadOptions.convertAttachments = true;
settings.loadOptions = loadOptions;
// create convert document request
var request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
convertApi.convertDocument(request);
console.log("Document converted successfully: " + response[0].url);
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create donwload file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("output.pdf", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save in your working directory
fs.writeFile("C:\\Files\\output.pdf", response, "binary", function (err) { });
console.log(response);
// initialize convert api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
var settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "sample.eml"; // input file
settings.format = "pdf"; // output format
// define eml load options
var loadOptions = new groupdocs_conversion_cloud.EmlLoadOptions();
loadOptions.displayHeader = true; // display email header
loadOptions.displayFromEmailAddress = true; // display "from" email address
loadOptions.displayToEmailAddress = true; // display "to" email address
loadOptions.displayEmailAddress = true; // display email address
loadOptions.displayCcEmailAddress = true; // display "cc" email address
loadOptions.displayBccEmailAddress = true; // display "bcc" email address
settings.loadOptions = loadOptions;
// define pdf convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.centerWindow = true; // set position of the document's window. Default: false.
convertOptions.marginTop = 50; // top margin
convertOptions.marginLeft = 50; // left margin
settings.convertOptions = convertOptions;
settings.outputPath = "output.pdf";
// create convert document request
var request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
convertApi.convertDocument(request);
console.log("Document converted successfully: " + response[0].url);
// initialize convert api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
var settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "sample.msg"; // input file
settings.format = "pdf"; // output format
// define eml load options
var loadOptions = new groupdocs_conversion_cloud.MsgLoadOptions();
loadOptions.displayCcEmailAddress = false; // display "cc" email address
loadOptions.displayBccEmailAddress = false; // display "bcc" email address
settings.loadOptions = loadOptions;
settings.outputPath = "output.pdf";
// create convert document request
var request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
convertApi.convertDocument(request);
console.log("Document converted successfully: " + response[0].url);
// open file in IOStream from local/disc.
var resourcesFolder = 'C:\\Files\\sample.eml';
// read file
fs.readFile(resourcesFolder, (err, fileStream) => {
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create upload file request
var request = new groupdocs_conversion_cloud.UploadFileRequest("sample.eml", fileStream, myStorage);
// upload file
fileApi.uploadFile(request);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment