简体   繁体   English

如何使用jszip压缩pdf或ppt文件?

[英]How to zip a pdf or ppt file using jszip?

I want zip a pdf or some other format like excel, ppt etc using jszip. 我想使用jszip将pdf或其他格式(例如excel,ppt等)压缩。

How to zip this format in jszip? 如何在jszip中压缩此格式?

My code is as below 我的代码如下

<BODY>
  <a id ='file' href="data/some.pdf" type="application/pdf; length=432628">Some.pdf</a>
  <input id="button" type="button" onclick="create_zip()" value="Create Zip"></input>
</BODY>
<SCRIPT>
        function create_zip() {
        var data = document.getElementById('file');
            var zip = new JSZip();
            zip.add(data, {base64: true});
            content = zip.generate();
            location.href = "data:application/zip;base64," + content;
        }
</SCRIPT>

What I want is that I will get the path of the file from the a element in the html. 我想要的是,我将从html中的a元素获取文件的路径。 I want to zip the pdf using jszip 我想使用jszip压缩pdf

jsfiddle jsfiddle

JSZipUtils may be useful for you. JSZipUtils可能对您有用。 This is the example for the documentation: 这是文档的示例:

JSZipUtils.getBinaryContent("path/to/picture.png", function (err, data) {
    if(err) {
        throw err; // or handle the error
    }
    var zip = new JSZip();
    zip.file("picture.png", data, {binary:true});
}

Which can be easily adapted to your case: 可以轻松适应您的情况:

function create_zip() {
    var url = document.getElementById('file').href;
    JSZipUtils.getBinaryContent(url, function (err, data) {
        if(err) {
            throw err; // or handle the error
        }
        var zip = new JSZip();
        zip.file("Some.pdf", data, {binary:true});
        content = zip.generate();
     }
}

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

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