easypdfcloud-node
Advanced tools
Comparing version
@@ -30,13 +30,13 @@ import { FileData } from './FileData.js'; | ||
* @param {string | WorkflowSetting} workflow Workflow ID or workflow setting | ||
* @param {FileData} file Input file | ||
* @param {string | FileData} file Input file | ||
* @returns {Promise<Job>} Promise which resolves to newly created job | ||
*/ | ||
startNewJob(workflow: string | WorkflowSetting, file: FileData): Promise<Job>; | ||
startNewJob(workflow: string | WorkflowSetting, file: string | FileData): Promise<Job>; | ||
/** | ||
* Starts new job with multiple files | ||
* @param {string | WorkflowSetting} workflow Workflow ID or workflow setting | ||
* @param {FileData[]} files Input files | ||
* @param {string[] | FileData[]} files Input files | ||
* @returns {Promise<Job>} Promise which resolves to newly created job | ||
*/ | ||
startNewJobWithMultipleFiles(workflow: string | WorkflowSetting, files: FileData[]): Promise<Job>; | ||
startNewJobWithMultipleFiles(workflow: string | WorkflowSetting, files: string[] | FileData[]): Promise<Job>; | ||
} |
@@ -38,2 +38,6 @@ /* | ||
const NodeEnvironmentConfig_js_1 = require("./Config/NodeEnvironmentConfig.js"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const util = require("util"); | ||
const readFileAsync = util.promisify(fs.readFile); | ||
/** | ||
@@ -50,3 +54,3 @@ * easyPDF Cloud client | ||
* @param {string | WorkflowSetting} workflow Workflow ID or workflow setting | ||
* @param {FileData} file Input file | ||
* @param {string | FileData} file Input file | ||
* @returns {Promise<Job>} Promise which resolves to newly created job | ||
@@ -57,3 +61,11 @@ */ | ||
const restApi = this.restApi; | ||
const jobId = yield restApi.createNewJob(workflow, file, true); | ||
let fileData = null; | ||
if (file instanceof FileData_js_1.FileData) { | ||
fileData = file; | ||
} | ||
else { | ||
const fileBuffer = yield readFileAsync(file); | ||
fileData = new FileData_js_1.FileData(path.basename(file), Uint8Array.from(fileBuffer)); | ||
} | ||
const jobId = yield restApi.createNewJob(workflow, fileData, true); | ||
return new Job_js_1.Job(restApi, jobId); | ||
@@ -65,3 +77,3 @@ }); | ||
* @param {string | WorkflowSetting} workflow Workflow ID or workflow setting | ||
* @param {FileData[]} files Input files | ||
* @param {string[] | FileData[]} files Input files | ||
* @returns {Promise<Job>} Promise which resolves to newly created job | ||
@@ -72,3 +84,3 @@ */ | ||
const restApi = this.restApi; | ||
if (!files || files.length === 0) { | ||
if (files === null || typeof files === 'undefined' || files.length === 0) { | ||
throw new Error('No input files specified'); | ||
@@ -81,35 +93,80 @@ } | ||
const fileNameMap = {}; | ||
fileNameMap[files[0].name.toLowerCase()] = true; | ||
const jobId = yield restApi.createNewJob(workflow, files[0], false); | ||
try { | ||
for (let i = 1; i < fileCount; ++i) { | ||
const file = files[i]; | ||
let fileName = file.name; | ||
const fExtPos = fileName.lastIndexOf('.'); | ||
const fName = (fExtPos > 0) ? fileName.substr(0, fExtPos) : fileName; | ||
const fExt = (fExtPos > 0) ? fileName.substr(fExtPos) : ''; | ||
let fNameIndex = 0; | ||
while (true) { | ||
const fileNameLC = fileName.toLowerCase(); | ||
if (typeof fileNameMap[fileNameLC] === 'undefined') { | ||
fileNameMap[fileNameLC] = true; | ||
break; | ||
if (files[0] instanceof FileData_js_1.FileData) { | ||
const fileDataList = files; | ||
let fileData = fileDataList[0]; | ||
fileNameMap[fileData.name.toLowerCase()] = true; | ||
const jobId = yield restApi.createNewJob(workflow, fileData, false); | ||
try { | ||
for (let i = 1; i < fileCount; ++i) { | ||
fileData = fileDataList[i]; | ||
let fileName = fileData.name; | ||
const fExtPos = fileName.lastIndexOf('.'); | ||
const fName = (fExtPos > 0) ? fileName.substr(0, fExtPos) : fileName; | ||
const fExt = (fExtPos > 0) ? fileName.substr(fExtPos) : ''; | ||
let fNameIndex = 0; | ||
while (true) { | ||
const fileNameLC = fileName.toLowerCase(); | ||
if (typeof fileNameMap[fileNameLC] === 'undefined') { | ||
fileNameMap[fileNameLC] = true; | ||
break; | ||
} | ||
++fNameIndex; | ||
fileName = fName + ' (' + fNameIndex + ')' + fExt; | ||
} | ||
++fNameIndex; | ||
fileName = fName + ' (' + fNameIndex + ')' + fExt; | ||
const newFileData = new FileData_js_1.FileData(fileName, fileData.blob); | ||
yield restApi.uploadInput(jobId, newFileData); | ||
} | ||
const fileData = new FileData_js_1.FileData(fileName, file.blob); | ||
yield restApi.uploadInput(jobId, fileData); | ||
yield restApi.startJob(jobId); | ||
} | ||
yield restApi.startJob(jobId); | ||
catch (e) { | ||
try { | ||
yield restApi.deleteJob(jobId); | ||
} | ||
catch (eInner) { | ||
} | ||
throw e; | ||
} | ||
return new Job_js_1.Job(restApi, jobId); | ||
} | ||
catch (e) { | ||
else { | ||
const filePathList = files; | ||
let filePath = filePathList[0]; | ||
let fileName = path.basename(filePath); | ||
fileNameMap[fileName.toLowerCase()] = true; | ||
let fileBuffer = yield readFileAsync(filePath); | ||
let fileData = new FileData_js_1.FileData(fileName, Uint8Array.from(fileBuffer)); | ||
const jobId = yield restApi.createNewJob(workflow, fileData, false); | ||
try { | ||
yield restApi.deleteJob(jobId); | ||
for (let i = 1; i < fileCount; ++i) { | ||
filePath = filePathList[i]; | ||
fileName = path.basename(filePath); | ||
const fExtPos = fileName.lastIndexOf('.'); | ||
const fName = (fExtPos > 0) ? fileName.substr(0, fExtPos) : fileName; | ||
const fExt = (fExtPos > 0) ? fileName.substr(fExtPos) : ''; | ||
let fNameIndex = 0; | ||
while (true) { | ||
const fileNameLC = fileName.toLowerCase(); | ||
if (typeof fileNameMap[fileNameLC] === 'undefined') { | ||
fileNameMap[fileNameLC] = true; | ||
break; | ||
} | ||
++fNameIndex; | ||
fileName = fName + ' (' + fNameIndex + ')' + fExt; | ||
} | ||
fileBuffer = yield readFileAsync(filePath); | ||
fileData = new FileData_js_1.FileData(fileName, Uint8Array.from(fileBuffer)); | ||
yield restApi.uploadInput(jobId, fileData); | ||
} | ||
yield restApi.startJob(jobId); | ||
} | ||
catch (eInner) { | ||
catch (e) { | ||
try { | ||
yield restApi.deleteJob(jobId); | ||
} | ||
catch (eInner) { | ||
} | ||
throw e; | ||
} | ||
throw e; | ||
return new Job_js_1.Job(restApi, jobId); | ||
} | ||
return new Job_js_1.Job(restApi, jobId); | ||
}); | ||
@@ -116,0 +173,0 @@ } |
{ | ||
"name": "easypdfcloud-node", | ||
"version": "0.9.11", | ||
"version": "0.9.12", | ||
"description": "A Node library for easyPDF Cloud API", | ||
@@ -5,0 +5,0 @@ "main": "dist-cjs/easypdfcloud-node.js", |
115
README.MD
@@ -29,6 +29,4 @@ # easypdfcloud-node | ||
You can create a workflow by connecting series of workflow tasks with mouse drag & drop from [easyPDF Cloud](https://www.easypdfcloud.com) website. After creating a new workflow, you can get its workflow ID from the [developer information page](https://www.easypdfcloud.com/developer/). | ||
You can create a workflow by connecting series of workflow tasks with mouse drag & drop from [easyPDF Cloud](https://www.easypdfcloud.com) website. After creating a new workflow, you can get its workflow ID from the [developer information page](https://www.easypdfcloud.com/developer/), and then pass it to the API. | ||
#### Promise | ||
```javascript | ||
@@ -43,38 +41,2 @@ "use strict"; | ||
// Create easyPDF Cloud client object | ||
const client = new easyPdfCloud.Client(clientConfig); | ||
// Load input file | ||
const inputFileBuffer = fs.readFileSync('/Users/someone/input.docx'); | ||
const inputFileData = new easyPdfCloud.FileData('input.docx', Uint8Array.from(inputFileBuffer)); | ||
// Upload input file and start new job | ||
console.log('Start new job ...'); | ||
client.startNewJob('<workflow ID>', inputFileData).then(function(job) { | ||
// Wait until job execution is completed | ||
console.log('Wait for job execution completion ...'); | ||
job.waitForJobExecutionCompletion().then(function(jobExecutionResult) { | ||
// Save output to file | ||
const fileData = jobExecutionResult.fileData; | ||
console.log('Output: ' + fileData.name + ' (' + fileData.getDataByteSize() + ' bytes)'); | ||
fs.writeFileSync('/Users/someone/output.pdf', fileData.blob, 'binary'); | ||
}, function(err) { | ||
console.log(err); | ||
}); | ||
}, function(err) { | ||
console.log(err); | ||
}); | ||
``` | ||
#### async/await | ||
```javascript | ||
"use strict"; | ||
const fs = require('fs'); | ||
const easyPdfCloud = require('easypdfcloud-node'); | ||
// easyPDF Cloud API credentials | ||
const clientConfig = new easyPdfCloud.ClientConfig('<client ID>', '<client secret>'); | ||
(async () => { | ||
@@ -84,18 +46,12 @@ // Create easyPDF Cloud client object | ||
// Load input file | ||
const inputFileBuffer = fs.readFileSync('/Users/someone/input.docx'); | ||
const inputFileData = new easyPdfCloud.FileData('input.docx', Uint8Array.from(inputFileBuffer)); | ||
// Upload input file and start new job | ||
console.log('Start new job ...'); | ||
const job = await client.startNewJob('<workflow ID>', inputFileData); | ||
const job = await client.startNewJob('<workflow ID>', '/Users/someone/input.docx'); | ||
// Wait until job execution is completed | ||
console.log('Wait for job execution completion ...'); | ||
const jobExecutionResult = await job.waitForJobExecutionCompletion(); | ||
const result = await job.waitForJobExecutionCompletion(); | ||
// Save output to file | ||
const fileData = jobExecutionResult.fileData; | ||
console.log('Output: ' + fileData.name + ' (' + fileData.getDataByteSize() + ' bytes)'); | ||
fs.writeFileSync('/Users/someone/output.pdf', fileData.blob, 'binary'); | ||
fs.writeFileSync('/Users/someone/output.pdf', result.fileData.blob, 'binary'); | ||
})(); | ||
@@ -106,6 +62,6 @@ ``` | ||
Alternatively, you can create a workflow on the spot and pass it to the API. Below example creates a workflow that adds a custom watermark text to a PDF. | ||
Alternatively, you can create a workflow on the spot and pass it to the API. | ||
<br><br> | ||
Following example creates a workflow that adds a watermark to a PDF. | ||
#### Promise | ||
```javascript | ||
@@ -120,49 +76,2 @@ "use strict"; | ||
// Create easyPDF Cloud client object | ||
const client = new easyPdfCloud.Client(clientConfig); | ||
// Load input file | ||
const inputFileBuffer = fs.readFileSync('/Users/someone/input.pdf'); | ||
const inputFileData = new easyPdfCloud.FileData('input.pdf', Uint8Array.from(inputFileBuffer)); | ||
// Dynamically create workflow | ||
const workflowSetting = new easyPdfCloud.WorkflowSetting(); | ||
// Add "Add watermark" task | ||
const addWatermarkToPdfTask = workflowSetting.AddNewAddWatermarkToPdfTask(); | ||
// Configure watermark settings | ||
addWatermarkToPdfTask.Angle = 45; | ||
addWatermarkToPdfTask.OutlineOnly = true; | ||
addWatermarkToPdfTask.Text = 'Hello'; | ||
// Upload input file and start new job | ||
console.log('Start new job ...'); | ||
client.startNewJob(workflowSetting, inputFileData).then(function(job) { | ||
// Wait until job execution is completed | ||
console.log('Wait for job execution completion ...'); | ||
job.waitForJobExecutionCompletion().then(function(jobExecutionResult) { | ||
// Save output to file | ||
const fileData = jobExecutionResult.fileData; | ||
console.log('Output: ' + fileData.name + ' (' + fileData.getDataByteSize() + ' bytes)'); | ||
fs.writeFileSync('/Users/someone/output.pdf', fileData.blob, 'binary'); | ||
}, function(err) { | ||
console.log(err); | ||
}); | ||
}, function(err) { | ||
console.log(err); | ||
}); | ||
``` | ||
#### async/await | ||
```javascript | ||
"use strict"; | ||
const fs = require('fs'); | ||
const easyPdfCloud = require('easypdfcloud-node'); | ||
// easyPDF Cloud API credentials | ||
const clientConfig = new easyPdfCloud.ClientConfig('<client ID>', '<client secret>'); | ||
(async () => { | ||
@@ -172,6 +81,2 @@ // Create easyPDF Cloud client object | ||
// Load input file | ||
const inputFileBuffer = fs.readFileSync('/Users/someone/input.pdf'); | ||
const inputFileData = new easyPdfCloud.FileData('input.pdf', Uint8Array.from(inputFileBuffer)); | ||
// Dynamically create workflow | ||
@@ -190,3 +95,3 @@ const workflowSetting = new easyPdfCloud.WorkflowSetting(); | ||
console.log('Start new job ...'); | ||
const job = await client.startNewJob(workflowSetting, inputFileData); | ||
const job = await client.startNewJob(workflowSetting, '/Users/someone/input.pdf'); | ||
@@ -198,6 +103,4 @@ // Wait until job execution is completed | ||
// Save output to file | ||
const fileData = jobExecutionResult.fileData; | ||
console.log('Output: ' + fileData.name + ' (' + fileData.getDataByteSize() + ' bytes)'); | ||
fs.writeFileSync('/Users/someone/output.pdf', fileData.blob, 'binary'); | ||
fs.writeFileSync('/Users/someone/output.pdf', result.fileData.blob, 'binary'); | ||
})(); | ||
``` |
7955
0.72%361063
-0.13%99
-49.49%3
50%