westy92 / html-pdf-chrome

HTML to PDF or image (jpeg, png, webp) converter via Chrome/Chromium
https://www.npmjs.com/package/html-pdf-chrome
MIT License
776 stars 60 forks source link

Allow Chrome instance to launch on first run and persist for future runs #308

Open adamreisnz opened 3 years ago

adamreisnz commented 3 years ago

Hi @westy92 , we're using this library on Heroku and it's doing the job of generating PDF files without issues. However, we are trying to ascertain whether the Heroku buildpack starts and runs chrome in the background or whether this library is forced to start it up for each use.

Is there a way to log/debug that using this library?

And is there perhaps a flag or setting we can use to keep the app running after a first PDF has been generated?

Keen to hear your suggestions/tips, as I think currently it's starting chrome up for every PDF causing needless overhead.

I have a related ticket open at https://github.com/heroku/heroku-buildpack-google-chrome/issues/111

westy92 commented 3 years ago

If you pass a host and port in the CreateOptions then this library will not launch Chrome. If you omit them, it will, since there is no existing instance of Chrome for it to use. See: https://github.com/westy92/html-pdf-chrome/blob/master/src/index.ts#L32

As recommended in the README, you can look into a solution like this to ensure Chrome runs once and stays running: https://github.com/westy92/html-pdf-chrome#usage

adamreisnz commented 3 years ago

I am enquiring for a solution specifically for Heroku. As far as I'm aware, Heroku has it's own process manager to ensure that a web process keeps running and restarts after it crashes. So as far as I'm aware, it's not possible to install pm2 on it and use it to keep Chrome running.

What I am asking for, is a solution with this library. Since it is obviously capable of starting chrome instance on demand if needed, would it not be capable of starting Chrome and keep it running so that any future PDF generation calls can utilise this existing Chrome instance?

For example, if we don't specify host/port, and it launches a Chrome instance, can we pass a flag like keepAlive: true to ensure the instance will keep running? And for future calls to use this instance, if it is available?

westy92 commented 3 years ago

Ah, I understand the intention, thank you for explaining more.

I think your best action would be to directly use what this library uses under the hood, chrome-launcher.

This library isn't currently designed to hold any state between runs, but I will mark this as a feature request.

adamreisnz commented 3 years ago

Great, thank you I will look into that library.

On Thu, 5 Aug 2021 at 14:17, Seth Westphal @.***> wrote:

Ah, I understand the intention, thank you for explaining more.

I think your best action would be to directly use what this library uses under the hood, chrome-launcher https://github.com/GoogleChrome/chrome-launcher.

This library isn't currently designed to hold any state between runs, but I will mark this as a feature request.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/westy92/html-pdf-chrome/issues/308#issuecomment-893110638, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADXYQXSRQG76GP6NOK25ELT3HYCRANCNFSM5BN7Y7CQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

adamreisnz commented 3 years ago

This seems to work well, using the Chrome launcher under the hood. We now use it to launch an instance of Chrome and pass the port to this library, which then reuses this instance rather than launching one itself (and terminating it afterwards).

Would still be keen on a flag to possible keep the instance alive 👍🏼