# Work with SVG files

## Content

<span class="fade-in-token">SVG (Scalable Vector Graphics) is a vector image format that allows graphics to be rendered at any size without loss of quality. DsPdfJS provides APIs to load, render, and generate SVG documents.</span>

### Render SVG to PNG

You can render an SVG image to a bitmap and save it as a PNG image. In DsPdfJS, this is done by loading an SVG file into an SvgDocument object and drawing it onto a BmpContext.
The BmpContext class provides several methods for rendering SVG content:

* [drawSvg](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/BmpContext#drawsvg) – draws the SVG document using its viewport position.
* [drawSvgContent](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/BmpContext#drawsvgcontent) – draws the SVG content using the content bounds.
* [drawSvgToContentRect](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/BmpContext#drawsvgtocontentrect) – draws and scales the SVG content to fit a specified rectangle.
* [drawSvgToRect](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/BmpContext#drawsvgtorect) – draws and scales the SVG viewport to fit a rectangle.

To render an SVG image to PNG:

1. Load the SVG file using the [SvgDocument.load](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgDocument#load) method.
2. Create a [BmpContext](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/BmpContext) to provide the drawing surface.
3. Draw the SVG document using the [drawSvg ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/BmpContext#drawsvg)method (or another SVG drawing method if resizing or positioning is required).
4. Save the resulting bitmap as a PNG image.

The following code example shows how to render an SVG image to a PNG file.

>type=note
> **Note:** The examples on this page use helper functions from the [Snippet Helpers](https://developer.mescius.com/document-solutions/javascript-pdf-api/docs/snippet-helpers) module. These utilities are provided only to simplify the examples and are not part of the DsPdfJS API.

```auto
const svgDoc = SvgDocument.load(await Util.loadFile("images/ImageAlign.svg"));
svgDoc.sansSerifFont = Font.getPdfFont(StandardPdfFont.Helvetica);

const rect = svgDoc.measure(0, 0);
const ctx = new BmpContext(rect.width, rect.height, 2, "GhostWhite");
ctx.drawSvg(svgDoc, -rect.x, -rect.y);

const imgBytes = ctx.bitmap.saveAsPng();
Util.saveFile("svgToBitmap.png", imgBytes, 'image/png');
```

![svgToBitmap.png](https://cdn.mescius.io/document-site-files/images/706ce9b9-7836-44f3-b62b-7d4408387ae3/svgToBitmap-20260430.45b03f.png?width=600)
You can also render compressed SVG files (.svgz) by loading them using [SvgDocument.loadGz](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgDocument#loadgz).

```javascript
const svgDoc = SvgDocument.loadGz(await Util.loadFile("images/Ducky.svgz"));
const ctx = new BmpContext(600, 600, 1, "MintCream");
ctx.drawSvg(svgDoc, 0, 0);

const imgBytes = ctx.bitmap.saveAsPng();
Util.saveFile("drawSvgZ.png", imgBytes, 'image/png');
```

![drawSvgZ.png](https://cdn.mescius.io/document-site-files/images/706ce9b9-7836-44f3-b62b-7d4408387ae3/drawSvgZ-20260501.ea67a0.png?width=450)

### Render Graphics and Text to SVG

In addition to rendering existing SVG files, DsPdfJS allows you to generate SVG documents programmatically by drawing graphics and text using the SvgContext class. The SvgContext records drawing operations and converts them into an SvgDocument, which can then be saved or processed further.
Graphics elements such as shapes, gradients, and transformations can be drawn using the same drawing APIs used for bitmap contexts. Text can be rendered either as SVG text elements or as vector paths. This behavior is controlled by the [drawTextAsPath](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgContext#drawtextaspath) property of the drawing context.
To render graphics and text to an SVG document:

1. Create an instance of the SvgContext class to define the SVG drawing surface.
2. Draw graphics and text using drawing methods such as [drawRect](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgContext#drawrect), [drawEllipse](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgContext#drawellipse), or [drawLayout](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgContext#drawlayout).
3. Optionally control text rendering behavior using the [drawTextAsPath ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgContext#drawtextaspath)property.
4. Convert the recorded drawing operations into an SvgDocument using the [toSvgDocument ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgContext#tosvgdocument)method.
5. Save the resulting SVG document.

```javascript
const segoeFont = Font.load(await Util.loadFile("fonts/segoeuib.ttf"));

const ctx = new SvgContext(800, 600, {
    shapeRendering: ShapeRendering.CrispEdges
});

const lgBr = new LinearGradientBrush({
    startColor: "Turquoise",
    startPoint: { x: 0, y: 0 },
    gradientStops: [{ color: "MediumVioletRed", offset: 0.5 }],
    endColor: "SeaGreen",
    endPoint: { x: 1, y: 0 }
});

ctx.drawRect(5, 5, 790, 590, {
    radius: 20,
    lineColor: "DarkSlateBlue",
    lineWidth: 6,
    lineStyle: DashStyle.DashDot,
    lineCap: PenLineCap.Square,
    fillBrush: lgBr
});

ctx.translate(100, 50);
ctx.rotate(30);
ctx.drawText({
    text: "Hello World!",
    font: segoeFont,
    bold: true,
    foreColor: "Yellow",
    fontSize: 70
}, 0, 0);

ctx.pushGroup("id", "myEllipse");

ctx.resetTransform();
ctx.translate(400, 400);
ctx.rotate(-30);
const hb = new HatchBrush({
    style: HatchStyle.Backslashes,
    foreColor: "White"
});
ctx.drawEllipse({ width: 300, height: 200 }, {
    fillBrush: hb,
    lineColor: "Red",
    lineWidth: 3
});

ctx.popGroup();

ctx.title = "Simple SVG";

const svgDoc = ctx.toSvgDocument();
Util.saveFile("createSvg.svg", svgDoc.save(), 'image/svg+xml');
```

![createSvg.svg](https://cdn.mescius.io/document-site-files/images/706ce9b9-7836-44f3-b62b-7d4408387ae3/createSvg-20260501.5cf5be.svg?width=650)
The following example demonstrates how text can be rendered as SVG text elements or converted to vector paths.

```javascript
const ctx = new SvgContext(900, 500);

const fmt = new Format({
    font: Font.load(await Util.loadFile("fonts/segoeui.ttf")),
    fontSize: 67,
    foreColor: "Green"
});

const tl = new Layout({
    maxWidth: 300,
    runs: [{
        text: "The quick brown fox jumps over the lazy dog.",
        format: fmt
    }]
});

// Render text as SVG text elements
ctx.drawTextAsPath = false;
ctx.drawLayout(tl, 100, 10);

// Render text as vector paths
ctx.drawTextAsPath = true;
ctx.drawLayout(tl, 500, 10);

const svgDoc = ctx.toSvgDocument();
Util.saveFile("textToSvg.svg", svgDoc.save(), 'image/svg+xml');
```

![textToSvg.svg](https://cdn.mescius.io/document-site-files/images/706ce9b9-7836-44f3-b62b-7d4408387ae3/textToSvg-20260501.cfcf4f.svg?width=700)

### Render PDF Page as SVG

DsPdfJS allows you to export a PDF page to the SVG format. SVG is a vector format, so the resulting image can be scaled without loss of quality.
The [PdfPage ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/PdfPage)class provides several methods for exporting a page as SVG:

* [saveAsSvg ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/PdfPage#saveassvg)– saves the page as SVG data in a byte array.
* [saveAsSvgDocument ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/PdfPage#saveassvgdocument)– returns an SvgDocument instance that can be inspected or modified before saving.
* [saveAsSvgGz ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/PdfPage#saveassvggz)– saves the page as compressed SVG data (.svgz).

To render a PDF page as SVG:

1. Load the PDF document using PdfDocument.load.
2. Access the desired page from the document’s pages collection.
3. Call the [saveAsSvg ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/PdfPage#saveassvg)method to generate the SVG data.
4. Save the resulting byte array to a file.

The following code example shows how to export a PDF page as an SVG file.

```javascript
const doc = PdfDocument.load(await Util.loadFile("Wetlands.pdf"));
const page = doc.pages.getAt(0);

const svgBytes = page.saveAsSvg();
Util.saveFile("pdfPageToSvg.svg", svgBytes, "image/svg+xml");
```

### Save SVG to Byte Array

When working with SVG documents generated using SvgContext, you can save the resulting SvgDocument as a byte array. This allows you to store the SVG file, send it over a network, or process it further.
The SvgDocument class provides the following methods:

* [save ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgDocument#save)– saves the SVG document to a UTF‑8 encoded byte array.
* [saveGz ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgDocument#savegz)– saves the SVG document as compressed SVG (.svgz) data.

To save an SVG document to a byte array:

1. Create or obtain an [SvgDocument](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgDocument).
2. Call the [save ](https://developer.mescius.com/document-solutions/javascript-pdf-api/api/classes/SvgDocument#save)method to generate the SVG data.
3. Save or process the returned byte array.

The following code example shows how to save an SVG document to a byte array.

```javascript
const ctx = new SvgContext(400, 200);

ctx.drawRect(20, 20, 360, 160, {
    lineColor: "DarkBlue",
    lineWidth: 4,
    fillColor: "LightSkyBlue"
});

const svgDoc = ctx.toSvgDocument();

const svgBytes = svgDoc.save();
Util.saveFile("saveSvg.svg", svgBytes, "image/svg+xml");
```