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

3

我想使用jszip压缩pdf或其他格式,比如excel、ppt等。

如何在jszip中进行这些格式的压缩?

我的代码如下:

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

我想要做的是从html中的a元素获取文件路径。 我想使用jszip压缩PDF。 jsfiddle
1个回答

2
JSZipUtils 可能对您有用。这是文档的示例:
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});
}

这可以很容易地适用于您的情况:
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();
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接