Download PDF from Firebase Functions and puppeteer

Viewed 73
exports.getPDF = functions.https.onCall(async (data, context)=> {
  var browser = await puppeteer.launch({ args: [
    '--no-sandbox',
    '--disable-setuid-sandbox'
  ]})

  const page = await browser.newPage()
  await page.goto(`somelink`)
  const buffer = await page.pdf({
    printBackground: true
  })
  return buffer
})

I have a code like above which takes a page and makes it into a pdf.

However, when I call the function from the client (Chrome) and download it as a pdf file, the pdf does not open.

let pdfFunc = functions.httpsCallable('getPdf')

const res = await pdfFunc()

const blob = new Blob([JSON.stringify(res.data)], {type: 'application/pdf'})
const encodedUri = window.URL.createObjectURL(blob)
const link = document.createElement("a")
link.setAttribute("href", encodedUri)
link.setAttribute("download", 'test')

The file downloads as pdf but does not read properly. What am I doing wrong?

0 Answers
Related