使用 blob 在浏览器中下载 PDF
我无法完全理解如何从谷歌电子表格 PDF 导出 Web 链接下载 PDF。我为此案例生成了一个测试电子表格。
我知道我需要对 blob 实施encodeURIComponent 和/或“Buffer.from”,但无论我这样做,它都只会为我下载一个损坏的 PDF。
这就是我目前拥有的最原始的形式。感谢您的支持!
Node 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:
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 can not quite wrap my head around on how to download a PDF from a google spreadsheet PDF Export Weblink. 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.
This is what I currently have in its rawest form. Thank you for your support!
Node 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:
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为从脚本中的
var uri = 'data:application/pdf;base64,' + blob;
来看,在这种情况下,需要将下载的数据转换为base64。虽然我不确定Node JS:
和Javascript:
之间的脚本之间的关系,但根据您的情况,以下修改如何?From:
To:
通过此,您可以按如下方式使用
data
。注意:
作为附加信息,例如,如果您想仅使用 Javascript 将电子表格下载为 PDF 文件,您还可以使用以下脚本。但是,在这种情况下,电子表格需要公开共享。请注意这一点。
I thought that from
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 betweenNode JS:
andJavascript:
, in your situation, how about the following modification?From:
To:
By this, you can use
data
as follows.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. But, in this case, the Spreadsheet is required to be publicly shared. Please be careful about this.