{"id":9102,"date":"2023-12-13T09:32:29","date_gmt":"2023-12-13T09:32:29","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9102"},"modified":"2025-09-01T06:39:59","modified_gmt":"2025-09-01T06:39:59","slug":"generate-an-svg-file-in-nodejs","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/generate-an-svg-file-in-nodejs","title":{"rendered":"How to Generate an svg File in Node.js (Data Visualisation Using rough.js)"},"content":{"rendered":"<p>See below example for How to Generate an svg File in Node.js<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\r\nconst { DOMImplementation, XMLSerializer } = require('xmldom');\r\nconst xmlSerializer = new XMLSerializer();\r\nconst document = new DOMImplementation().createDocument('https:\/\/www.bacancytechnology.com\/hire-rpa-developer', 'html', null);\r\n\/\/ Function to generate SVG content\r\nfunction generateSVG() {\r\n    const svg = document.createElementNS('http:\/\/www.w3.org\/2000\/svg', 'svg');\r\n    const rc = rough.svg(svg);\r\n    svg.appendChild(rc.rectangle(10, 10, 200, 100, { roughness: 2, fill: 'blue' }));\r\n\r\n\r\n    let xml = xmlSerializer.serializeToString(svg);\r\n    return xml;\r\n}\r\n\r\n\r\n\/\/ Function to save SVG to a file\r\nfunction saveSVGToFile(svgContent, fileName) {\r\n    fs.writeFileSync(fileName, svgContent, 'utf-8');\r\n    console.log(`SVG saved to ${fileName}`);\r\n}\r\n\r\n\r\n\/\/ Generate SVG and save to file\r\nconst generatedSVG = generateSVG();\r\nsaveSVGToFile(generatedSVG, 'output1.svg');<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>See below example for How to Generate an svg File in Node.js const { DOMImplementation, XMLSerializer } = require(&#8216;xmldom&#8217;); const xmlSerializer = new XMLSerializer(); const document = new DOMImplementation().createDocument(&#8216;https:\/\/www.bacancytechnology.com\/hire-rpa-developer&#8217;, &#8216;html&#8217;, null); \/\/ Function to generate SVG content function generateSVG() { const svg = document.createElementNS(&#8216;http:\/\/www.w3.org\/2000\/svg&#8217;, &#8216;svg&#8217;); const rc = rough.svg(svg); svg.appendChild(rc.rectangle(10, 10, 200, 100, { roughness: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9103,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-9102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9102"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9102"}],"version-history":[{"count":4,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9102\/revisions"}],"predecessor-version":[{"id":13322,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9102\/revisions\/13322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9103"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}