Converting signature (.svg file) into a png

Is there a way to convert a signature (.svg file) into a png? The reason we would want to convert it to png is because the service that we’re uploading the signature to doesn’t support previewing svgs, but it does support pngs.

Hi Alex

I did some googling and found an NPM package that will do the conversion in memory, as opposed to in some chromium window or on the file system which is not supported in Cloud Code.

Below code should work for you use case

const svg2img = require('svg2img');
export async function run() {
    // get the object that has the signature attachment
    var object = await DB.object.first();
    // get the URLS for that signature attachment
    var urls = object.signature_field.urls;
    // use the svg URL which is a link to the raw SVG data
    var svgUrl = urls.svg;
    // fetch the raw data from the URL
    let svgResponse = await fetch(svgUrl);
    if (svgResponse.ok) {
        // parse the raw data from the fetch response
        let svgData = await svgResponse.text();
        // Convert from svg string - returns a Buffer
        svg2img(svgData, async function(error, buffer) {
            // use Buffer to create new PNG attachment
            var png = await Attachment.create({filename: 'signature.png', data: buffer});
            // store PNG attachment wherever
            object.signature_png_field = png;