dropWizard
dropWizard

Reputation: 3538

simulating downloading a PDF from a web page

Theres a website that lets me download the contents of the page into a PDF.

When I do that, I'm watching the network request and see the network activity thats associated with downloading the PDF

fetch("https://www.website.com/cap/people/profileExportPdf/pdf_name.pdf", {
  "headers": {
    "accept": "*/*",
    "accept-language": "en-US,en;q=0.9",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "vieweeids": "702604177,4R09,CAP",
    "x-li-page-instance": "urn:li:page:cap-fe-desktop-profile-view;S0qlT2ijT/2aPYUbf3QrDQ==",
    "x-requested-with": "XMLHttpRequest"
  },
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": null,
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
});

However, when I paste the copy of a fetch into the devTools, the PDF isn't downloaded. The Fetch request is fulfilled and doesn't throw any errors.

I don't know anything about what goes into downloading files from websites.

From my understanding, this is hitting an API and my thought would be a response to the browser that triggers a file downloand. This doesn't seems to be happening, which leads me to think that maybe I'm hitting the wrong API? What would should I be looking for in the network request to mirror

If anyone has some guidance on whats going on and how I can fix this, that would be great!

Upvotes: 0

Views: 181

Answers (1)

Anup
Anup

Reputation: 629

fetch() will give you a blob and you have to create url out of it to download that

Something like this might work

fetch('https://www.website.com/cap/people/profileExportPdf/pdf_name.pdf', {
    method: 'GET',
}).then(resp => resp.blob())
    .then(blob => {
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        // the filename you want
        a.download = 'name';
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
    })
    .catch((err) => console.log(err));

Upvotes: 1

Related Questions