Draw SVG with NodeJS in backend Server

979 views
Skip to first unread message

Loïc TESSIER

unread,
Dec 22, 2021, 11:15:55 AM12/22/21
to JointJS
Hello everybody,

for an industrial project, we have to draw a large quantity of diagrams and export them as SVG in a PDF file.

For performance purpose, the drawing should be more efficient if it is done by NodeJS in backend Server.

I used JSDOM library to emulate a dom and draw svg but an error occured :

--> jQuery requires a window with a document

here is my code :

const jsdom = require("jsdom");

const { JSDOM } = jsdom;
const fs = require ('fs')
const joint = require ('jointjs')
const path = require ('path')

const filepath = path.join( __dirname , './template_raccordement.html')
const content = fs.readFileSync(filepath, {encoding:'utf8', flag:'r'});
const myLibrary = fs.readFileSync("../node_modules/jointjs/dist/joint.js", { encoding: "utf-8" });

const { window } = new JSDOM(content, {
    contentType: "text/html",
    includeNodeLocations: true,
    resources: "usable",
    storageQuota: 10000000,
    runScripts: "dangerously"
});

const $ = require('jquery')(window);

global.window = window
global.document = window.document

global.jQuery = $
global.$ = $

var element = $('#raccordementDiv')

console.log (element.text())

    var graph = new joint.dia.Graph;

    const paper = new joint.dia.Paper({
        el: element,
        model: graph,
        width: 1600,
        height: 800,
        gridSize: 1,
        interactive: true,
        async: true,
        frozen: false,
        sorting: joint.dia.Paper.sorting.APPROX,
        background: { color: '#F3F7F6' },
        //cellViewNamespace: shapes
    });

    console.log (paper)


After many hours on the internet to find a fix, i don't know how to do.

I have also tested with phantomJS but this one seems too older, deprecated and hard to deal with it.

Any idea ?
maybe another library for emulate jointjs in nodejs ?

thanks in advance for your help And have a merry christmas

Loic

Alexander Nestorov

unread,
Feb 3, 2022, 11:53:28 AM2/3/22
to JointJS
Hi!

JSDOM doesn't support some (most?) of the SVG specs. IMHO you'll have better luck running this in a headless chrome.

Regards!

Roman Bruckner

unread,
Feb 3, 2022, 12:13:59 PM2/3/22
to joi...@googlegroups.com
I agree. You can try Puppeteer (https://github.com/puppeteer/puppeteer) for instance.

Take a screenshot or convert to pdf (similar to this https://github.com/kumilingus/svg2pdf/blob/master/index.js).

Also note that `'data:image/svg+xml;utf8,' + paper.svg.outerHTML` is an SVG image.

Best,
Roman




--

---
You received this message because you are subscribed to the Google Groups "JointJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jointjs+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jointjs/396b3213-248e-44f2-b14a-6e9376278c31n%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages