初始化專案:租屋契約 PDF 產生器

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 12:04:54 +08:00
commit e1fcf3eb77
13 changed files with 1768 additions and 0 deletions

118
README.MD Normal file
View File

@@ -0,0 +1,118 @@
# 租屋契約 PDF 產生器
這是一個 Node.js 網頁工具,用指定的 Word `.doc` 範本產生租屋契約 PDF。網頁會帶入
- `{{每月租金}}`:預設 `8000`
- `{{繳款日期}}`:預設 `18`
- `{{保證金}}`:預設 `16000`
產生流程:
1. 從 `templates/` 複製指定的 `.doc` 範本到暫存資料夾。
2. 替換暫存 `.doc` 裡的三個佔位符。
3. 呼叫 LibreOffice headless 將暫存 `.doc` 轉成 PDF。
4. 將 PDF 回傳給網頁下載;手機瀏覽器支援時可直接分享。
5. 刪除暫存 `.doc` 與轉檔過程產生的檔案。
## 專案結構
```text
.
├─ public/ # 前端網頁
├─ src/contractService.js # 複製範本、替換文字、轉 PDF
├─ templates/ # 放 Word .doc 範本
├─ server.js # Express API
├─ Dockerfile
└─ README.MD
```
## 範本準備
把 Word 範本放到 `templates/` 資料夾,範本內請保留以下文字:
```text
{{每月租金}}
{{繳款日期}}
{{保證金}}
```
目前程式針對舊版 `.doc` 以固定長度替換,填入的值不能比佔位符字數更長。金額與日期這類短數字可以正常使用。
## 本機執行
本機需要先安裝:
- Node.js 20 或更新版本
- LibreOffice且 `soffice` 指令可在命令列執行
安裝套件:
```powershell
npm install
```
啟動:
```powershell
npm start
```
開啟:
```text
http://localhost:3000
```
## Docker 執行
建立 image
```powershell
docker build -t rental-contract-pdf .
```
啟動 container
```powershell
docker run --rm -p 3000:3000 rental-contract-pdf
```
若要把本機 `templates/` 掛進 container
```powershell
docker run --rm -p 3000:3000 -v "${PWD}\templates:/app/templates" rental-contract-pdf
```
開啟:
```text
http://localhost:3000
```
## 手機下載或分享
手機連到這個網站後,產生 PDF 時會優先使用瀏覽器的檔案分享功能。若瀏覽器或連線環境不支援檔案分享,網頁會保留下載 PDF 按鈕。
注意:多數手機瀏覽器的檔案分享功能需要 HTTPS 或 localhost。若用區網 IP 開啟,例如 `http://192.168.x.x:3000`,可能只能下載,不能直接分享。
## API
產生 PDF
```http
POST /api/contracts/pdf
Content-Type: application/json
```
Body
```json
{
"template": "租屋契約-內容_逢甲 A.doc",
"monthlyRent": "8000",
"paymentDay": "18",
"deposit": "16000"
}
```
成功時會回傳 `application/pdf`。