安装环境shell脚本(centos7)

#!/bin/bash
#suppor china
yum install wqy-microhei-fonts wqy-zenhei-fonts

#office to pdf
yum install unoconv -y

#pdf to slide image
yum install ImageMagick -y

使用node构造核心类officeCovert.js

const unoconv = require('awesome-unoconv');
const fs = require('fs');
const child_process = require("child_process");

class OfficeCovert{
    //file to pdf 
    file2pdf(sourceFile, generateFile){
        return unoconv.convert(sourceFile, generateFile);
    }

    //file to image list
    file2images(file, path, prefix = '', suffix ='', ext = 'png'){
        return new Promise((resolve, reject)=> {
            if(fs.existsSync(file)){
                this.rm(path);
                this.mkdir(path);
                child_process.exec(`convert ${file} ${path}/${prefix}%d${suffix}.${ext} `, (...args) =>{
                    if(args && args[0] === null){
                        resolve(path);
                    }else{
                        reject(...args);
                    }
                }, err => {
                    reject(err);
                });
            }else{
                reject('file not found');
            }
        });
    }

    //delete file or path
    rm(path){
        let files = [];
        if(fs.existsSync(path)){
            if(fs.statSync(path).isDirectory()){
                files = fs.readdirSync(path);
                files.forEach((file, index)=> {
                    let curPath = path + '/' + file;
                    if(fs.statSync(curPath).isDirectory()){
                        this.rm(curPath);
                    }else{
                        fs.unlinkSync(curPath);
                    }
                });
            }else{
                fs.unlinkSync(path);
            }
        }
    }
    //create path
    mkdir(path){
        if(!fs.existsSync(path)){
            fs.mkdir(path,{ recursive: true }, res=>  {
                if(res){
                    console.warn(res);
                }
            });    
        }
    }
}

module.exports = OfficeCovert;

入口文件index.js

const {resolve} = require('path');

const OfficeCovert = require('./officeCovert');
//定义需要转换的文件
const pptFile = resolve('./office/test2.pptx');
//生成pdf名称
const pdfFile = resolve('./output/test2.ppf');
//设置图片目录
const imgFile = resolve('./output/test2/tst');
const oc = new OfficeCovert();

//geneate pdf
oc.file2pdf(pptFile, pdfFile).then(gFile =>{
    //generate image
    oc.file2images(gFile, imgFile, 'test11', 'mytest', 'png').then(path=> {
        console.log('generate image success !!!!', path);
        //clean file 
        oc.rm(gFile);
    }).catch((...args) => {
        console.log(args);
    });
});

标签: none

添加新评论