【问题标题】:Is there a way to add pdfs to a jszip zip, using pdf-lib?有没有办法使用 pdf-lib 将 pdf 添加到 jszip zip?
【发布时间】:2022-01-07 09:04:21
【问题描述】:

早上好。正如标题所说,我正在尝试将 pdfs 文件添加到 zip 中。我正在使用 pdf-lib 和 jszip。

这是我实现逻辑的开关案例代码:

case 'singolo':
        

        const zip = new JSZip();

        for(let i = 0; i < pages.length; i++){
            const pdf = await PDFDocument.create();
            const [copiedPages] = await pdf.copyPages(pdfFile, [i])
            pdf.addPage(copiedPages);
            const singlePdf = await pdf.save(); //format uint8array

            console.log('singlePdf', singlePdf)

            zip.file(`pagina ${i + 1}`, singlePdf)

        }

        zip.generateAsync({type: "blob"}) //ritorna una promise quindi posso usare il then
            .then(function(content) {
                saveAs(content, fileName ? fileName : `singoloPdf`); //metodo di fileSaver per poter scaricare lo zip
        });
        break;
    }

当我下载 zip 文件时,它包含未格式化的文件,无法使用 adobe reader 打开,但我必须指定使用哪个程序打开它们。

谁能给我指路?

提前致谢

【问题讨论】:

    标签: javascript reactjs zip jszip


    【解决方案1】:

    我已经通过在添加文件时添加文件扩展名解决了这个问题。

    所以上一行代码:

    zip.file(`pagina ${i + 1}`, singlePdf)
    

    成为:

    zip.file(`pagina ${i + 1}.pdf`, singlePdf)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多