25

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!

user7637745
  • 965
  • 2
  • 14
  • 27
Ishank Jain
  • 309
  • 5
  • 8
  • 1
    possible duplicate of [downloading a file that comes as an attachment in a POST request response in PhantomJs](http://stackoverflow.com/questions/16144252/downloading-a-file-that-comes-as-an-attachment-in-a-post-request-response-in-pha) – Artjom B. Jul 22 '15 at 15:15
  • 2
    All answers are given in casperjs. Is there any solution for this question using phantomjs? – Ishank Jain Jul 22 '15 at 15:26
  • Sorry, about that. I've grabbed the wrong link. Will see if I can post another link later. – Artjom B. Jul 22 '15 at 15:28
  • Possible duplicate of [Trouble downloading PhantomJS generated pdf](http://stackoverflow.com/questions/17506210/trouble-downloading-phantomjs-generated-pdf) – Paul Sweatte Jan 18 '17 at 00:14

2 Answers2

1

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.

Jakub Kubista
  • 101
  • 10
0

I've been using a package called 'html-pdf' (which uses PhantomJS behind the scenes) in order to create .pdf files (using ejs templates). Hopefully the method I've been using gives you some guidance.

I'm using Angular, but this should apply to what you're doing. The method I used receives the response from the server as a blob, and creates a new Blob instance, then downloads the created .pdf to the browser using a plugin (which should exist for your framework of choice):

  generatePDF(quote) {
    this.http.post(ENV.pdfURL, { quote }, {
      // tell the server that response type expected is blob
      responseType: 'blob'
    }).subscribe((res) => {
      // create a new instance of blob using Blob constructor
      const blob = new Blob([res], { type: 'application/pdf' });
      const filename = `${quote.customerCompany}-${this.getDate()}-${quote.productName}-quote-${quote.selectedDuration / 12}yr.pdf`;
      // use a plugin to save the file to the browser
      FileSaver.saveAs(blob, filename);
    });
  }

I think that the reason you're having trouble here is that you're asking the server to send you JSON as a response, instead of a blob file object.

jacobedawson
  • 2,929
  • 25
  • 27