Kristian
Kristian

Reputation: 83

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. 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")

Upvotes: 2

Views: 7005

Answers (1)

Tanaike
Tanaike

Reputation: 201378

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

Upvotes: 1

Related Questions