关于express:建立一个PDF转docx的在线服务

46次阅读

共计 4196 个字符,预计需要花费 11 分钟才能阅读完成。

首先,构建一个 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…

正文完
 0