【问题标题】:Large Html file to PDF将大型 Html 文件转换为 PDF
【发布时间】:2020-12-30 10:12:11
【问题描述】:

我想将一个大的 html 文件(21 Mb,不包括照片)转换为 .pdf,但我无法做到

Javascript 或 python 甚至 CLI 中的解决方案都可以工作

我尝试了两种方法:

  1. 使用 html 节点

    function writeToPdf(htmlpath, pdfpath) {
             const writeStream = fs.createWriteStream(pdfpath);
             const options = { format: "Letter" };
             const html = fs.readFileSync(htmlpath, "utf8");
             return new Promise(function (resolve, reject) {
                    pdf.create(html, options).toStream(function (err, stream) {
                    stream.pipe(writeStream);
             });
             writeStream.on("error", function (err) {
              console.log(err);
              reject(err);
             });
             resolve({});
           });
      }
    
    let htmlpath = 'input.html';
    let outpath = 'output.pdf';
    writeToPdf(htmlpath, outpath)
    

这适用于小 html 文件,但会卡住,或者我不知道大 html 文件处理了多少文件

  1. 我在 windows 上使用 wkhtmltopdf,但在 50% 的时候卡住了几次

任何帮助对我来说都会很棒,包括 JavaScript 或 Python

【问题讨论】:

  • pandoc 呢?
  • 是 html 到 pdf 或流,因为输入很大
  • 我无法猜测你的情况,但对我来说,通过 LaTeX 在 .md 到 .pdf 导出中工作,在包含大量图像和表格的两列的 389 页密集文本的文档中,所以...

标签: python node.js wkhtmltopdf html-to-pdf node-pdfkit


【解决方案1】:

使用 pdfmake npm 库可以很容易地从所有来源中创建 pdf。 它具有使用 javascript 的客户端和服务器端实现。

https://pdfmake.github.io/docs/0.1/

使用 Playground 在线创建输入模板。

http://pdfmake.org/playground.html

https://www.npmjs.com/package/pdfmake

还可以在线获得大量材料。 如果您需要更多帮助,请发表评论。

【讨论】:

猜你喜欢
  • 2011-02-27
  • 1970-01-01
  • 2018-06-03
  • 2013-07-10
  • 2012-02-11
  • 1970-01-01
  • 2010-10-12
相关资源
最近更新 更多