简体   繁体   English

在浏览器中用 blob 下载 PDF

[英]Download PDF in browser with blob

I can not quite wrap my head around on how to download a PDF from a google spreadsheet PDF Export Weblink.我不太清楚如何从谷歌电子表格 PDF 导出 Web 链接下载 PDF。 I generated a testing spreadsheet for this case.我为这个案例生成了一个测试电子表格。

I understand that I need to implement encodeURIComponent and/or "Buffer.from" to the blob but however I do it, it only downloads a broken PDF for me.我知道我需要对 blob 实施 encodeURIComponent 和/或“Buffer.from”,但无论我怎么做,它只会为我下载损坏的 PDF。

This is what I currently have in its rawest form.这是我目前最原始的形式。 Thank you for your support!感谢您的支持!

Node JS:节点JS:

const fetch = require('node-fetch');

var url = "https://docs.google.com/spreadsheets/d/1fLjKASR_g5wsvOjjJi6RclqMVd2o_1On-OfimXtId4E/export?exportFormat=pdf&format=pdf&size=A4&fzr=true&gid=477517973&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&portrait=true&fitw=true&fith=true&top_margin=0.20&bottom_margin=0.20&left_margin=0.20&right_margin=0.20";

let blob = await fetch(url).then(r => r.blob());

// then send blob variable to javascript 

Javascript: Javascript:

function downloadURI(name) {
        var uri = 'data:application/pdf;base64,' + blob; 
        var link = document.createElement('a');
        link.download = name;
        link.href = uri;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        delete link;
      }
      downloadURI("test"+".pdf")

I thought that from var uri = 'data:application/pdf;base64,' + blob;我认为来自var uri = 'data:application/pdf;base64,' + blob; in your script, in this case, it is required to convert the downloaded data as the base64. Although I'm not sure about the relationship between the scripts between Node JS: and Javascript: , in your situation, how about the following modification?在你的脚本中,在这种情况下,需要将下载的数据转换为 base64。虽然我不确定Node JS:Javascript:之间的脚本之间的关系,但在你的情况下,如何进行以下修改?

From:从:

let blob = await fetch(url).then(r => r.blob());

To:到:

let buf = await fetch(url).then((r) => r.arrayBuffer());
const data = Buffer.from(buf).toString("base64");

By this, you can use data as follows.这样,您可以按如下方式使用data

var uri = 'data:application/pdf;base64,' + data;

Note:笔记:

  • As the additional information, for example, if you want to download your Spreadsheet as a PDF file using only Javascript, you can also use the following script.作为附加信息,例如,如果您希望仅使用 Javascript 将电子表格下载为 PDF 文件,您也可以使用以下脚本。 But, in this case, the Spreadsheet is required to be publicly shared.但是,在这种情况下,电子表格需要公开共享。 Please be careful about this.请注意这一点。

     async function downloadURI(name) { var url = "https://docs.google.com/spreadsheets/d/1fLjKASR_g5wsvOjjJi6RclqMVd2o_1On-OfimXtId4E/export?exportFormat=pdf&format=pdf&size=A4&fzr=true&gid=477517973&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&portrait=true&fitw=true&fith=true&top_margin=0.20&bottom_margin=0.20&left_margin=0.20&right_margin=0.20"; let blob = await fetch(url).then((r) => r.blob()); var f = new FileReader(); f.readAsDataURL(blob); f.onload = d => { var uri = d.target.result; var link = document.createElement('a'); link.download = name; link.href = uri; document.body.appendChild(link); link.click(); document.body.removeChild(link); delete link; } } downloadURI("test"+".pdf")

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

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