node.jsfabricjs2

How to create a fabric object on the node server?


I'm working on an application, in which user creates a series of canvases in their browser (using fabricjs for that purpose). The data are stored in a database, which then are planned to be used on the server side (nodejs) to generate PNG images of the canvases and then put them in a downloadable PDF file (I know I can do all this on the client side but the specificity of the app requires me to do this on the server).

The problem? Whenever I'm trying to create a fabric object in the node, I get the Element is not defined error. Here's a code sample:

function generatePdf(someVeryImportantData) {

    var canvas = new fabric.StaticCanvas(null, { width: 300, height: 500});
    var text = new fabric.IText('Hello world');
                      //  ^ this is where the error occurs
    canvas.add(text); // <-- can't even reach that :-(
}

Node version: 8.11.2

Fabric version: 2.3.6

Tried downgrading node down to version 6, didn't help. It would be great if the solution worked both on client and server side.


Solution

  • It seems IText has an external dependency: fonts. Those are included by default in the web browser, not so much in nodejs, where they should be supplied explicitly. Using simple objects like geometric shapes works fine.