HTML to JPG

Convert HTML to JPEG in C# .NET

HTML is perfect for displaying content on the web, but it isn’t always ideal for sharing, embedding, or preserving content in a visual format. Therefore, by converting HTML to JPG not only simplifies the distribution of web content but also enhances its accessibility and usability across various platforms. This article provides the details on generating static image representations of dynamic web pages using .NET REST API.

HTML to JPG Conversion API

Aspose.HTML Cloud SDK for .NET provides a powerful and flexible solution for converting HTML content into high-quality JPG images. You can perform this conversion with minimal effort, ensuring that your HTML content is accurately rendered and converted into a visually appealing JPG image. The first step in the conversion process is to add the REST API reference in .NET project. Please search Aspose.HTML-Cloud in NuGet packages manager and click the Add Package button. Secondly, create a free account over Aspose.Cloud dashboard and obtain your Client Credentials details.

How to Convert HTML to JPG in C# .NET

Let’s delve into the details of a powerful and flexible solution for converting HTML to JPG images using C# .NET.

var storageApi = new HtmlApi(CLIENT_ID, CLIENT_SECRET).StorageApi;

Create an instance of StorageApi while passing client credentials as arguments.

var exists = await storageApi.UploadFileAsync(@"C:\Users\"+ inputFileName, inputFileName, "Internal");

Upload input HTML to cloud storage.

var api = new HtmlApi(clientID, clientSecret).ConvertApi;

Now create an instance of HtmlApi where we pass input credentials as arguments.

var options = new ImageConversionOptions();

We can optionally define the margin details for the resultant JPEG image.

var result = await api.ConvertAsync(inputFileName, resultantFileName, options);

Finally, call the API to convert HTML to JPG image and upload the resultant image to cloud storage.

Convert Web to JPG using cURL Commands

Aspose.HTML Cloud APIs can also be accessed via cURL commands using command line terminal. So as a prerequisite for this approach, first we need to generate a JSON Web Token (JWT) based on your individual client credentials. Please execute the following command to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now that the JWT token has been generated, we need to execute the following command to convert HTML to JPG image.

curl -v "https://api.aspose.cloud/html/{sourceFile}/convert/image/jpg?outPath={convertedImage}&storage=Internal" \
-X PUT \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"

Replace sourceFile with the name of input HTML file uploaded to cloud storage, and convertedImage with the name of resultant JPG image.

Conclusion

We have learned that the ease of use, high-quality output, and scalability offered by our Cloud SDK makes it an indispensable asset for any development workflow. Start exploring the Aspose.HTML Cloud SDK today and take advantage of its powerful features to streamline your HTML to JPG conversion tasks.

We also recommend visiting the following blogs for further details regarding: