Converting vector graphics to raster images is a frequent requirement when generating thumbnails, previews, or PDFs on the server. GroupDocs.Conversion Cloud SDK for Node.JS provides a robust library that handles SVG to PNG conversion entirely in the cloud. In this guide you will see a complete working example, a matching cURL workflow, configuration tips, and best‑practice recommendations. By the end you’ll be able to integrate server‑side SVG to PNG conversion into any Node.JS application.

Full Working Example for SVG to PNG Conversion in Node.JS

This example demonstrates how to convert an SVG file stored in GroupDocs Cloud storage to a PNG image using the GroupDocs.Conversion Cloud SDK for Node.JS.

const { Configuration, ConvertApi, ConvertDocumentRequest, ConvertSettings, PngOptions, StorageApi, DownloadFileRequest } = require("@groupdocs/conversion-cloud");
const fs = require('fs');
const path = require('path');

// -----------------------------------------------------------------------------
// Configuration – replace with your own credentials
// -----------------------------------------------------------------------------
const config = new Configuration({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
});

const convertApi = new ConvertApi(config);
const storageApi = new StorageApi(config);

// -----------------------------------------------------------------------------
// Define source SVG and target PNG locations inside GroupDocs Cloud storage
// -----------------------------------------------------------------------------
const inputSvgPath = "input/sample.svg";          // SVG stored in cloud storage
const outputPngPath = "output/sample.png";       // Desired PNG location

// -----------------------------------------------------------------------------
// PNG conversion options – set dimensions to control memory/CPU usage
// -----------------------------------------------------------------------------
const pngOptions = new PngOptions({
    width: 1024,               // Resize width (optional)
    height: 768,               // Resize height (optional)
    backgroundColor: "#FFFFFF" // Ensure opaque background
});

// -----------------------------------------------------------------------------
// Build conversion settings object
// -----------------------------------------------------------------------------
const convertSettings = new ConvertSettings({
    filePath: inputSvgPath,
    format: "png",
    outputPath: outputPngPath,
    options: pngOptions
});

const convertRequest = new ConvertDocumentRequest(convertSettings);

// -----------------------------------------------------------------------------
// Execute conversion, then download the PNG locally
// -----------------------------------------------------------------------------
convertApi.convertDocument(convertRequest)
    .then(convResult => {
        console.log("Conversion succeeded. Cloud file:", convResult.path);
        const downloadReq = new DownloadFileRequest({ path: convResult.path });
        return storageApi.downloadFile(downloadReq);
    })
    .then(fileBuffer => {
        const localFile = path.resolve(__dirname, "sample.png");
        fs.writeFileSync(localFile, fileBuffer);
        console.log("PNG downloaded to:", localFile);
    })
    .catch(err => {
        console.error("Error during SVG → PNG conversion:", err);
    })
    .finally(() => {
        // Clean up HTTP client resources if the SDK exposes a close method
        if (config && config.httpClient && typeof config.httpClient.close === "function") {
            config.httpClient.close();
        }
    });

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (input/sample.svg, output/sample.png, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

SVG to PNG Conversion via REST API Using cURL

If you prefer a pure REST approach, the same conversion can be performed with cURL commands. The steps below show how to obtain an access token, upload the SVG, start the conversion, and download the PNG.

Authenticate and Get Access Token

curl -X POST "https://api.groupdocs.cloud/v2.0/auth/token" \
     -H "Content-Type: application/json" \
     -d '{
           "client_id": "YOUR_CLIENT_ID",
           "client_secret": "YOUR_CLIENT_SECRET"
         }'

The response contains an access_token that you will use in subsequent calls.

Upload the Source SVG File

curl -X POST "https://api.groupdocs.cloud/v2.0/storage/upload?path=input/sample.svg" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -F "file=@/path/to/local/sample.svg"

Execute the SVG to PNG Conversion

curl -X POST "https://api.groupdocs.cloud/v2.0/conversion/convert" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "filePath": "input/sample.svg",
           "format": "png",
           "outputPath": "output/sample.png",
           "options": {
               "width": 1024,
               "height": 768,
               "backgroundColor": "#FFFFFF"
           }
         }'

The API returns the path of the newly created PNG file in cloud storage.

Download the Resulting PNG File

curl -X GET "https://api.groupdocs.cloud/v2.0/storage/download?path=output/sample.png" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o sample.png

These cURL commands give you full control over the conversion process without writing any code. For more details, see the official API documentation.

