Rasterization

The ESD/SPD Web editor uses the Scalable Vector Graphics (SVG) format as its underlying document format. SVG diagrams created in the ESD/SPD Web editor can be displayed in any modern browser as well as other SVG-capable applications. If your application requires diagram storage or rendering using raster images, you can use one of the diagram rasterization technologies provided by the ESD/SPD Web SDK. Three methods are supported:

Rasterize REST Service:

As described here the ESD/SPD Web Editor relies on a web service provided by ESD/SPD Web Editor Server. This service exposes a rasterize endpoint that your application can call to generate an image from an SVG diagram. The rasterize service accepts a stringified Javascript object containing rasterization options. This object has the same format as exportImage method's options parameter. In addition, the object must include a data property, containing the SVG diagram to be rasterized. The object may also include a uom property, containing the unit-of-measure to be used when generating the image. The valid values for the uom property sre:

If the uom propery is not included, the the image will be generated using US feet and inches.

Sample Javascript code to rasterize a diagram:


// sample assumes the svgDiagram variable contains an SVG diagram authored
// in the ESD/SPD Web editor.

var xhr = new XMLHttpRequest(),
    rqst = {
        image: true,
        encoding: 'base64',
        format: 'image/png',
        width: 600,
        height: 600,
        margin: 12,
        data: svgDiagram
    },

    // assuming the ESD/SPD service is located at http://localhost:3000
    path = "http://localhost:3000/REST/rasterize";

    xhr.open('POST', path, true);
    xhr.responseType = 'text';
    xhr.onload = function() {
        var rasterImage;
        if (this.status == 200) {
            rasterImage = this.response;
            // rasterImage is a string containing the rasterized diagram, base64 encoded
        }
    };

    xhr.send(JSON.stringify(rqst));
            

Rasterizer Command Line Utility:

The ESD/SPD Web SDK includes a command line utility you can use to convert ESD/SPD Web editor diagrams to image files.

The rasterizer is called svg_rasterizer.exe and is installed in:

<<SDK install directory>>/server/bin

The rasterizer utility can be controlled by command line arguments or by a JSON file. To operate the utility with command line arguments, invoke the utility with a command line similar to:

svg_rasterizer --sourcePath=c:\diagrams\diagram1.svg --imagePath=c:\images\diagram1.png --format=image/png

The rasterizer utility supports these command line arguments:

help — Displays help.

control — Path to a control file. If control is specified other arguments should be omitted. The format of this file is described below.

sourcePath — Path to a diagram file created by the ESD/SPD Web editor.

imagePath — Path for the output image file.

format — Specifies the image format. 'image/jpeg' and 'image/png' are supported.

imageQuality — This value sets the quality level for a JPG image. Acceptable values are between 0.0 and 1.0. For best quality specify 1.0. A quality setting of 0.75 provides good image quality and compression. Lower quality settings provide greater image compression. The default is 0.75. This value is ignored when exporting a PNG image.

trim — This option, when set to true, trims the white space so the resulting image has the same aspect ratio as the diagrams viewbox.

width — The image width in pixels. The default is 600px.

height — The image height in pixels. The default is 600px.

margin — The image margin width in pixels. The default is 10px.

encoding — Specify 'base64' to base-64 encode the image. If encoding is any other value or is not specified, the image will be stored in binary format.

includePrefix — When set to 'true', adds a data: URL prefix to the exported image string. The setting applies only if encoding is 'base64'.

zoomTo — Optional clip rectangle in 'left top width height' format. The values are specified in the ESD/SPD Web editor's coordinate system.

timeout — Rasterizer timeout in milliseconds. If the rasterization process takes longer than the specified timeout, the utility will exit with an error code. The default timeout is 30,000 milliseconds (30 seconds).

The rasterizer utility also supports a JSON-format control file. Using this method you can rasterize multiple diagrams in a single invocation of the utility. Command line argument --control specifies the name of a JSON file containing instructions for the rasterizer.

svg_rasterizer --control=c:\config.json

A sample control file:

        {"timeout": 15000,
         "items":
            [
                {
                    "sourcePath": "c:/diagrams/diagram1.svg",
                    "imagePath": "c:/images/diagram1.png",
                    "format": "image/png",
                    "width": 1024,
                    "height": 1024,
                    "margin": 25,
                    "encoding": ""
                },
                {
                    "sourcePath": "c:/diagrams/diagram2.svg",
                    "imagePath": "c:/images/diagram2.jpg.txt",
                    "format": "image/jpeg",
                    "imageQuality": 0.85,
                    "width": 1600,
                    "height": 1200,
                    "margin": 35,
                    "encoding": "base64",
                    "includePrefix": true
                },
                {
                    "data": " ..."
                    "imagePath": "c:/images/diagram3.png",
                    "format": "image/png",
                    "width": 1024,
                    "height": 1024
                }
            ]
        }
    

The timeout item specifies the rasterizer timeout in milliseconds. The items array should contain one or more rasterization specification objects. Each specification object is a hash of rasterization control options - these options have the same names and meaning as the command line options described above. One additional option is supported: a data member that contains the SVG diagram to be rasterized. When data is used, sourcePath should be omitted.

Return Codes

On exit svg_rasterizer returns a numeric code. These codes may be returned:

0 — Success.

1 — A command option is in error.

2 — Control data is incomplete.

3 — The control file could not be opened.

6 — A dependency is missing or encountered an error.

10 — An unspecified error occurred.

11 — The output image path was not specified.

12 — The output image file could not be created.

13 — The image format is not valid. 'image/jpeg' and 'image/png' are supported.

14 — An error occurred saving the image file. This may be due to a permissions issue, insufficient space, or some other file creation or write problem.

15 — The input diagram file could not be loaded.

16 — The input diagram file could not be opened.

17 — The input diagram file could not be parsed.

18 — A timeout occurred.

19 — A socket communication error occurred.

20 — A rasterizer browser initialization error occurred.