pdfkit node module

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

2 Likes

hi there, did you make any progrgress regarding that?
i want to print events and blog posts - at least events are accessible as DB…

I could not figure it out using pdfkit. However, it’s fairly straightforward to sign up for an acocunt on https://pdfgeneratorapi.com/ which allows you to create API calls in your backend code and have pdf documents returned.

requires is not supported by wix use import to define require npm

const PDFDocument = require (‘pdfkit’);
const fs = require (‘fs-extra’);

to

import PDFDocument from ‘pdfkit’;
import fs from ‘fs-extra’;

and how do you download the pdf or blob to a new tab :(… I coulnt figure out that return

Uncaught (in promise) Error: EROFS: read-only file system, open ‘informacion.pdf’ this is from return statement

Were you able to find a solution? I’m having the same issue with PDFkit

If you are having an issue with code, p lease add your own issue into a new post instead of bumping up an old post. Explain what you are trying to do, what works, and what doesn’t. Show your code.