Last active
June 22, 2021 08:02
-
-
Save groupdocs-cloud-gists/9f2bb8a0d667af15b420f8df908fa046 to your computer and use it in GitHub Desktop.
Convert Images to PDF using a REST API on the cloud in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Convert JPG Images to PDF documents using Node.js | |
1. Programmatically upload JPG image on the cloud | |
2. Convert uploaded JPG to PDF programmatically using Node.js | |
3. Download the converted PDF file from the cloud | |
4. JPG to PDF Conversion with Advanced Options | |
5. Convert JPG to PDF without using Cloud Storage | |
6. Convert JPG to PDF and Add Watermark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret); | |
// define convert settings | |
let settings = new groupdocs_conversion_cloud.ConvertSettings(); | |
settings.filePath = "sample.jpg"; | |
settings.format = "pdf"; | |
settings.outputPath = "output"; | |
// define watermark options | |
let watermark = new groupdocs_conversion_cloud.WatermarkOptions(); | |
watermark.text = "This is a Sample watermark"; | |
watermark.color = "White"; | |
watermark.width = 250; | |
watermark.height = 100; | |
watermark.background = false; | |
// define PDF convert options | |
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions(); | |
convertOptions.watermarkOptions = watermark; | |
settings.convertOptions = convertOptions | |
// create convert document request | |
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings); | |
let result = await convertApi.convertDocument(request); | |
console.log("Document converted successfully: " + result[0].url); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global.clientId = "659fe7da-715b-4744-a0f7-cf469a392b73"; | |
global.clientSecret = "b377c36cfa28fa69960ebac6b6e36421"; | |
global.myStorage = ""; | |
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret); | |
config.apiBaseUrl = "https://api.groupdocs.cloud"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret); | |
// define convert settings | |
let settings = new groupdocs_conversion_cloud.ConvertSettings(); | |
settings.filePath = "sample.jpg"; | |
settings.format = "pdf"; | |
settings.outputPath = "output"; | |
// convert document request | |
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings); | |
let result = await convertApi.convertDocument(request); | |
console.log("Document converted successfully: " + result[0].url); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret); | |
// input file | |
let file = fs.readFileSync('C:\\Files\\sample.jpg'); | |
// convert document direct request | |
let request = new groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", file); | |
// convert document | |
let result = await convertApi.convertDocumentDirect(request); | |
// save to the local path | |
fs.writeFile("C:\\Files\\sample.pdf", result, "binary", function (err) { }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret); | |
// define convert settings | |
let settings = new groupdocs_conversion_cloud.ConvertSettings(); | |
settings.storageName = myStorage; | |
settings.filePath = "sample.jpg"; | |
settings.format = "pdf"; | |
// define PDF convert options | |
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions(); | |
convertOptions.centerWindow = true; | |
convertOptions.compressImages = false; | |
convertOptions.dpi = 1024; | |
convertOptions.fitWindow = false; | |
convertOptions.grayscale = true; | |
convertOptions.imageQuality = 100; | |
convertOptions.marginTop = 5; | |
convertOptions.marginLeft = 5; | |
convertOptions.height = 4096; | |
settings.convertOptions = convertOptions; | |
settings.outputPath = "output"; | |
// create convert document request | |
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings); | |
let result = await convertApi.convertDocument(request); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// construct FileApi | |
let fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config); | |
// download file request | |
let request = new groupdocs_conversion_cloud.DownloadFileRequest("output\\sample.pdf", myStorage); | |
let response = await fileApi.downloadFile(request); | |
fs.writeFile("C:\\Files\\sample.pdf", response, "binary", function (err) { }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open file in IOStream from local/disc. | |
let resourcesFolder = 'C:\\Files\\sample.jpg'; | |
fs.readFile(resourcesFolder, (err, fileStream) => { | |
// construct FileApi | |
let fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config); | |
// create upload request | |
let request = new groupdocs_conversion_cloud.UploadFileRequest("sample.jpg", fileStream, myStorage); | |
fileApi.uploadFile(request); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment