2023-10-03 22:11:16 +07:00
# report-server-ts
2023-10-07 17:29:53 +07:00
เป็น Web API ออกแบบมาเพื่อสร้างเอกสาร สามารถใช้ frontend โดยตรง เพื่อจะได้ไม่ต้องเขียนโปรแกรมทำเอกสารเฉพาะแต่ละตัวออกมา ปรับปรุงของเดิมจาก
เขียนใหม่เป็น TypeScript ตัด pandoc ออก เริ่มแรกลองใช้ bun แทน node รองรับ TypeScript โดยไม่ต้องตั้งค่า แต่มีปัญหากับ libreoffice-file-converter ต้องแก้ค่าใน package.json และ docker-template คาดว่าเป็นปัญหาจาก eval ทำให้ ส่วน EXEC กับ custom function ทำงานไม่ได้ เลยกลับมาใช้ node ตั้งค่าของ TypeScript [ตามเวปนี้ ](https://www.geeksforgeeks.org/how-to-use-express-in-typescript/ ) ให้ใช้ ES module ได้ด้วย
```bash
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
```bash
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 docx และ xlsx อย่างง่าย ใช้ค่า default ได้เลย สามารถทดสอบการแปลงไปไฟล์แบบต่างๆที่ Libreoffice รองรับได้ ควรทดสอบรูปแบบข้อมูล(json) ให้เข้ากับ template(docx,xlsx) ก่อนใช้งานเพราะ error log จะแสดงที่เซิร์เวอร์เท่านั้น
``` bash
$ 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
```bash
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
```
## report-templates/docx
ใช้ library [docx-templates ](https://www.npmjs.com/package/docx-templates ) รับข้อมูลใน json กับ MS Word (template) เพิ่อสร้างเอกสาร MS Word สำหรับการใช้งาน PDF จะใช้ LibreOffice ในการแปลงจาก docx เป็น pdf เพื่อจะได้ตัดคำภาษาไทยได้
## report-templates/xlsx
comming soon