refer from report-server-ts
Find a file
Sorawit Bholsithi bb6dbd8055 ๊Update Readme
2023-10-07 17:56:17 +07:00
libs Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
templates Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
test-run Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
ThaiFonts Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
.gitignore Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
app.ts Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
compose.yaml Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
Dockerfile Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
package-lock.json Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
package.json Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00
README.md ๊Update Readme 2023-10-07 17:56:17 +07:00
tsconfig.json Support TS, swagger,Template(xls,docx),PDF convert 2023-10-07 17:29:53 +07:00

report-server-ts

เป็น Web API ออกแบบมาเพื่อสร้างเอกสาร สามารถใช้ frontend โดยตรง เพื่อจะได้ไม่ต้องเขียนโปรแกรมทำเอกสารเฉพาะแต่ละตัวออกมา ปรับปรุงของเดิมจาก

Feature & Change

  • แก้ไขจากเดิมตัวเดิมเป็น JavaScript เป็น TypeScript ตัด pandoc ออก
  • ใช้ docx xlsx เป็น template เพื่อสร้างเอกสารคล้าย Mail Merge ลูกค้าออกแบบเองได้
  • รายงานเป็นไฟล์แบบเดียวกับ template หรือแปลงเป็น pdf หรือฟอร์แม็ตอื่นๆได้
  • แปลงไฟล์จาก MS Office เป็น PDF
  • โค้ดมีการ obfuscator
  • API document ด้วย swagger
  • มีโปรแกรมช่วยทดสอบ template แบบง่ายๆให้
  • Docker Image จะใช้แบบ Standalone หรือเป็น Microservice ร่วมกับโปรเจ็กอื่นๆได้ ใช้งานทันที

ติดตั้ง

clone project แล้วไปหัวข้อใช้งานได้เลย หัวนี้ไว้เพื่ออ้างอิงเท่านั้น ตั้งค่าของ TypeScript ตามเวปนี้ ให้ใช้ ES module ได้ด้วย

npm i express 
npm i -D typescript @types/express @types/node ts-node
npm i docx-templates xlsx-template-next swagger-ui-express swagger-jsdoc yaqrcode libreoffice-file-converter
npm i -D @types/swagger-ui-express @types/swagger-jsdoc
# obfuscate code tools
npm i -D javascript-obfuscator
# add type support for yaqrcode
cd node_modules/yaqrcode
wget https://raw.githubusercontent.com/zenozeng/node-yaqrcode/master/index.d.ts

การใช้งาน

ดู scripts ใน package.json และ compose.yaml

npm run dev
npm run build
npm run serve
npm run obfuscator
npm run preview
npm run build:docker
docker compose up -d

ทดสอบ template

ไปที่โฟลเดอร์ test-run มีโปรแกรมเพื่อทดสอบ template ที่ออกมามาก่อน ไปว่างในเซิร์ฟเวอร์ ใช้ค่า default ได้เลย แปลงไปไฟล์แบบต่างๆที่ Libreoffice รองรับได้ ควรทดสอบรูปแบบข้อมูล(json) ให้เข้ากับ template(docx,xlsx) ถ้าเกิดปัญหา ถ้าค่าไม่ครบ template แบบ docx จะ error log ที่เซิร์ฟเวอร์ ส่วน xlsx ไม่แจ้งปัญหา แค่ไม่แสดงค่านั้นๆ คู่มือการใช้งานที่สมบูรณ์ให้ไปที่เวปของ library ที่ใช้ docx-templates และ xlsx-template-next

$ npx ts-node docx-template.ts 
Output extension(docx,pdf,odt): pdf
JSON data path(./docx.json): 
Base path of templates-docx(..): 

$ npx ts-node xlsx-template.ts 
Output extension(xlsx,pdf): ods
JSON data path(./xlsx.json): 
Base path of templates-docx(..):

Build docker

docker build -t docker.frappet.com/demo/report-server .
docker push docker.frappet.com/demo/report-server
docker run --name rserver -p 80:3000 docker.frappet.com/demo/report-server

Bun Note

เริ่มแรกในการพอร์ตจาก JavaScript ลองใช้ Bun(TypeScript) แทน Node.js ตัว Bun ค่อนข้างน่าประทับใจใช้ TypeScript โดยตรงไม่ต้องตั้งค่า หรือติดตั้งเพิ่ม แต่มีปัญหากับ libreoffice-file-converter ต้องแก้ค่าใน package.json

docker-template คาดว่าเป็นปัญหาจาก eval ทำให้ ส่วน EXEC กับ custom function ทำงานไม่ได้ เลยกลับมาใช้ node

Todo

  • รองรับ Authentication Header เพื่อให้ยูสเซอร์ในระบบใช้งานได้เท่านั้น
  • หาทางสร้างเอกสารจาก text เช่น Markdown เป็นเอกสาร MS Office