node html转图片、node读取docx文件、node将docx转为html、node将docx转为PDF文件、node将PDF转为jpg

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)
    })
})

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值