zoukankan      html  css  js  c++  java
  • 使用node将word转为pdf

    刚开始拿到这个需求是很烧脑的,因为我们要实现的是文件的预览,当碰到文件时word的时候,就很麻烦了,找了好多案例也没着急合适的,后面再一个不经意间,看到了libreoffice,它是再linux系统下使用的,确定我们的系统安装了libreoffice,并且做好了配置之后,我们就可以用node实现word转pdf了。

    在做node转pdf时,我们需要调用系统的环境,所以需要引入:

    const util = require('util');  //用来提供常用函数的集合
    var exec = util.promisify(require('child_process').exec);  // util.promisify把原来的异步回调方法改成返回 Promise 实例的方法,  child_process  模块提供了衍生子进程的能力 创建子shell,可以直接执行shell管道命令,有回调

    然后通过调用系统环境,来实现word转pdf

    async function wordToPdf(wordPath,pdfPath) {
        const {stdout,stderr} =await exec('libreoffice6.4 --headless --convert-to pdf --outdir '+pdfPath+' '+wordPath);
            console.log('stdout:', stdout);
            console.log('stderr:', stderr);
    }

    通过上面的方法就可以实现word转pdf。



     
  • 相关阅读:
    httpClient-3.1学习笔记
    HTTP Header 详解
    Java:对象的强、软、弱和虚引用
    Spring @ResponseBody 返回乱码 的优雅解决办法
    Spring MVC 返回类型为字符串时, 返回中文变成"?"处理
    GroupVarint
    Format
    DynamicConverter
    Thread pools & Executors
    Futures
  • 原文地址:https://www.cnblogs.com/lxz123/p/13639144.html
Copyright © 2011-2022 走看看