建立一个PDF转docx的在线服务

首先,构建一个typeScript的express应用:

package.json

{
  "name": "pdf2docx",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon ./src/index.ts",
    "build": "tsc --project ./",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "codetyphon",
  "license": "ISC",
  "dependencies": {
    "@types/multer": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "multer": "^1.4.2",
    "winax": "^1.20.0"
  },
  "devDependencies": {
    "@types/cors": "^2.8.7",
    "@types/express": "^4.17.8",
    "@types/node": "^14.11.2",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "tslint": "^6.1.3",
    "typescript": "^4.0.3"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "rootDir": "./src",
        "outDir": "./build",
        "esModuleInterop": true,
        "strict": true
    }
}

srcindex.ts

var express = require('express')
import { Request, Response, NextFunction } from 'express'
import cors from 'cors'

var app = express()
app.use(cors())
var port = 9090

app.get('/', async function (req: Request, res: Response) {
    res.send('hello')
})

app.listen(port, '0.0.0.0', () => console.log(`app listening on port ${port}!`))

此时可以启动了:

yarn start

接下来,安装 multer(一个上传的中间件),以便可以上传PDF文件。

yarn add multer

然后,安装 winax,以便操作 activex 。

yarn add winax

现在,package.json如下:

{
  "name": "pdf2docx",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon ./src/index.ts",
    "build": "tsc --project ./",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "codetyphon",
  "license": "ISC",
  "dependencies": {
    "@types/multer": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "multer": "^1.4.2",
    "winax": "^1.20.0"
  },
  "devDependencies": {
    "@types/cors": "^2.8.7",
    "@types/express": "^4.17.8",
    "@types/node": "^14.11.2",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "tslint": "^6.1.3",
    "typescript": "^4.0.3"
  }
}

srcindex.ts 引入multer、winax

import multer from 'multer'
var winax = require('winax');

创建Word.Application实例

var word = new winax.Object('Word.Application');

Word.Application的具体用法可以参考 https://docs.microsoft.com/en...

配置 multer:

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, `${__dirname}/../upload`)
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + '.pdf')
    }
})

const upload = multer({
    storage: storage,
    fileFilter: (req:Request, file, cb) => {
        if (file.mimetype == "application/pdf") {
            cb(null, true);
        } else {
            cb(null, false);
            return cb(new Error('Only .pdf format allowed!'));
        }
    }
})

设置路由:

app.post('/pdf2doc', upload.single('file'), async (req: Request, res: Response) => {
    try {
        const { originalname, mimetype, path } = req.file
        if (mimetype == 'application/pdf') {
            //do
            console.log(path)
            const doc = word.documents.open(path)
            const name = originalname.replace('.pdf', '.docx')
            doc.SaveAs2(`${__dirname}/../download/${name}`, 16)
            doc.close()
            res.json(`http://localhost:9090/download/${name}`);
        } else {
            res.json({
                err: true,
                msg: 'file type is not pdf'
            });
        }
    } catch (err) {
        console.log(err)
        res.sendStatus(400);
    }
})

中间件设置的是file,上传时,文件的字段同样也要时 file

upload.single('file')

设置下载目录

app.use('/download', express.static('download'))

最后,用POST MAN进行测试,一切正常。

完整的 srcindex.ts:

var express = require('express')
import { Request, Response, NextFunction } from 'express'
import cors from 'cors'
import multer from 'multer'
var winax = require('winax');
const word = new winax.Object('Word.Application');
//https://docs.microsoft.com/en-us/javascript/api/word/word.application

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, `${__dirname}/../upload`)
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + '.pdf')
    }
})

const upload = multer({
    storage: storage,
    fileFilter: (req:Request, file, cb) => {
        if (file.mimetype == "application/pdf") {
            cb(null, true);
        } else {
            cb(null, false);
            return cb(new Error('Only .pdf format allowed!'));
        }
    }
})

var app = express()
app.use(cors())
app.use('/download', express.static('download'))
var port = 9090

app.get('/', async function (req: Request, res: Response) {
    res.send('hello')
})

app.post('/pdf2doc', upload.single('file'), async (req: Request, res: Response) => {
    try {
        const { originalname, mimetype, path } = req.file
        if (mimetype == 'application/pdf') {
            //do
            console.log(path)
            const doc = word.documents.open(path)
            const name = originalname.replace('.pdf', '.docx')
            doc.SaveAs2(`${__dirname}/../download/${name}`, 16)
            doc.close()
            res.json(`http://localhost:9090/download/${name}`);
        } else {
            res.json({
                err: true,
                msg: 'file type is not pdf'
            });
        }
    } catch (err) {
        console.log(err)
        res.sendStatus(400);
    }
})

app.listen(port, '0.0.0.0', () => console.log(`app listening on port ${port}!`))

最后,全部代码位于:https://github.com/codetyphon...

你可能感兴趣的:(expresspdfdocx)