0

I use PhantomJS as a tool for PDF rendering. Here is the code snippet:

page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            //page.render(output);
            page.render('/dev/stdout', { format: 'pdf' })
            phantom.exit();
        }, 30000);
    }
});

As you can see, render() method will be called after 30 seconds in case of success respond after open() method. Is there a possibility to use some callback when this pdf-file is really created, rendered and ready to be downloaded, to provide a link to user not after 30 seconds, but exactly after the file created?

max
  • 612
  • 7
  • 26
  • Yes, `page.render()` is a synchronous function, so it blocks until it is done, so you can simply call the code that depends in the `render` output directly behind that. Though, I'm not sure what this has to do with downloading stuff. It's not clear what you want to achieve. Be aware of the [XY problem](http://meta.stackexchange.com/q/66377/266187). – Artjom B. Aug 18 '16 at 18:45
  • Hi Artjom, I would like to achieve following: 1. When user press 'Download' button, I as a developer need to be sure that requested file is really rendered and ready to be downloaded. Only after this text of this button can be changed to 'Save', and when user presses this button again, file can be downloaded. 2. We achieved this already with provided code snippet, but with timeout (30 sec). How can I let user know tat file is rendered without timeout, but using specific event (something like 'file-is-rendered')? Thanks. – max Aug 19 '16 at 06:44
  • Does it make sense to use waitfor.js?https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js – max Aug 19 '16 at 18:25
  • The 30 second timeout that you have is actually **before** you even call `page.render`. It seems to me that you're actually asking how to wait until the page is fully loaded before starting to render it, right? – Artjom B. Aug 19 '16 at 19:03
  • Artjom, right. You know, each pdf-file can have different size (depending on structure complexity), so I don't know for sure which time is needed for preparing that file and when I can download it. – max Aug 20 '16 at 10:42
  • 1
    As I said, `page.render` is synchronous, so you don't have to wait for the generation of the file, but for the page to be ready. Have a look at [this](http://stackoverflow.com/a/21401636/1816580) to wait for a "full" page load before rendering the page. – Artjom B. Aug 20 '16 at 10:48
  • Artjom, danke Vielmals! – max Aug 20 '16 at 10:55
  • (Kein Problem) ... If this solves your issue, I would close your question as a duplicate. – Artjom B. Aug 20 '16 at 10:58
  • It definitely duplicated. I'll try to implement different approaches and let you know if I'll have any issue :) – max Aug 20 '16 at 11:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121437/discussion-between-max-and-artjom-b). – max Aug 20 '16 at 19:20

0 Answers0