Q:

Injecting data object to window with Puppeteer

Background
I am using Puppeteer to create some PDFs. I need to inject some data into the page when Puppeteer loads it. 
Problem 
I have tried using evaluateOnNewDocument() which was successful when using a String only. When I try with an Object it fails. I also tried with evaluate() and it fails regardless of what I pass in. 
Example 
// Works
    await page.evaluateOnNewDocument(() => {
           window.pdfData = {};
           window.pdfData = "Some String";
    });

// Does not work 
    await page.evaluateOnNewDocument(() => {
           window.pdfData = {};
           window.pdfData = data;
    });

// Fails
await page.evaluate(data => {
     window.pdfData = {};
     window.pdfData = data;
}, data);

I would like to access this object like this, 
const data = window.pdfData;

Question
What is the proper way to pass a data object into window on a loaded Puppeteer page so that it can be accessed within the page to use the data client side?

A:

Passing object to evaluate
You can pass data which will be serialized as JSON.
await page.evaluateOnNewDocument(data => { // <-- pass as parameter
  window.pdfData = data; // <-- read it here
}, data); // <-- pass as argument

Passing object to evaluateOnNewDocument
evaluateOnNewDocument works similarly to evaluate, except it will run whenever there is a new window/navigation/frame. This way the data will stay even if you navigate away to another page.
You can pass data and read inside the function. 
await page.evaluateOnNewDocument(data => {
  window.pdfData = data;
}, data);

