【问题标题】:How to convert all djvu files to pdf如何将所有 djvu 文件转换为 pdf
【发布时间】:2018-08-29 00:01:38
【问题描述】:

这是答案。只需使用来自 DJView 库的 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);
});

【问题讨论】:

    标签: node.js pdf converter djvu


    【解决方案1】:

    进口

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

    一次优化最大转换的队列 let 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)
                            }
                        })
                    }
                });
            });
        });
    
    }
    

    从启动目录初始化

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

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多