Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to Convert TEXT file to PDF Online in Node.js
// How to Convert TEXT to PDF file in Node.js using REST API
const convert = async () => {
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
const settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "nodejs-testing/sample-file.txt";
settings.format = "pdf";
settings.outputPath = "nodejs-testing/sample-file.pdf";
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
await convertApi.convertDocument(request);
catch (err) {
throw err;
.then(() => {
console.log("Successfully converted TEXT file to PDF file format.");
.catch((err) => {
console.log("Error occurred while converting the PDF file:", err);

You can convert text file to pdf document programmatically on the cloud. In this article, you will learn how to convert text file to pdf format in Node.js using REST API.

The following topics are covered in this article:

  1. TEXT to PDF Conversion REST API and Node.js SDK
  2. Upload TEXT File on the Cloud
  3. How to Convert TEXT to PDF file in Node.js using REST API
  4. Download the Converted PDF file
// construct FileApi to download converted file
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create donwload file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("nodejs-testing/sample-file.pdf", myStorage);
// download file and response type Stream
.then(function (response) {
// save file in your system directory
fs.writeFile("H:\\groupdocs-cloud-data\\sample-file.pdf", response, "binary", function (err) { });
console.log("Expected response type is Stream: " + response.length);
.catch(function (error) {
console.log("Error: " + error.message);
// open file in IOStream from your system drive.
var resourcesFolder = 'H:\\groupdocs-cloud-data\\sample-file.txt';
// 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-file.txt", fileStream, myStorage);
// upload file
.then(function (response) {
console.log("Expected response type is FilesUploadResult: " + response.uploaded.length);
.catch(function (error) {
console.log("Error: " + error.message);
# Import Node.js SDK in your node application from
global.groupdocs_conversion_cloud = require("groupdocs-conversion-cloud");
global.fs = require("fs");
// get clientId and clientSecret from (free registration is required).
global.clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
global.clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
global.myStorage = "test-internal-storage";
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment