Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Completion Trigger always times out #137

Open
danielwestendorf opened this issue Mar 30, 2018 · 5 comments
Open

Event Completion Trigger always times out #137

danielwestendorf opened this issue Mar 30, 2018 · 5 comments

Comments

@danielwestendorf
Copy link

I'm having trouble getting completion triggers working. What I think was working with a previous release is no longer working for me.

html-pdf-chrome version: 0.5.0

const htmlPdf = require('html-pdf-chrome');
const html = '<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <h1 id="title">Event completion trigger test</h1> <script type="text/javascript"> let title = document.getElementById("title"); title.dispatchEvent(new CustomEvent("pdfRenderReady", {})); </script> </body> </html>'

const options = { 
  completionTrigger: new htmlPdf.CompletionTrigger.Event('pdfRenderReady', '#title', 2000) 
}

htmlPdf.create(html, options).then((pdf) => pdf.toFile('test.pdf'));

This results in:

> htmlPdf.create(html, options2).then((pdf) => pdf.toFile('test.pdf'));
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      { removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [] } }
> (node:21385) UnhandledPromiseRejectionWarning: Error: CompletionTrigger timed out.
    at /Users/dwestendorf/Repos/pdf-bot-pro/node_modules/html-pdf-chrome/lib/src/index.js:155:23
    at Generator.next (<anonymous>)
    at fulfilled (/Users/dwestendorf/Repos/pdf-bot-pro/node_modules/html-pdf-chrome/lib/src/index.js:4:58)
    at <anonymous>
(node:21385) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

What is my malfunction?

@danielwestendorf
Copy link
Author

Womp! The trigger was firing before the page was fully loaded. This html works as expected.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <h1 id="title">Event completion trigger test</h1>
    <script type="text/javascript">
      setTimeout(function() {
        let title = document.getElementById("title");

        title.dispatchEvent(new CustomEvent("pdfRenderReady", {}));
      }, 500)
    </script>
  </body>
</html>

@danielwestendorf
Copy link
Author

Un-womp.

It seems that event completion triggers only works after some arbitrary point in time, I assume after the page navigation has completed. If the JS has completed before that, then the event is never received.

I tried firing my event after the load event was received on the window, and this was too soon. I have to add an arbitrary timeout, or repeatedly fire the the event to get the completion trigger to fire.

My use case is that I have browser-side JavaScript which sometimes takes a long time to evaluate, and at other times is really quick, depending on the data being rendered. I was hoping to use a single event to indicate that the load is complete.

@westy92
Copy link
Owner

westy92 commented Apr 2, 2018

I'll look into attaching an event listener before the page load. Thanks for the info!

@danielwestendorf
Copy link
Author

FWIW, it may just make more sense to eliminate events as a completion trigger and encourage variable or element triggers.

@630348568
Copy link

I also found this problem. How do I deal with it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants