Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Word Documents to PDF using a REST API in Node.js
PDF is a preferred format to share important documents, and it is a common practice to share Word documents in a PDF format. Although Microsoft Office provides a built-in functionality to convert Word to PDF, you may need to convert your Word documents (DOC or DOCX) into PDF programmatically. As a Node.js developer, you can easily convert Word documents to PDF files in your Node.js applications on the cloud. In this article, you will learn how to convert Word documents to PDF using a REST API in Node.js.
The following topics shall be covered here:
1. Convert Word Documents to PDF using a REST API in Node.js
2. Word to PDF Conversion with Advanced Options
3. Convert Word to PDF with Watermark using Node.js
4. DOCX to PDF Conversion without using Cloud Storage
5. Convert Range of Pages from DOCX to PDF in Node.js
6. Convert Specific Pages of DOCX to PDF using 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 api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.docx";
settings.format = "pdf";
settings.outputPath = "sample.pdf";
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// api initialization
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// read input file
let file = fs.readFileSync('C:\\Files\\sample.docx');
// create convert reauest
let request = new groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", file);
// convert document
let result = await convertApi.convertDocumentDirect(request);
// write output file
fs.writeFile("C:\\Files\\sample_direct.pdf", result, "binary", function (err) { });
console.log("Document converted: " + result.length);
// api initialization
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.docx";
settings.format = "pdf";
// define PDF convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.fromPage = 1; // start page number
convertOptions.pagesCount = 2; // total pages to convert
settings.convertOptions = convertOptions
settings.outputPath = "two-pages.pdf";
// create convert request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// api initialization
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define conver settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.docx";
settings.format = "pdf";
// define convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.pages = [1,2]; // page numbers to convert
settings.convertOptions = convertOptions
settings.outputPath = "specific_pages.pdf";
// convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// api initialization
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.docx";
settings.format = "pdf";
// define DOCX load options
var loadOptions = new groupdocs_conversion_cloud.DocxLoadOptions();
loadOptions.password = "password"; // password
loadOptions.hideComments = true; // hide comments
loadOptions.hideWordTrackedChanges = true; // hide track changes
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.displayDocTitle = true; // display document title. Default: false
convertOptions.marginTop = 5; // top margin
convertOptions.marginLeft = 5; // left margin
convertOptions.marginRight = 5; // right margin
convertOptions.marginBottom = 5; // bottom margin
settings.convertOptions = convertOptions;
settings.outputPath = "converted_with_options.pdf";
// create convert document request
var request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
convertApi.convertDocument(request);
// api initialization
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.docx";
settings.format = "pdf";
settings.outputPath = "converted_with_watermark.pdf";
// define watermark options
let watermark = new groupdocs_conversion_cloud.WatermarkOptions();
watermark.text = "CONFIDENTIAL";
watermark.bold = true;
watermark.fontSize = 54;
watermark.color = "Gray";
watermark.background = false;
watermark.rotationAngle = 30;
watermark.left = 120;
watermark.top = 400;
// define pdf convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.watermarkOptions = watermark;
settings.convertOptions = convertOptions
// define convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create donwload file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("sample.pdf", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save in your working directory
fs.writeFile("C:\\Files\\sample.pdf", response, "binary", function (err) { });
// Open file in IOStream from local/disc.
var resourcesFolder = 'C:\\Files\\sample.docx';
// 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.docx", 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