url 2 pdf support, update test run, reduse imge size,

This commit is contained in:
oom 2025-02-25 19:50:21 +07:00
parent e921875bde
commit f6b68e4379
32 changed files with 3851 additions and 1108 deletions

View file

@ -1,7 +1,7 @@
import express from "express"
export const xlsxTemplateRoute = express.Router()
import { mimeToExtension, templateData } from "./report-template"
import { mimeToExtension, templateOption } from "./report-template"
import { ExcelTemplate } from "xlsx-template-next"
import fs from "fs"
import { LibreOfficeFileConverter } from "libreoffice-file-converter"
@ -11,26 +11,36 @@ const TEMPLATE_FOLDER_NAME = "templates/xlsx"
* xlsxTemplate Uses xlsx-template-next to convert input data and template to output buffer.
* You have to handle exception throw by function
* template keep in folder templates
* @param {String} base base path of caller relate to template-docx foler (no trail slash)
* @param {templateData} tdata Template Information in JSON format
* @param {Buffer|String} t template in buffer format or path to file
* @param {templateOption} tdata Template Information in JSON format
* @param {String} outputMediaType output extension
* @param {Number} tab tab page of spread sheet , default = 1
* @return {Promise<Uint8Array>} output buffer after apply template.
*/
export async function xlsxTemplateX(templateBuff: Buffer, tdata: templateData, outputMediaType: string = "xlsx", tab: number = 1): Promise<Uint8Array> {
export async function xlsxTemplateX(t: Buffer|String, tdata: templateOption, outputMediaType: string = "xlsx", tab: number = 1): Promise<Uint8Array> {
try {
const templateBuff = Buffer.isBuffer(t)?t: await fs.promises.readFile(String(t))
const template = new ExcelTemplate()
// let templateBuff = await fs.promises.readFile(`${base}/${TEMPLATE_FOLDER_NAME}/${tdata.template}.xlsx`)
await template.load(templateBuff)
await template.process(tab, tdata.data)
const buffer = await template.build({ type: "uint8array" })
if (outputMediaType === "xlsx") return buffer as Uint8Array
const buffer = await template.build({ type: "uint8array" }) as Uint8Array
if (outputMediaType === "xlsx") return buffer
const libreOfficeFileConverter = new LibreOfficeFileConverter({
childProcessOptions: {
timeout: 60 * 1000,
},
})
const lbuffer = await libreOfficeFileConverter.convertBuffer(Buffer.from(buffer as Uint8Array), outputMediaType)
//const lbuffer = await libreOfficeFileConverter.convertBuffer(Buffer.from(buffer as Uint8Array), outputMediaType)
const lbuffer = await libreOfficeFileConverter.convert({
buffer:Buffer.from(buffer),
format: outputMediaType,
input: "buffer",
output: "buffer"
})
return lbuffer
} catch (e) {
throw e
@ -88,7 +98,7 @@ xlsxTemplateRoute.get("/", async function (req, res) {
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/templateData'
* $ref: '#/components/schemas/templateOption'
* example:
* template: hello
* reportName: xlsx-report
@ -105,6 +115,10 @@ xlsxTemplateRoute.get("/", async function (req, res) {
* schema:
* type: string
* format: binary
* application/vnd.ms-excel:
* schema:
* type: string
* format: binary
* application/vnd.oasis.opendocument.spreadsheet:
* schema:
* type: string
@ -126,8 +140,6 @@ xlsxTemplateRoute.post("/", async function (req, res) {
if (!req.headers["content-type"] || !req.headers["accept"]) throw new Error("Require header content-type, accept")
let inputType = mimeToExtension(req.headers["content-type"])
let outputMediaType = mimeToExtension(req.headers["accept"])
console.log("content-type: ", inputType)
console.log("accept: ", outputMediaType)
let template = null
// Save the converted file to disk
if (req.query["folder"]) {
@ -142,10 +154,17 @@ xlsxTemplateRoute.post("/", async function (req, res) {
res.setHeader("Content-Length", buffer.length)
res.end(buffer)
} catch (ex) {
res.statusCode = 500
res.statusMessage = "Internal Server Error"
res.end(res.statusMessage)
console.error("Error during apply template: ", ex)
if(ex instanceof SyntaxError){
res.statusCode = 400
res.statusMessage = ex.message
res.end(res.statusMessage)
console.error("report-template/xlsx: ", ex)
}else{
res.statusCode = 500
res.statusMessage = "Internal Server Error during POST report-template/xlsx"
res.end(res.statusMessage)
console.error("report-template/xlsx: ", ex)
}
}
})
@ -246,7 +265,7 @@ xlsxTemplateRoute.post("/upload", async function (req, res) {
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/templateData'
* $ref: '#/components/schemas/templateOption'
* example:
* template: xlsx-report
* responses: