简体   繁体   English

有没有办法使用 pdf-lib 将 pdf 添加到 jszip zip?

[英]Is there a way to add pdfs to a jszip zip, using pdf-lib?

Good morning.早上好。 As the title says i'm trying to add pdfs files to a zip.正如标题所说,我正在尝试将 pdfs 文件添加到 zip。 I'm using pdf-lib and jszip.我正在使用 pdf-lib 和 jszip。

here's my switch case code where i'm implementing the logic:这是我正在实现逻辑的开关案例代码:

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

when i download the zip file, it contains not formatted files for being opened with adobe reader, but i've to specify with which program open them.当我下载 zip 文件时,它包含未格式化的文件,可使用 adobe reader 打开,但我必须指定使用哪个程序打开它们。

Anyone can point me the way?谁能给我指路?

Thanks in advance提前致谢

i've resolved the issue by adding the file extension when adding the files.我已经通过在添加文件时添加文件扩展名解决了这个问题。

So the previous line of code:所以上一行代码:

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

Became:成为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM