I'm trying to download some PDF file using PhantomJS. There is no direct URL for downloading that PDF, as it calls some internal JavaScript function, when I click the submit button.
Here is the code that I am using to download PDF file:
page.open(url, function(status){ page.evaluate(function(){ document.getElementById('id').click(); }); }); page.onResourceReceived = function(request){ console.log('Received ' + JSON.stringify(request, undefined, 4)); };
The 'id' is the element id for submit button. The problem here is that even though I am getting the response (inside onResourceReceived
callback) as JSON format, but I'm not able to save the attachment as some PDF file.
When I run the above code, I get following output as JSON string:
Received { "contentType": "application/pdf", "headers": [ // Some other headers. { "name": "Content-Type", "value": "application/pdf" }, { "name": "content-disposition", "value": "attachment; filename=FILENAME.PDF" }, ], "id": 50, "redirectURL": null, "stage": "end", "status": 200, "statusText": "OK", "url": "http://www.someurl.com" }
Please, suggest solutions using PhantomJS only. Thank you!
With the use of the <a> tag download attribute, we can download pdf files, images, word files, etc. The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink.
Scroll to the Privacy & Security settings and click 'Site Settings'. What is this? On the Site Settings page, click 'PDF documents'. On the page that follows, turn on the 'Download PDF files instead of automatically opening them in Chrome' option.
In general, I would recommend to stop using PhantomJS and have a look on Headless Chrome. Here is a nice article about this topic. I was using https://github.com/puppeteer/puppeteer for this purpose and it was an easily integrated solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With