0

I am using AWS Lambda + Puppeteer + Handlebars to generate PDF at runtime. I am creating the HTML on the fly and need to set cookies in order to render some images in the HTML pages before creating the PDF.

Puppeteer v4.0.0 Node v12.x

I have run into 2 issues:

  1. page.setCookie() not working
  2. I have custom fonts to be applied to the PDF - I have copied fonts and placed fonts config in the fonts folder and set FONTCONFIG_PATH env variable to /var/task/fonts (which seemed to work in PhantomJS but not in Chrome headless).

I have tried all possible ways to page.setCookie() but it doesn't seem to be working.

Code:

const page = await browser.newPage();
var pdfCookies = {
    "name": "edge-auth", // required
    "value": conf["edge-auth"], // required
    "domain": ".abc.com",
    "path": "/",
    "httponly": true,
    "secure": true
};
await page.setCookie(pdfCookies);
await page.setContent(data, {waitUntil:'networkidle0'});

I have added a console log for "await page.cookies()" - but I get a blank array [] as output.

Any help will be appreciated.

3 Answers3

0

page.setCookie

  1. You should use page.setCookies() in the following order, otherwise puppeteer will set the required cookies on the about:blank page:
const page = await browser.newPage();
await page.goto(url);

const pdfCookies ={
  'name': 'edge-auth',
  'value': conf["edge-auth"],
  'path': '/',
//'domain': '.abc.com',
  'httpOnly': true,
  'secure': true
}

await page.setCookie(pdfCookies);

const cookies = await page.cookies(url);
console.log(JSON.stringify(cookies));

I suggest to open any URL (or at least a Data URI like: await page.goto('data:text/html,<h1>Template</h1>');) where you can set the content later on.

  1. You also have a typo in httpOnly (it is not httponly!)
  2. Actually the 'domain':'.abc.com' pair causes the empty array. It looks to me a CORS issue.

Fonts

It depends on how you are using the environment variables. I suppose they are passed from the Node.js script to the Handlebars template. Make sure to use them like process.env.FONTCONFIG_PATH. You can ˙console.log()˙ it in the begenning of the script to see if it is visible to your environment or if it has the correct value.

theDavidBarton
  • 3,429
  • 3
  • 10
  • 33
  • Thanks! for your help David. The Cookies issue is resolved. It worked! Thanks once again. Regarding Fonts issue - I am still not able to load the fonts. I did check the FONTCONFIG_PATH with console.log and here's the output: FONTCONFIG_PATH: /var/task/fonts – Manmeet Bhurjee Jun 29 '20 at 04:37
  • About the font I have no idea. You may have success if you use `page.addStyleTag()` to set the font's path inline in the template. Or if your font is avilable at Google Fonts as well I'd suggest to use it in the hbs template file from the url (like in this example: http://www.codepool.info/2018/12/puppeteer-future-of-automation.html). If you set it from url you should wait until `networkidle0` but as I see you are already using it. – theDavidBarton Jun 29 '20 at 08:40
  • Thanks for all your help, David, appreciate it. – Manmeet Bhurjee Jun 29 '20 at 08:59
  • calling `page.goto` before calling `page.setCookie` is *NOT* required (and also doesn't make sense) so the order in-fact doesn't matter. There is no problem to set cookies on an 'about:blank' page as long as you're setting the domain of your cookies correctly – gilad mayani Apr 19 '21 at 15:51
  • the reason for the empty array in `page.cookies` is puppeteer will only return the specified domain's cookies, which by default will be current domain. calling `page.cookies(myDomain)` will return the correct cookies. – gilad mayani Apr 19 '21 at 15:56
  • hi @giladmayani, thank you for taking the time and reply to this answer. could you kindly elaborate your solution in a separate answer with a usage that you advise? I am pretty convinced that `page.setCookie` after `page.goto` does make sense ;) – theDavidBarton Apr 19 '21 at 20:53
  • @theDavidBarton I don't have a solution to the OP because I don't know that specific case and I don't know `page.setContent` but I have working apps with `page.setCookie` coming before `page.goto`, you can also see answers on https://github.com/puppeteer/puppeteer/issues/1342 `setCookie` after `page.goto` won't make sense e.g when opening a page with session cookies. if the cookies are not there from the beginning, it will redirect to 'login' page. cookies are often set by one page and later read by another. – gilad mayani Apr 20 '21 at 10:12
0

You should use cookies like this:

    const pdfCookies =[{
  'name': 'edge-auth',
  'value': conf["edge-auth"],
  'path': '/',
//'domain': '.abc.com',
  'httpOnly': true,
  'secure': true
}]

And then they should work

Paja Aleksic
  • 121
  • 2
0

This is the definition that is given by Puppeteer Docs


const pdfCookies ={
  'name': 'version',
  'value':"2",
  'url':"https://examplke.com"
}

const browser = await puppeteer.launch({
        headless: false,
        devtool: true,
    });


let page1 = await browser.newPage();
            await page1.setCookie(pdfCookies);
            await page1.goto(each);

https://pptr.dev/#?product=Puppeteer&version=v1.12.2&show=api-pagesetcookiecookies

lokesh
  • 134
  • 6