diff --git a/UsesCases/Parser/parserGetFormsAsFDF.js b/UsesCases/Parser/parserGetFormsAsFDF.js new file mode 100644 index 00000000..e9ac2de5 --- /dev/null +++ b/UsesCases/Parser/parserGetFormsAsFDF.js @@ -0,0 +1,23 @@ +import { ParserHelper, pdfApi } from "./parserHelper.js"; +import path from 'node:path'; + +export {ParseExportFormsAsFDF}; + +const ParseExportFormsAsFDF = { + async export(documentName, outputFDFName, localFolder, remoteFolder) { + if ( pdfApi ) { + await ParserHelper.uploadDocument(documentName, localFolder, remoteFolder); + + const fdfPath = path.join(remoteFolder, outputFDFName) + const response = await pdfApi.putExportFieldsFromPdfToFdfInStorage( documentName, fdfPath, null, remoteFolder ); + + if (response.body.code == 200) { + console.log("ParseExportFormsAsFDF(): Pdf document '" + documentName + "' form fields successfully exported to '" + outputFDFName + "' file!"); + await ParserHelper.downloadResult(outputFDFName, localFolder, remoteFolder, ""); + } + else + console.error("ParseExportFormsAsFDF(): Unexpected error!") + + } + } +}; \ No newline at end of file diff --git a/UsesCases/Parser/parserGetFormsAsXML.js b/UsesCases/Parser/parserGetFormsAsXML.js new file mode 100644 index 00000000..bbbf8fcf --- /dev/null +++ b/UsesCases/Parser/parserGetFormsAsXML.js @@ -0,0 +1,23 @@ +import { ParserHelper, pdfApi } from "./parserHelper.js"; +import path from 'node:path'; + +export {ParseExportFormsAsXML}; + +const ParseExportFormsAsXML = { + async export(documentName, outputXMLName, localFolder, remoteFolder) { + if ( pdfApi ) { + await ParserHelper.uploadDocument(documentName, localFolder, remoteFolder); + + const xmlPath = path.join(remoteFolder, outputXMLName) + const response = await pdfApi.putExportFieldsFromPdfToXmlInStorage( documentName, xmlPath, null, remoteFolder ); + + if (response.body.code == 200) { + console.log("ParseExportFormsAsXML(): Pdf document '" + documentName + "' form fields successfully exported to '" + outputXMLName + "' file!"); + await ParserHelper.downloadResult(outputXMLName, localFolder, remoteFolder, ""); + } + else + console.error("ParseExportFormsAsXML(): Unexpected error!") + + } + } +}; \ No newline at end of file diff --git a/UsesCases/Parser/parserGetImages.js b/UsesCases/Parser/parserGetImages.js new file mode 100644 index 00000000..508fa464 --- /dev/null +++ b/UsesCases/Parser/parserGetImages.js @@ -0,0 +1,29 @@ +import { ParserHelper, pdfApi } from "./parserHelper.js"; +import fs from 'node:fs/promises'; +import path from 'node:path'; + +export {ParseExportImages}; + +const ParseExportImages = { + async export(documentName, pageNumber, localFolder, remoteFolder) { + if ( pdfApi ) { + await ParserHelper.uploadDocument(documentName, localFolder, remoteFolder); + + const response = await pdfApi.getImages( documentName, pageNumber, null, remoteFolder ); + + if (response.body.code == 200) { + + response.body.images.list.forEach(async function (image) { + const responseImage = await pdfApi.getImageExtractAsPng(documentName, image.id, null, null, null, remoteFolder); + + const filePath = path.join(localFolder, image.id + ".png"); + await fs.writeFile(filePath, responseImage.body); + console.log("Downloaded: " + filePath); + }); + } + else + console.error("ParseExportImages(): Unexpected error!") + + } + } +}; \ No newline at end of file diff --git a/UsesCases/Parser/parserGetTables.js b/UsesCases/Parser/parserGetTables.js new file mode 100644 index 00000000..11480aa5 --- /dev/null +++ b/UsesCases/Parser/parserGetTables.js @@ -0,0 +1,37 @@ +import { ParserHelper, pdfApi } from "./parserHelper.js"; +import fs from 'node:fs/promises'; +import path from 'node:path'; + +export {ParseExportTables}; + +const ParseExportTables = { + async export(documentName, localFolder, remoteFolder) { + if ( pdfApi ) { + await ParserHelper.uploadDocument(documentName, localFolder, remoteFolder); + + const response = await pdfApi.getDocumentTables( documentName, null, remoteFolder ); + + if (response.body.code == 200) { + console.log("ParseExportTables(): Tables successfully extracted!"); + + var result = "[\n"; + await Promise.all( + response.body.tables.list.map(async (table) => { + const responseTable = await pdfApi.getTable(documentName, table.id, null, remoteFolder) + .then(function(responseTable){ + result += JSON.stringify(responseTable.body.table) + ",\n\n"; + }); + }) + ); + result += "]"; + + const filePath = path.join(localFolder, "parsed_tables_output.json"); + await fs.writeFile(filePath, result); + console.log("Downloaded: " + filePath); + } + else + console.error("ParseExportTables(): Unexpected error!") + + } + } +}; \ No newline at end of file diff --git a/UsesCases/Parser/parserGetTextBoxes.js b/UsesCases/Parser/parserGetTextBoxes.js new file mode 100644 index 00000000..9bf1763e --- /dev/null +++ b/UsesCases/Parser/parserGetTextBoxes.js @@ -0,0 +1,37 @@ +import { ParserHelper, pdfApi } from "./parserHelper.js"; +import fs from 'node:fs/promises'; +import path from 'node:path'; + +export {ParseExportTextBoxes}; + +const ParseExportTextBoxes = { + async export(documentName, localFolder, remoteFolder) { + if ( pdfApi ) { + await ParserHelper.uploadDocument(documentName, localFolder, remoteFolder); + + const response = await pdfApi.getDocumentTextBoxFields( documentName, null, remoteFolder ); + + if (response.body.code == 200) { + console.log("ParseExportTextBoxes(): TextBox Fileds successfully extracted!"); + + var result = "[\n"; + await Promise.all( + response.body.fields.list.map(async (textbox) => { + const responseText = await pdfApi.getTextBoxField(documentName, textbox.fullName, null, remoteFolder) + .then(function(responseTextBox){ + result += JSON.stringify(responseTextBox.body.field) + ",\n\n"; + }); + }) + ); + result += "]"; + + const filePath = path.join(localFolder, "parsed_text_boxes_output.json"); + await fs.writeFile(filePath, result); + console.log("Downloaded: " + filePath); + } + else + console.error("ParseExportTextBoxes(): Unexpected error!") + + } + } +}; \ No newline at end of file diff --git a/UsesCases/Parser/parserHelper.js b/UsesCases/Parser/parserHelper.js new file mode 100644 index 00000000..8eb3f8f3 --- /dev/null +++ b/UsesCases/Parser/parserHelper.js @@ -0,0 +1,42 @@ +import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" } +import fs from 'node:fs/promises'; +import path from 'node:path'; +import { PdfApi } from "../../src/api/api.js"; + +export { configParams, pdfApi, ParserHelper }; + +const configParams = { + LOCAL_FOLDER: "C:\\Samples\\", + PDF_DOCUMENT_NAME: "sample.pdf", + REMOTE_FOLDER: 'TempPdfCloud', + + XML_OUTPUT_FILE: "output_sample.xml", + FDF_OUTPUT_FILE: "output_sample.fdf", + + PAGE_NUMBER: 1, + +}; + +const pdfApi = new PdfApi(credentials.id, credentials.key); + +const ParserHelper = { + async uploadFile (fileName, localFolder, tempFolder) { + const fileNamePath = path.join(localFolder, fileName); + const fileData = await fs.readFile(fileNamePath); + const storagePath = path.join(tempFolder, fileName); + await pdfApi.uploadFile(storagePath, fileData) + .then(() => console.log("File: '" + fileName +"' successfully uploaded.")); + }, + + async uploadDocument(document, localFolder, tempFolder) { + await this.uploadFile(document, localFolder, tempFolder) + }, + + async downloadResult(document, localFolder, tempFolder, prefix) { + const fileName = path.join(tempFolder, document); + const changedPdfData = await pdfApi.downloadFile(fileName); + const filePath = path.join(localFolder, prefix + document); + await fs.writeFile(filePath, changedPdfData.body); + console.log("Downloaded: " + filePath); + }, +}; \ No newline at end of file diff --git a/UsesCases/Parser/parserLaunch.js b/UsesCases/Parser/parserLaunch.js new file mode 100644 index 00000000..08cf405e --- /dev/null +++ b/UsesCases/Parser/parserLaunch.js @@ -0,0 +1,26 @@ +import { configParams } from "./parserHelper.js"; +import { ParseExportFormsAsXML } from "./parserGetFormsAsXML.js" +import { ParseExportFormsAsFDF } from "./parserGetFormsAsFDF.js"; +import { ParseExportImages } from "./parserGetImages.js"; +import { ParseExportTables } from "./parserGetTables.js"; +import { ParseExportTextBoxes } from "./parserGetTextBoxes.js"; + +async function main() { + try { + + await ParseExportFormsAsXML.export(configParams.PDF_DOCUMENT_NAME, configParams.XML_OUTPUT_FILE, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER); + + await ParseExportFormsAsFDF.export(configParams.PDF_DOCUMENT_NAME, configParams.FDF_OUTPUT_FILE, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER); + + await ParseExportImages.export(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER); + + await ParseExportTables.export(configParams.PDF_DOCUMENT_NAME, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER); + + await ParseExportTextBoxes.export(configParams.PDF_DOCUMENT_NAME, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER); + + } catch (error) { + console.error("Error:", error.message); + } +} + +await main(); \ No newline at end of file