【问题标题】:How to zip a pdf or ppt file using jszip?如何使用 jszip 压缩 pdf 或 ppt 文件?
【发布时间】:2014-02-28 09:16:54
【问题描述】:

我想要使用 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

【问题讨论】:

    标签: javascript zip jszip


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2014-03-23
      • 2017-03-15
      • 2020-08-21
      • 2021-11-08
      相关资源
      最近更新 更多