更新 port 為 3005,更新 README

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 15:19:56 +08:00
parent 55cb49a8bc
commit a318c18214
2 changed files with 30 additions and 20 deletions

View File

@@ -1,3 +1,5 @@
# sudo docker run -d -p 3005:3005 --name rental-contract-pdf rental-contract-pdf
FROM node:20-bookworm-slim
RUN apt-get update \
@@ -20,11 +22,11 @@ RUN if [ -d fonts ] && ls fonts/*.{ttf,otf,ttc} 2>/dev/null | grep -q .; then \
fi
ENV NODE_ENV=production
ENV PORT=3000
ENV PORT=3005
ENV TEMPLATE_DIR=/app/templates
ENV TEMP_DIR=/tmp/rental-contracts
ENV SOFFICE_BIN=soffice
EXPOSE 3000
EXPOSE 3005
CMD ["npm", "start"]

View File

@@ -1,6 +1,6 @@
# 租屋契約 PDF 產生器
這是一個 Node.js 網頁工具,用指定的 Word `.doc` 範本產生租屋契約 PDF。網頁會帶入
這是一個 Node.js 網頁工具,用指定的 Word `.docx` 範本產生租屋契約 PDF。網頁會帶入
- `{{每月租金}}`:預設 `8000`
- `{{繳款日期}}`:預設 `18`
@@ -8,11 +8,11 @@
產生流程:
1. 從 `templates/` 複製指定的 `.doc` 範本到暫存資料夾。
2. 替換暫存 `.doc` 裡的三個佔位符。
3. 呼叫 LibreOffice headless 將暫存 `.doc` 轉成 PDF。
1. 從 `templates/` 複製指定的 `.docx` 範本到暫存資料夾。
2. 替換暫存 `.docx` 裡的三個佔位符。
3. 呼叫 LibreOffice headless 將暫存 `.docx` 轉成 PDF。
4. 將 PDF 回傳給網頁下載;手機瀏覽器支援時可直接分享。
5. 刪除暫存 `.doc` 與轉檔過程產生的檔案。
5. 刪除暫存 `.docx` 與轉檔過程產生的檔案。
## 專案結構
@@ -20,7 +20,8 @@
.
├─ public/ # 前端網頁
├─ src/contractService.js # 複製範本、替換文字、轉 PDF
├─ templates/ # 放 Word .doc 範本
├─ templates/ # 放 Word .docx 範本
├─ fonts/ # 自訂字體(隨 Docker image 一起打包)
├─ server.js # Express API
├─ Dockerfile
└─ README.MD
@@ -36,14 +37,21 @@
{{保證金}}
```
目前程式針對舊版 `.doc` 以固定長度替換,填入的值不能比佔位符字數更長。金額與日期這類短數字可以正常使用
支援 `.docx` 格式。佔位符若被 Word 拆成多個 run程式會自動合併處理
## 自訂字體
把 `.ttf`、`.otf`、`.ttc` 字體檔放到 `fonts/` 資料夾:
- **本機執行**:轉換時自動載入至 LibreOffice profile。
- **Docker**build image 時安裝至系統字體LibreOffice 直接讀取。
## 本機執行
本機需要先安裝:
- Node.js 20 或更新版本
- LibreOffice且 `soffice` 指令可在命令列執行
- LibreOfficeWindows 預設路徑為 `C:\Program Files\LibreOffice\program\soffice.exe`
安裝套件:
@@ -60,40 +68,40 @@ npm start
開啟:
```text
http://localhost:3000
http://localhost:3005
```
## Docker 執行
建立 image
```powershell
docker build -t rental-contract-pdf .
```bash
sudo docker build -t rental-contract-pdf .
```
啟動 container
```powershell
docker run --rm -p 3000:3000 rental-contract-pdf
```bash
sudo docker run -d -p 3005:3005 --name rental-contract-pdf rental-contract-pdf
```
若要把本機 `templates/` 掛進 container
```powershell
docker run --rm -p 3000:3000 -v "${PWD}\templates:/app/templates" rental-contract-pdf
```bash
sudo docker run -d -p 3005:3005 -v /path/to/templates:/app/templates --name rental-contract-pdf rental-contract-pdf
```
開啟:
```text
http://localhost:3000
http://localhost:3005
```
## 手機下載或分享
手機連到這個網站後,產生 PDF 時會優先使用瀏覽器的檔案分享功能。若瀏覽器或連線環境不支援檔案分享,網頁會保留下載 PDF 按鈕。
注意:多數手機瀏覽器的檔案分享功能需要 HTTPS 或 localhost。若用區網 IP 開啟,例如 `http://192.168.x.x:3000`,可能只能下載,不能直接分享。
注意:多數手機瀏覽器的檔案分享功能需要 HTTPS 或 localhost。若用區網 IP 開啟,例如 `http://192.168.x.x:3005`,可能只能下載,不能直接分享。
## API
@@ -108,7 +116,7 @@ Body
```json
{
"template": "租屋契約-內容_逢甲 A.doc",
"template": "租屋契約-內容_逢甲 A.docx",
"monthlyRent": "8000",
"paymentDay": "18",
"deposit": "16000"