Hello,
I installed the pdfkit and fs modules to generate pdfs and pass them to the users of my site. I call the node modules from a backend file I created. Here is the code in that file:
const PDFDocument = require (‘pdfkit’);
const fs = require (‘fs-extra’);
export async function generatePDF()
{
var pdf;
var document = new PDFDocument();
var writeStream = fs.createWriteStream(‘filename.pdf’);
document.pipe(writeStream);
document.moveTo(300, 75)
.lineTo(373, 301)
.lineTo(181, 161)
.lineTo(419, 161)
.lineTo(227, 301)
.fill('red', 'even-odd');
var loremIpsum = ‘Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in…’;
document.y = 320;
document.fillColor('black')
document.text(loremIpsum, {
paragraphGap: 10,
indent: 20,
align: 'justify',
columns: 2
});
document.end();
var done = new Promise ( function (resolve, reject) {
writeStream.on(‘finish’, function () {
resolve(“write finished”);
});
writeStream.on(‘error’, function (error) {
reject(error);
});
});
pdf = await done;
return pdf;
}
This code should return “write finished” if the pdf was generated. However, it always returns Error: EROFS: read-only file system, open ‘filename.pdf’. I believe this is because we cannot write on wix’s server directory. This necessitates writing the PDF directly to a url and passing it back to the front end code. Does anyone know how to do this? (the ‘pdfkit’ documentation says you can pipe straight to res. But res is undefined in our backend code, so I can’t get that to work.)
Thanks,
Eric