Reputation: 109
The following code is from a Node.js REST service. "generate" is a method on a class (not shown). The call to "generate" is shown below the class definition.
When I invoke this endpoint a PDF is created ("test.pdf") and the results come back to the browser; however, it's empty. It's as if the call to Handlebars didn't execute or the code got ahead of that call and executed it with an empty string ("h").
That being said, I've logged the value of "h", and it sure enough displays the HTML document with all the Handlebar placeholders filled out correctly.
I have a feeling this has to do with the Promises but I'm not sure. Hopefully another pair of eyes can help me see the problem.
NOTE: I'm aware of async/await, however, I'm on Node 6.9.5 so it's not available.
Thanks in advance!
class PDF {
...
generate() {
return new Promise((resolve,reject)=>{
this.render().then((h)=>{
const browser = Puppeteer.launch({headless: true}).then((b)=>{
const page = b.newPage().then((p)=>{
p.setContent(h, {waitUntil: 'load'}).then(()=>{
p.pdf({path: 'test.pdf', format: 'A4'}).then((z)=>{
resolve(z);
});
});
});
});
});
});
}
}
// *****************************************************************************
// REST API Routes
// *****************************************************************************
app.get('/mysite/pdf', (req,res)=>{
var pdf = new PDF(33);
pdf.generate().then((data)=>{
res.contentType('application/pdf');
res.send(data);
});
});
Upvotes: 0
Views: 1535
Reputation: 109
The problem wasn't Node, it was the HTML template I was loading. It had malformed HTML which was causing the page not to display in the browser not to mention generating a blank PDF.
Upvotes: 1