I’m encountering a problem while utilizing Puppeteer for a project. Here’s the code I’m working with:
const puppeteer = require('puppeteer');
(async () => {
const browserInstance = await puppeteer.launch();
const newTab = await browserInstance.newPage();
await newTab.goto('https://10.1.40.117/print/d37a2017-4bc4-46fb-9a8a-7ddc31e65a33', {waitUntil: 'networkidle2'});
await newTab.pdf({path: 'hn_output.pdf', format: 'A4'});
await browserInstance.close();
})();
However, I keep receiving the following error message:
(node:16064) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: net::ERR_CERT_AUTHORITY_INVALID at https://10.1.40.117/print/d37a2017-4bc4-46fb-9a8a-7ddc31e65a33
(node:16064) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, unhandled rejections will stop the Node.js process and return a non-zero exit code.
Could anyone assist me with this issue?
Hey FlyingStar, the error you’re seeing is because Puppeteer doesn’t trust self-signed certificates by default. You can bypass this issue by adding ignoreHTTPSErrors: true
when launching the browser:
const puppeteer = require('puppeteer');
(async () => {
const browserInstance = await puppeteer.launch({ignoreHTTPSErrors: true});
const newTab = await browserInstance.newPage();
await newTab.goto('https://10.1.40.117/print/d37a2017-4bc4-46fb-9a8a-7ddc31e65a33', {waitUntil: 'networkidle2'});
await newTab.pdf({path: 'hn_output.pdf', format: 'A4'});
await browserInstance.close();
})();
That should do the trick!
While the solution provided by CreativeArtist88 is applicable for bypassing the HTTPS errors, it's important to consider the implications of ignoring certificate errors, especially in environments where security is a priority. Ignoring HTTPS warnings can expose applications to security vulnerabilities, particularly if you're dealing with sensitive information.
If you're working in a development environment and ignoring certificate errors is acceptable, you can certainly use the ignoreHTTPSErrors: true
option as shown. However, for production environments, a more secure approach would be to ensure that the certificate is valid. Here’s how you can do that:
- Obtain a certificate from a trusted Certificate Authority (CA).
- Install the certificate on your server where the application runs.
- Ensure your server is correctly configured to use this certificate.
Implementing these steps would ensure that your Puppeteer application complies with standard security practices without compromising on the safety of your data. If you face difficulties setting up a valid certificate, consulting with your network administrator or using a service like Let's Encrypt could help facilitate this process.