PDF轉Word

👤 veilei 📦 v1.0.0 ⭐ 0.0 ⬇️ 1.3K 下載
📄 辦公效率 免費

📖 技能介紹

PDF to Word (pdf-to-word) Skill

PDF 轉 Word 轉換技能,基於 doc-converter 專案實現。

專案位置

/home/vei/.openclaw/workspace/doc-converter/
├── backend/          # Node.js 後端 (Fastify + Prisma)
├── frontend/         # React 前端 (Vite + TypeScript)
└── docker-compose.yml

核心轉換流程

1. 上傳 PDF 檔案

前端上傳(已實現於 HomePage.tsx):

POST /api/v1/file/upload
Content-Type: multipart/form-data
Body: file (PDF檔案,最大50MB)
Response: { fileId, originalName, format, size, pages }

直接 API 呼叫:

curl -X POST http://localhost:3000/api/v1/file/upload \
  -F "file=@your.pdf"

2. 建立 PDF→Word 轉換任務

POST /api/v1/convert/
Content-Type: application/json
Body: { fileId: "<fileId>", conversionType: "pdf_to_word" }
Response: { taskId, status, conversionType, createdAt, expiresAt }

3. 輪詢任務狀態

GET /api/v1/convert/:taskId
Response: {
  taskId, status: "PENDING" | "PROCESSING" | "COMPLETED" | "FAILED",
  progress: 0-100,
  output: { fileId, originalName, format, size, downloadUrl } | null,
  errorMessage: string | null
}

狀態說明:

  • PENDING - 排隊中
  • PROCESSING - 轉換中(有 progress 進度)
  • COMPLETED - 完成,可下載
  • FAILED - 失敗,檢視 errorMessage

4. 下載轉換結果

GET /api/v1/convert/:taskId/download
Response: 檔案流 (application/vnd.openxmlformats-officedocument.wordprocessingml.document)

5. 轉換歷史

GET /api/v1/convert/history
Response: { total, tasks: [{ taskId, conversionType, status, createdAt, input, output }] }

後端轉換實現

核心檔案:backend/src/jobs/converter.ts

PDF → Word 使用 pdf2docx Python庫:

from pdf2docx import Converter
c = Converter('input.pdf')
c.convert('output.docx')
c.close()

其他轉換型別使用 LibreOffice:

  • Word/Excel/PPT → PDF:soffice --headless --convert-to pdf
  • PDF → 圖片:pdftoppm -jpeg -r 200
  • 圖片 → PDF:convert 或 gs

前端元件

  • HomePage.tsx - 主轉換介面(拖拽上傳、轉換選項、結果展示)
  • HistoryPage.tsx - 轉換歷史
  • convertStore - Zustand 狀態管理

支援的轉換型別

conversionType 說明 引擎
pdf_to_word PDF → Word pdf2docx
word_to_pdf Word → PDF LibreOffice
pdf_to_images PDF → 圖片 pdftoppm
images_to_pdf 圖片 → PDF ImageMagick/gs
word_to_image Word → 圖片 LibreOffice + pdftoppm
excel_to_pdf Excel → PDF LibreOffice
ppt_to_pdf PPT → PDF LibreOffice

快速使用流程

  1. 上傳 PDF:拖拽或點選選擇 PDF 檔案
  2. 選擇轉換格式:自動顯示 PDF→Word 選項

    7w4.net小蔥技能站,你的AI助手技能庫。

  3. 開始轉換:點選轉換按鈕
  4. 等待完成:輪詢任務狀態(每1.5秒)
  5. 下載結果:點選下載按鈕獲取 .docx 檔案

關鍵程式碼參考

  • 轉換路由:backend/src/routes/convert.ts
  • 轉換Worker:backend/src/jobs/converter.ts
  • 前端API:frontend/src/api/client.ts
  • 狀態管理:frontend/src/stores/convertStore.ts

詳細 API 和架構說明見 references/

📁 包含檔案 (4 個)

📄 SKILL.md 3.6 KB
📄 _meta.json 134 B
📄 references/api.md 2.6 KB
📄 references/architecture.md 4.5 KB