Reputation: 143
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:
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.
Upvotes: 0
Views: 1297
Reputation: 343
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
Upvotes: 0
Reputation: 191
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
Upvotes: 0
Reputation: 8861
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.
httpOnly
(it is not httponly
!)'domain':'.abc.com'
pair causes the empty array. It looks to me a CORS issue.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.
Upvotes: 0