如何将所有djvu文件转换为pdf

时间:2018-03-20 08:47:56

标签: node.js pdf converter djvu

这是答案。只需使用DJView lib中的nodejs和ddjvu即可。 有

进口

var Dao = require('./dao')

mydb = new Dao('sample_database');
mydb.append('hello')

转换文件的方法,转换时删除。

const fs = require('fs');
const os = require('os');
const {spawn} = require('child_process');
const path = require('path');
const maxProcess = os.cpus().length - 1;// count of procces - 1 for system needs
let nowPlayed = 0;

一次优化最大转换的队列     让queue = [];

function chpoc(args) {
    console.log(args[1] + " start converting");
    spawn(`ddjvu`, ["-format=pdf", args[0], args[1] + ".pdf"]).on('close', (data) => {
        console.log(args[1] + ".pdf converted");
        fs.unlink(args[0], (err) => {
            if (err) throw err;
            console.log(args[0] + ' successfully deleted!');
            nowPlayed--;
        })
    });
}

填充队列并启动它

function startQueue() {
    if (nowPlayed < maxProcess && queue.length) {
        nowPlayed++;
        queue.pop()();
    }
}

setInterval(startQueue, 500)

显示错误

function workWithFile(filepath) {
    const args = filepath.match(/(.*)\.djvu/)
    if (args && args.length) {
        queue.push(() => {
            chpoc(args);
        });
    }
}

目录三并找到djvus

const eachCallback = function (err) {
    err && console.error(err);
}

init从启动目录

let filePaths = [];

function getFiles(dirPath, callback) {
    fs.readdir(dirPath, function (err, files) {
        if (err) return callback(err);
        files.forEach((fileName) => {
            setTimeout(() => {
                let filePath = path.join(dirPath, fileName);
                if (filePath) {
                    fs.stat(filePath, function (err, stat) {
                        if (err) return eachCallback(err);

                        if (stat.isDirectory()) {
                            getFiles(filePath, callback);
                        } else if (stat.isFile() && /\.djvu$/.test(filePath)) {
                            filePaths.push(filePath);
                            callback(filePath)
                        }
                    })
                }
            });
        });
    });

}

1 个答案:

答案 0 :(得分:0)

进口

const fs = require('fs');
const os = require('os');
const {spawn} = require('child_process');
const path = require('path');
const maxProcess = os.cpus().length - 1;// count of procces - 1 for system needs
let nowPlayed = 0;

转换文件的方法,转换时删除。

function chpoc(args) {
    console.log(args[1] + " start converting");
    spawn(`ddjvu`, ["-format=pdf", args[0], args[1] + ".pdf"]).on('close', (data) => {
        console.log(args[1] + ".pdf converted");
        fs.unlink(args[0], (err) => {
            if (err) throw err;
            console.log(args[0] + ' successfully deleted!');
            nowPlayed--;
        })
    });
}

一次优化最大转换的队列让queue = [];

function startQueue() {
    if (nowPlayed < maxProcess && queue.length) {
        nowPlayed++;
        queue.pop()();
    }
}

setInterval(startQueue, 500)

填充队列并启动它

function workWithFile(filepath) {
    const args = filepath.match(/(.*)\.djvu/)
    if (args && args.length) {
        queue.push(() => {
            chpoc(args);
        });
    }
}

显示错误

const eachCallback = function (err) {
    err && console.error(err);
}

目录三并找到djvus

let filePaths = [];

function getFiles(dirPath, callback) {
    fs.readdir(dirPath, function (err, files) {
        if (err) return callback(err);
        files.forEach((fileName) => {
            setTimeout(() => {
                let filePath = path.join(dirPath, fileName);
                if (filePath) {
                    fs.stat(filePath, function (err, stat) {
                        if (err) return eachCallback(err);

                        if (stat.isDirectory()) {
                            getFiles(filePath, callback);
                        } else if (stat.isFile() && /\.djvu$/.test(filePath)) {
                            filePaths.push(filePath);
                            callback(filePath)
                        }
                    })
                }
            });
        });
    });

}

init从启动目录

getFiles(__dirname, function (file) {
    workWithFile(file);
});