Hello, like a few in the past, I have a need to create PDF documents from the content generated via Velo code (essentially, query results that are formatted in a certain way, like a summary of historical activity for a user under certain criteria).
Found an old post that was closed without resolution and then one with PDFgeneratorAPI (which now costs 60 USD per month!!!). Found pdfKit to be a better option (standard npm package), but can’t get it to work.
Here is what I have done so far:
-
Added CreatePDF() as an event in events.js backend file
-
Created pdfCreateor.jsw module in backend to call CreatePDF function
-
CreatePDF code was kept just like it was in the old closed post (for simplicity). Here it is:
import PDFDocument from 'pdfkit';
import fs from 'fs-extra';
export async function CreatePDF()
{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);
});
});
}
When I run this code in the backend using the RUN command (by the way, very useful one for backend code testing!), here is what I get as the message:
[backend/pdfCreator.jsw, CreatePDFjsw] called
[backend/pdfCreator.jsw, CreatePDFjsw] returned with undefined
I am expecting “write finished” as the message (resolve). Any hints/help?
Also, if anybody has had success with pdfKit, would they mind sharing their code and example output PDF files? Much appreciated!!