'Download PDF file form embed tag using Puppeteer

I am trying to download a pdf from a Website.

The website is made with the framework ZK, and it reveals a dynamic URL to the PDF for a window of time when an id number type in a input bar. This step is easy enough and I a able to get the PDF URL which opens up in the browser on a embedded tag.

However, it has been impossible for me to find a way to download the file to my computer. For days, I have tried and read everything from this, to this, to this.

The closes thing I have been able to get with this code:

let [ iframe ] = await page.$x('//iframe');
let pdf_url = await page.evaluate( iframe => iframe.src, iframe)

let res = await page.evaluate( async url => 
                await fetch(url, {
                        method: 'GET',
                        credentials: 'same-origin', // usefull when we are logged into a website and want to send cookies
                        responseType: 'arraybuffer', // get response as an ArrayBuffer
                }).then(response => response.text()), 
                pdf_url 
        )
console.log('res:', res);
//const response = await page.goto(pdf);
fs.writeFileSync('somepdf.pdf', res);

This results in a blank PDF file which is of 92K in size.

While the file I am trying to get is of 52K. I suspect the back-end might be sending me 'dummy' pdf file because my headers on the fetch request might not be correct.

What else can I try?

Here is the link to the PDF page.

You can use the random ID number I found: '1705120630'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source