I am trying to download a PDF file using PhantomJS. A direct URL to download this PDF does not exist, as it calls some internal JavaScript function when I click the submit button.
Here is the code that I use to download the 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)); };
'Id' is the identifier of the item for the submit button. The problem here is that although I get the answer (inside the onResourceReceived ) in JSON format, but I cannot save the attachment as some kind of PDF file.
When I run the above code, I get the following output as a 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 offer solutions using only PhantomJS. Thanks!
javascript phantomjs
Ishank jain
source share