svg to png

Convert SVG to PNG in Java

One of the prominent feature of SVG images is that they are scalable. The SVGs retain the same quality no matter what screen resolution or size they are being at. So you can design it at any resolution, and it will size up or down without damaging the quality or becoming pixelated. When comparing to raster images, you need to know what size you want from the beginning, or you could risk making the image too large or too small. But, since SVGs are based on points and paths instead of pixels, they can’t display as much detail as standard image formats. So if we convert SVG to PNG format, these issues can be resolved.

SVG to PNG Conversion API

In this article, we are going to use Aspose.Imaging Cloud SDK for Java which is REST based API offering the capabilities to programmatically edit, manipulate and transform raster, metafiles, photoshop to variety of Supported Formats. It also supports the feature to convert SVG to PNG online. Now in order to get started with SDK utilization, we need to add its reference in our java project by including following information in pom.xml (maven build type project).

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-imaging-cloud</artifactId>
        <version>22.4</version>
    </dependency>
</dependencies>

If we have already registered on Aspose Cloud Dashboard, please obtain your client credentials from Cloud Dashboard. Else, you need to first register a free account while using a valid email address.

Convert SVG to PNG in Java

The Image processing API is capable of converting PNG to SVG as well as SVG to PNG using Java code snippet.

  • First of all, create an object of ImagingApi using personalized client credentials
  • Secondly, read the content of SVG image using readAllBytes(…) method and pass to byte[] array
  • Then create an instance of UploadFileRequest while passing SVG name and upload to cloud storage using uploadFile(…) method
  • Now create an object of ConvertImageRequest where we specify input SNG and resultant image format as PNG
  • Call the method convertImage(…) to convert SVG to PNG format and result is returned as response stream
  • Finally, save the resultant PNG to local drive using FileOutputStream object

The input SVG image used in above example can be downloaded from File_Extension Icons.svg.

svg to png

Image:- convert svg to png online Preview

Save SVG as PNG using cURL Commands

The Aspose.Imaging Cloud is developed according to REST architecture, so we can save image as png using cURL commands. Now the first step is to generate a JWT access token (based on client credentials) using following command.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT token is generation, please execute the following command to convert SVG to PNG format.

curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/input.svg/convert?format=PNG" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-o Resultant.png

Conclusion

We have learned the details on how we can convert SVG to PNG using Java code snippet. At the same time, we have explored an option of converting SVG to PNG using cURL commands. Furthermore, if you want to modify the source code of SDK, you can download if from GitHub (all our Cloud SDKs are developed under MIT license). Also, one of the option of testing the promising features of API is via SwaggerUI within web browser.

Nevertheless, the Product Documentation is an amazing source of information to learn about other exciting features offered by the API Lastly, in case you encoutner any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.

Please visit the following links to learn more about: