-
Notifications
You must be signed in to change notification settings - Fork 310
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
Closing Tabs after printToPdf #214
Comments
you can use CDP.Close method e.g. async function getPdf(html) {
const {client, tab} = await load(html);
const {Page} = client;
console.log("tabID: "+tab.id);
// https://chromedevtools.github.io/debugger-protocol-viewer/tot/Page/#method-printToPDF
const pdf = await Page.printToPDF(options.printOptions);
await CDP.Close({port: options.port, id: tab.id}); // here's you need to pass tab id
return pdf;
} |
@gnat42 yes you can do as @hbakhtiyor says (thanks!); as you can see there should be no need to also close the client. Just make sure to proper handle errors in your code so you don't leave dangling tabs around. |
Ah, that's how it works, I had tried CDP.Close({tab.id}); client.target.closeTarget() and a variety of others. This works perfectly. @hbakhtiyor Much appreciated! One question @cyrus-and being quite new to Node/JS and this api. I don't really know all the places/errors I could experience. I'm thinking that I need to close the tab in
Are there other places in that code that you see have potential for errors I'm not catching? If so are there docs someplace that detail what exceptions/errors a function can throw? |
@gnat42 every method that returns a Promise could in principle fail, if you use the try {
await f();
} catch (err) {
// notify the user
} finally {
// cleanup code
} |
Apologies, I've nearly got this working but can't seem to figure out how to close a tab that was opened.
Everything seems to work except for one part which is closing the tabs I've opened. I expose this code via https get/post requests. For each request I open a new tab and either fetch the html or load the html directly. Once loaded, output a pdf and return the filename they can request to get the printed pdf (or directly return it). This works pretty good. Using ab I can do many concurrent requests and everything seems to work. There is one small issue is that it doesn't seem to close the tab. Since I'm running headless I'm seeing a new process for each request open. For example, when I first start Chrome, I have a total of 5 processes. For every request I make, I get another chrome process. It doesn't seem to go away. I'm fairly certain its because I'm calling client.close(); but not closing the tab first. You can see in the function getPdf included below that I've commented where I think I should be closing the tab but have so far been unable to do so. Full project source is here
The text was updated successfully, but these errors were encountered: