大 Html 文件转 PDF

问题描述

我想将大型 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

解决方法

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

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

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

http://pdfmake.org/playground.html

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

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