是否有用于将PDF转换为EPUB格式的npm软件包?

时间:2019-11-07 10:20:02

标签: node.js

我正在尝试将PDF文件转换为EPUB格式。因此,我使用了第三方API(ZAMZAR),发现生成的EPUB文件没有PDF那样的准确数据。

然后我尝试使用节点npm软件包(epub-gen,pdf2html)来执行此操作。但是没有找到预期的结果。

下面是我尝试过的代码。

const parseEpub = require('epub-modify').parseEpub
const path = require('path')
const Epub = require("epub-gen");
const pdf2html = require('pdf2html')
const dir = path.join(__dirname, '../../epubFiles/5.epub')

const convert2Html = (req, res) => {

    pdf2html.html(req.file.path, (err, html) => {
        if (err) {
            console.error('Conversion error: ' + err)
        } else {
            console.log(html)
            createEPUB(html)
        }
    })
}

const createEPUB = (req, res, html) => {
    let html_data = `

<title></title>
</head>
<body><div class="page"><p/>
</div>
</body></html>

    `
    const option = {
        title: "Alice's Adventures in Wonderland", // *Required, title of the book.
        author: "Lewis Carroll", // *Required, name of the author.
        publisher: "Macmillan & Co.", // optional,
        css: `table, th, td {
            border: 1px solid black;
          }`,
        content: [
            {
                title: "Alice's Adventures in Wonderland", // *Required, title of the book.
                author: "Lewis Carroll",
                data: html_data
            }
        ]
    };

    new Epub(option, dir).promise.then(
        (data) => console.log("Ebook Generated Successfully!",data),
    err => console.error("Failed to generate Ebook because of ", err)
    );
}

module.exports = {
    convert2Html: convert2Html
}

0 个答案:

没有答案