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!