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

[BUG] Unable to generate PDF file #148

Closed
timurguvenkaya opened this issue Aug 29, 2023 · 5 comments
Closed

[BUG] Unable to generate PDF file #148

timurguvenkaya opened this issue Aug 29, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@timurguvenkaya
Copy link

timurguvenkaya commented Aug 29, 2023

Environment

  • chromium Version: 116.0.0
  • puppeteer / puppeteer-core Version: 21.1.0
  • Node.js Version: 18.x
  • Lambda / GCF Runtime: nodejs18.x

Expected Behavior

PDF should be generated

Current Behavior

I manually created a layer through the AWS console and added arn to serverless.

I'm getting this error when I try to generate pdf with .pdf()

TypeError: t is not a constructor
    at ME (webpack:///puppeteer-core/lib/esm/puppeteer/common/util.js:451:12)
    at runNextTicks (node:internal/process/task_queues:60:5)
    at process.processImmediate (node:internal/timers:447:9)
    at u4.pdf (webpack:///puppeteer-core/lib/esm/puppeteer/common/Page.js:944:26)
    at Runtime.RY (webpack:///apps/api/src/handlers/bucket/upload.ts:100:24)
                                 const execPath = await chromium.executablePath();

                                 const browser = await puppeteer.launch({
					args: chromium.args,
					userDataDir: '/tmp',
					defaultViewport: chromium.defaultViewport,
					executablePath: execPath,
					headless: chromium.headless,
					ignoreHTTPSErrors: true,
				});

				const page = await browser.newPage();

				await page.goto(reportUrl, {
					waitUntil: ['networkidle0', 'domcontentloaded'],
				});

				console.log('Chromium:', await browser.version());
				console.log('Page Title:', await page.title());

				await page.emulateMediaType('screen');

				const webPagePdf = await page.pdf(); // Error here

				console.log('PAGE_PDF: ', webPagePdf);

				await page.close();

				await browser.close();

serverless.yml

uploadFunction:
    runtime: nodejs18.x
    handler: src/handlers/bucket/upload.handler
    events:
      - httpApi:
          path: /upload-report
          method: POST
    iamRoleStatements:
      - Effect: Allow
        Action:
          - s3:PutObject
        Resource:
          - "arn:aws:s3:::${self:custom.uploadBucketName}/*"
    layers:
      - arn:aws:lambda:us-east-1:************:layer:chromium:*

Steps to Reproduce

Try generating a pdf with some URL as in my code.

Possible Solution

Would love to get some tips what can be an issue

@timurguvenkaya timurguvenkaya added the bug Something isn't working label Aug 29, 2023
@Sparticuz
Copy link
Owner

I don't know much about it, but webpack requires you to use the externals feature

@fire015
Copy link

fire015 commented Sep 9, 2023

Works fine for me, but its hanging after calling browser.close()

@dfmmalaw
Copy link

Same issue for me. hanging after calling browser.close()

@Sparticuz
Copy link
Owner

If you are able to disable the graphics stack (if you don't need web gl) then browser.Close works... Or at least that's what I've heard

@praoshealth
Copy link

praoshealth commented Oct 7, 2023

The reason it is hanging is because there are 2 pages open and you have to close all pages before you close the browser. It must be loading a page by default.

const pages = await browser.pages();

console.log('pages', pages.length);

for (let i = 0; i < pages.length; i++) {
    await pages[i].close();
}

await browser.close();

Awesome repo BTW. Saved me a ton a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants