使用 blob 在浏览器中下载 PDF

发布于 2025-01-13 15:27:37 字数 1183 浏览 6 评论 0原文

我无法完全理解如何从谷歌电子表格 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2025-01-20 15:27:37

我认为从脚本中的 var uri = 'data:application/pdf;base64,' + blob; 来看,在这种情况下,需要将下载的数据转换为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");

通过此,您可以按如下方式使用data

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

注意:

  • 作为附加信息,例如,如果您想仅使用 Javascript 将电子表格下载为 PDF 文件,您还可以使用以下脚本。但是,在这种情况下,电子表格需要公开共享。请注意这一点。

     异步函数 downloadURI(name) {
        变量网址 = “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&页码=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";
    
        让 blob =等待 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');
          链接.下载=名称;
          链接.href = uri;
          document.body.appendChild(链接);
          链接.click();
          document.body.removeChild(链接);
          删除链接;
        }
      }
      downloadURI("测试"+".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 between Node JS: and Javascript:, in your situation, how about the following modification?

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.

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. 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")
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文