How SVG to PNG Conversion in Node.JS Works

  1. Import SDK Classes - The require("@groupdocs/conversion-cloud") statement loads essential classes such as ConvertApi, ConvertSettings, and PngOptions (API reference).

    const { ConvertApi, ConvertSettings, PngOptions } = require("@groupdocs/conversion-cloud");
    
  2. Configure Authentication - A Configuration object holds your clientId and clientSecret. This object is passed to both ConvertApi and StorageApi to authorize all subsequent calls.

    const config = new Configuration({ clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET" });
    
  3. Define Input and Output Paths - inputSvgPath points to the SVG in cloud storage, while outputPngPath specifies where the PNG should be saved.

    const inputSvgPath = "input/sample.svg";
    const outputPngPath = "output/sample.png";
    
  4. Set PNG Options - PngOptions lets you control dimensions and background color, which is useful for large or transparent SVGs.

    const pngOptions = new PngOptions({ width: 1024, height: 768, backgroundColor: "#FFFFFF" });
    
  5. Create Conversion Settings and Execute - ConvertSettings bundles the file path, target format, output location, and options. The convertDocument method performs the conversion, and the result contains the cloud path of the PNG.

    const convertSettings = new ConvertSettings({ filePath: inputSvgPath, format: "png", outputPath: outputPngPath, options: pngOptions });
    const convertRequest = new ConvertDocumentRequest(convertSettings);
    convertApi.convertDocument(convertRequest);
    
  6. Download the PNG - After conversion, StorageApi.downloadFile retrieves the binary data, which you can write to the local filesystem.

Understanding each step helps you customize the process, handle errors, and integrate the conversion into larger workflows.

Installing and Configuring GroupDocs.Conversion Cloud SDK for Node.JS

npm install groupdocs-conversion-cloud

The SDK requires Node.js 12 or later. After installing, create a configuration object with your clientId and clientSecret (available from your GroupDocs Cloud account). See the download page for the latest package version.

const { Configuration } = require("@groupdocs/conversion-cloud");
const config = new Configuration({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
});

Make sure the credentials have permission to access storage and conversion services. Once configured, you can instantiate ConvertApi and StorageApi as shown in the code example.

Fine-Tuning PNG Conversion Settings

The PngOptions class offers several properties you can adjust:

  • width / height - Define the output resolution. Larger values increase quality but consume more memory.
    const pngOptions = new PngOptions({ width: 1920, height: 1080 });
    
  • backgroundColor - Set a solid background for SVGs that contain transparency.
    const pngOptions = new PngOptions({ backgroundColor: "#000000" });
    
  • compressionLevel - (If supported) Choose a compression level between 0 (no compression) and 9 (maximum compression) to reduce file size.
    const pngOptions = new PngOptions({ compressionLevel: 6 });
    

These options are passed through the options property of ConvertSettings. Refer to the API reference for the full list of available settings.

Best Practices for Efficient SVG to PNG Conversion

  • Validate SVG Content - Ensure the SVG does not contain external scripts or malicious payloads before uploading. GroupDocs.Conversion sanitizes input, but pre‑validation adds an extra security layer.
  • Reuse the Configuration Object - Create a single Configuration instance and share it across multiple ConvertApi calls to reduce overhead.
  • Set Reasonable Dimensions - Limit width and height to the size you actually need. Oversized images increase processing time and memory usage.
  • Stream Large Files - For very large SVGs, consider streaming the download instead of loading the entire buffer into memory.
  • Monitor API Usage - Keep an eye on your conversion quota and handle rate‑limit responses gracefully.

Conclusion

GroupDocs.Conversion Cloud SDK for Node.JS makes server‑side SVG to PNG conversion straightforward, scalable, and secure. By following the complete code example, the equivalent cURL workflow, and the configuration tips above, you can integrate high‑performance image conversion into any backend service. Remember to review the licensing options production deployments require a paid subscription, and you can obtain a temporary license for testing from the temporary license page. Start converting today and deliver crisp PNG assets to your users with confidence.

FAQs

How do I perform SVG to PNG conversion in Node.JS using GroupDocs?
Use the GroupDocs.Conversion Cloud SDK for Node.JS. Upload the SVG to cloud storage, configure PngOptions, call convertDocument, and download the PNG. The full code example in this article shows each step.

Can I batch convert many SVG files at once?
Yes. Iterate over a list of SVG paths and invoke the same conversion request for each file. The SDK processes each request independently, allowing you to store all PNG results in a single output folder.

What should I do if the conversion fails with an error?
Check the error message returned in the catch block. Common issues include invalid credentials, missing file paths, or unsupported SVG features. Consult the official documentation for detailed error codes and troubleshooting steps.

Where can I find more information about PNG options?
All configurable properties are listed in the API reference for PngOptions. Visit the API reference to explore additional settings such as compression level and color depth.

Read More