|
| 1 | +import credentials from "../../../../Credentials/credentials.json" with { type: "json" }; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { PdfApi } from "../../../src/api/api.js"; |
| 5 | +import { Table } from "../../../src/models/table.js"; |
| 6 | +import { Cell } from "../../../src/models/cell.js"; |
| 7 | +import { FontStyles } from "../../../src/models/fontStyles.js"; |
| 8 | +import { GraphInfo } from "../../../src/models/graphInfo.js"; |
| 9 | +import { Row } from "../../../src/models/row.js"; |
| 10 | +import { TextRect } from "../../../src/models/textRect.js"; |
| 11 | + |
| 12 | +const configParams = { |
| 13 | + LOCAL_FOLDER: "C:\\Samples\\", |
| 14 | + PDF_DOCUMENT_NAME: "sample.pdf", |
| 15 | + LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf", |
| 16 | + PAGE_NUMBER: 2, // Your document page number... |
| 17 | + TABLE_ROWS: 5, |
| 18 | + TABLE_COLUMNS: 5, |
| 19 | +}; |
| 20 | + |
| 21 | +const pdfApi = new PdfApi(credentials.id, credentials.key); |
| 22 | + |
| 23 | +const pdfTables = { |
| 24 | + async uploadDocument () { |
| 25 | + const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME); |
| 26 | + const pdfFileData = await fs.readFile(fileNamePath); |
| 27 | + await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData); |
| 28 | + console.log("File '" + configParams.PDF_DOCUMENT_NAME + "' successfully uploaded!"); |
| 29 | + }, |
| 30 | + |
| 31 | + async downloadResult () { |
| 32 | + const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME); |
| 33 | + const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME); |
| 34 | + await fs.writeFile(filePath, changedPdfData.body); |
| 35 | + console.log("Downloaded: " + filePath); |
| 36 | + }, |
| 37 | + |
| 38 | + initTable () { |
| 39 | + const numOfCols = configParams.TABLE_COLUMNS; |
| 40 | + const numOfRows = configParams.TABLE_ROWS; |
| 41 | + |
| 42 | + const headerTextState = { |
| 43 | + font: "Arial Bold", |
| 44 | + fontSize: 11, |
| 45 | + foregroundColor: { a: 0xFF, r: 0xFF, g: 0xFF, b: 0xFF }, |
| 46 | + fontStyle: FontStyles.Bold, |
| 47 | + }; |
| 48 | + |
| 49 | + const commonTextState = { |
| 50 | + font: "Arial Bold", |
| 51 | + fontSize: 11, |
| 52 | + foregroundColor: { a: 0xFF, r: 0x70, g: 0x70, b: 0x70 }, |
| 53 | + }; |
| 54 | + |
| 55 | + const table = new Table(); |
| 56 | + table.rows = []; |
| 57 | + |
| 58 | + let colWidths = ""; |
| 59 | + for (let colIndex = 0; colIndex < numOfCols; colIndex++) |
| 60 | + { |
| 61 | + colWidths += " 70"; |
| 62 | + } |
| 63 | + |
| 64 | + table.columnWidths = colWidths; |
| 65 | + |
| 66 | + const borderTableBorder = new GraphInfo(); |
| 67 | + borderTableBorder.color = { a: 0xFF, r: 0x00, g: 0xFF, b: 0x00 }; |
| 68 | + borderTableBorder.lineWidth = 0.5; |
| 69 | + |
| 70 | + table.defaultCellBorder = { |
| 71 | + top: borderTableBorder, |
| 72 | + right: borderTableBorder, |
| 73 | + bottom: borderTableBorder, |
| 74 | + left: borderTableBorder, |
| 75 | + roundedBorderRadius: 0 |
| 76 | + }; |
| 77 | + table.left = 150; |
| 78 | + table.top = 250; |
| 79 | + |
| 80 | + for (let rowIndex = 0; rowIndex < numOfRows; rowIndex++) |
| 81 | + { |
| 82 | + const row = new Row(); |
| 83 | + |
| 84 | + row.cells = []; |
| 85 | + |
| 86 | + for (let colIndex = 0; colIndex < numOfCols; colIndex++) |
| 87 | + { |
| 88 | + const cell = new Cell(); |
| 89 | + |
| 90 | + cell.defaultCellTextState = commonTextState; |
| 91 | + |
| 92 | + if (rowIndex == 0) // header cells |
| 93 | + { |
| 94 | + cell.backgroundColor = { a: 0xFF, r: 0x80, g: 0x80, b: 0x80 }; |
| 95 | + cell.defaultCellTextState = headerTextState; |
| 96 | + } |
| 97 | + else { |
| 98 | + cell.backgroundColor = { a: 0xFF, r: 0xFF, g: 0xFF, b: 0xFF }; |
| 99 | + }; |
| 100 | + |
| 101 | + const textRect = new TextRect(); |
| 102 | + if (rowIndex == 0) |
| 103 | + textRect.text = "header #" + colIndex; |
| 104 | + else |
| 105 | + textRect.text = "value #'(" + rowIndex + "," + colIndex + "')"; |
| 106 | + cell.paragraphs = [textRect]; |
| 107 | + |
| 108 | + row.cells.push(cell); |
| 109 | + } |
| 110 | + table.rows.push(row); |
| 111 | + } |
| 112 | + return table; |
| 113 | + }, |
| 114 | + |
| 115 | + async addTableOnPage () { |
| 116 | + const jsonTable = this.initTable(); |
| 117 | + |
| 118 | + const resultTabs = await pdfApi.postPageTables(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, [ jsonTable ]); |
| 119 | + |
| 120 | + if (resultTabs.body.code == 200) { |
| 121 | + console.log("Table successfully added!"); |
| 122 | + return resultTabs.body.table; |
| 123 | + } |
| 124 | + else |
| 125 | + comsole.error("Unexpected error : can't get links!!!"); |
| 126 | + }, |
| 127 | +} |
| 128 | + |
| 129 | +async function main() { |
| 130 | + try { |
| 131 | + await pdfTables.uploadDocument(); |
| 132 | + await pdfTables.addTableOnPage(); |
| 133 | + await pdfTables.downloadResult(); |
| 134 | + } catch (error) { |
| 135 | + console.error("Error:", error.message); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +main(); |
0 commit comments