docx与doc文档格式是一样的,以下代码都适用
1.node实现网页转为图片:
安装依赖:
npm install phantom
代码块:
var phantom = require('phantom');
phantom.create().then(function(ph) {
ph.createPage().then(function(page) {
page.open("https://www.baidu.com").then(function(status) {
page.property('viewportSize', { width: 1920, height: 1080 });
page.render('./baidu.jpg').then(function() {
ph.exit();
});
});
});
});
运行之后会把open当中输入的网址转为jpg图片,保存在当前js文件的同级目录下
2.node实现读取docx文件(会去掉所有的换行)
var mammoth = require("mammoth");
mammoth.extractRawText({path: "./test.docx"})
.then(function(result){
var text = result.value; // The raw text
console.log(text);
}).done();
3.将docx文件转为html代码,path为docx文件的路径
var mammoth = require("mammoth");
mammoth.convertToHtml({path: "uploads/test.docx"})
.then(function(result){
var html = result.value; // The generated HTML
var messages = result.messages; // Any messages, such as warnings during conversion
console.log(html);
})
.done();
4.node将docx转为PDF文件:
var docxConverter = require('docx-pdf');
docxConverter('./test.docx','./ts.pdf',function(err,result){
if(err){
console.log(err);
}
console.log('result'+result);
});
5.node将PDF转为jpg
const path = require('path');
const pdf = require('pdf-poppler');
const fs = require('fs');
let dir="./"
let file = './1.pdf'
function convert(file){
let opts = {
format: 'jpeg',
out_dir: path.dirname(file),
out_prefix: path.basename(file, path.extname(file)),
page: null
}
pdf.convert(file, opts)
.then(res => {
console.log('Successfully converted: '+res);
})
.catch(error => {
console.error(error);
})
}
fs.readdir(dir, function (err, files) {
if (err) {
return console.log('目录不存在')
}
console.log(files)
files.forEach(function (file) {
let newFile = dir + "/"+file
console.log("new File: "+newFile)
convert(newFile)
})
})