-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-document.js
More file actions
97 lines (79 loc) · 3.78 KB
/
generate-document.js
File metadata and controls
97 lines (79 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pkg from 'windwardrestapi';
import fs from "fs";
const {WindwardClient, Template, Xml_10DataSource, OutputFormatEnum} = pkg;
const config = JSON.parse(fs.readFileSync('./config.json'));
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
async function main()
{
/**
* Step 1: INITIALIZE THE RESTFUL CLIENT AND SET UP THE TEMPLATE AND DATA SOURCE
* --------------------------------------------------------------------------------------------
*/
console.log("Initializing Fluent RESTful Client...");
// Create a new instance of the client using the RESTful Engine URL provided in the config file
// and pass the license to the client.
//
// The license should be specified in the config file
// If you don't have a license key, you can leave it blank and it will produce output with a watermark.
let client = new WindwardClient.WindwardClient(config['restful-engine-url'], config['license']);
// Display the version info for the restful engine
let version = await client.getVersionInfo();
console.log("Fluent RESTFul Engine Version: ", version);
// The datasource document I am sending to the engine to be processed
const dataSourcePath = './files/InvestmentFactSheet.xml';
// Create the object and pass in the datasource name, and the data file path.
let dataSource = new Xml_10DataSource('InvestmentFactSheet', undefined, dataSourcePath, undefined);
//The template file I wish to process.
const templatePath = './files/InvestmentFactSheet.docx';
//Create a new template object (to see all the input params check the documentation)
let template = new Template(OutputFormatEnum.PDF, [dataSource], undefined, templatePath,
undefined, undefined, undefined, undefined, undefined, undefined,
undefined, undefined, undefined, undefined, undefined, undefined);
/**
* Step 2: GENERATE THE DOCUMENT
* --------------------------------------------------------------------------------------------
*/
console.log("Generating Document...");
// Post document to the engine for processing
let document = await client.postDocument(template);
//check postDocument status and wait if not ready.
let documentReady = false;
while(true) {
await sleep(1000);
let status = await client.getDocumentStatus(document.Guid);
if (status == 302) {
// The document generation is complete, we can now proceed to retrieve the generated document
documentReady = true;
break;
}
else if (status == 201 || status == 202 || status == 404) {
// The document generation is still in progress, continue waiting
continue;
}
else {
// The document generation failed
console.error("Error retrieving document. Status code: ", status);
break;
}
}
if (!documentReady) {
console.error("Document generation did not complete successfully. Skipping retrieval.");
return;
}
/**
* Step 3: RETRIEVE THE DOCUMENT
* --------------------------------------------------------------------------------------------
*/
console.log("Retrieving Document...");
// Retrieve the generated document
let generatedDocument = await client.getDocument(document.Guid);
// Write the processed document to a file (ensure the file format matches the output format specified in the template)
fs.writeFile("./files/output.pdf", Buffer.from(generatedDocument.Data, "base64"), function(err){});
console.log("Generated document saved to /files/output.pdf");
// Delete the processed document from the engine
await client.deleteDocument(generatedDocument.Guid);
}
// Execute the main() method
main();