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



这是答案。只需使用 DJView lib 中的 nodejs 和 ddjvu。那里

进口

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

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

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

从开始目录初始化

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

导入

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

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

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

从开始目录初始化

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

最新更新