I am trying to generate a PDF file in some method and then display it in browser. So far, what I have is this:
nightmare
.goto(url)
.wait('body')
.evaluate( ()=>document.querySelector('body').innerHTML)
.end()
.then((response) => {
return getJSONData(response);
})
.then((data) => {
return limiter.schedule(() => processJSONData(data));
})
.then((pages) => {
createFile(res, url, pages);
})
.catch(err => {
console.log(err);
The createFile()
method creates the PDF file :
function createFile(res, url, data){
var pdfdoc = new PDFDocument;
var fileName = "My_File.pdf";
pdfdoc.pipe(fs.createWriteStream(fileName));
var pagelist = data.toString().split(",");
// Add a new page and create the PDF by adding each image per page
const pages = pagelist.map(page => {
pdfdoc.addPage();
pdfdoc.image(page, {scale:0.45, align:'center', valign:'center'});
});
// Once all pages are finished, mark the end of document, log and return the filename
Promise.all(pages).then(() => {
pdfdoc.end();
}).then( () => {
console.log("PDF file created : "+fileName);
renderPDF(res, fileName);
})
}
I have the method renderPDF()
as follows :
function renderPDF(response, mangaName) {
var dest = path.resolve(mangaName);
console.log("Rendering PDF file "+dest);
console.log("Response object content follows.");
response.sendFile(dest);
}
I do not understand exactly what I am doing wrong here. I even tried this but it pops out the file with 0 bytes content. How can I fix this?