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

AWS lambda: running puppeteer-core 21.5.2 and @sparticuz/chromium 118.0.0 on node 18 runtime failes ONLY when my html (for PDF rendering) contains https #12358

Closed
kalintro opened this issue Apr 28, 2024 · 1 comment

Comments

@kalintro
Copy link

Steps to reproduce

Snippet of the actual code rendering the pdf:

    // Synchronous processing section
    try {
        // Initialize puppeteer browser
        browser = await puppeteer.launch({
            args: chromium.args,
            defaultViewport: chromium.defaultViewport,
            executablePath: await chromium.executablePath(),
            headless: chromium.headless,
        });

        const page = await browser.newPage();

        page.on('requestfailed', request => {
          console.log(`Failed request: ${request.url()} Reason: ${request.failure().errorText}`);
        });

        // Log contentData for debugging
        console.log("Content Data:", contentData);

        // Directly use header and footer templates from contentData
        const headerTemplate = contentData.headerTemplate;
        const footerTemplate = contentData.footerTemplate;

        // Log the header and footer templates for debugging
        console.log("Header Template:", headerTemplate);
        console.log("Footer Template:", footerTemplate);

        // Log the content data being passed to the Handlebars template
        console.log("Content Data being passed to template:", JSON.stringify(contentData.content_data));

        // Compile the HTML template with Handlebars and set content
        const template = Handlebars.compile(htmlTemplate);
        const htmlContent = template(contentData.content_data);
        await page.setContent(htmlContent);

        // Log the final HTML content
        console.log("Final HTML Content being set in Puppeteer:", htmlContent);

        // Use margins from contentData if available, else use default margins
        const margins = contentData.margin || { top: "40px", bottom: "40px", left: "30px", right: "30px" };

        // Generate the PDF with specified options
        const pdfOptions = {
            format: contentData.format || 'A4',
            printBackground: typeof contentData.printBackground === 'boolean' ? contentData.printBackground : true,
            margin: margins,
            displayHeaderFooter: contentData.displayHeaderFooter || false
        };

        // Conditionally set header and footer templates based on displayHeaderFooter value
        if (pdfOptions.displayHeaderFooter) {
            pdfOptions.headerTemplate = headerTemplate || '';
            pdfOptions.footerTemplate = footerTemplate || '';
        } else {
            pdfOptions.headerTemplate = '';
            pdfOptions.footerTemplate = '';
        }

        // Check if orientation is provided in contentData and set landscape if specified
        if (contentData.orientation && contentData.orientation.toLowerCase() === 'landscape') {
            pdfOptions.landscape = true; // Set landscape mode if orientation is landscape
        }

        const pdfBuffer = await page.pdf(pdfOptions).catch(error => console.error('Error generating PDF:', error));

Expected results

A generated PDF with all html and content data in place. It works like a charm when sending only pure html without any https images or refferences

Actual results

2024-04-27T10:35:09.407Z 1a08f2ac-7988-451b-aab8-2df7e4df9632 ERROR Error during PDF generation: Error: Navigating frame was detached
at #onFrameDetached (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/LifecycleWatcher.js:109:47)
at /opt/nodejs/node_modules/puppeteer-core/lib/cjs/third_party/mitt/mitt.js:1:732
at Array.map ()
at Object.emit (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/third_party/mitt/mitt.js:1:716)
at CdpFrame.emit (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/EventEmitter.js:91:23)
at #removeFramesRecursively (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/FrameManager.js:454:15)
at #onClientDisconnect (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/FrameManager.js:102:42)

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 28, 2024

This is likely an indication of a renderer crash (you can double check by inspecting the CDP log https://pptr.dev/guides/debugging#log-devtools-protocol-traffic). We are not able to troubleshoot and debug issues with AWS Lambda and @sparticuz/chromium. I suggest you report the issue to @sparticuz/chromium. Alternatively, please reproduce the issue outside the AWS Lambda environment using the latest version of Puppeteer.

@OrKoN OrKoN closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants