Turning web pages into high‑resolution images is a common requirement for reporting, thumbnail creation, and email rendering. The Aspose.CAD Cloud SDK for Node.js offers a robust solution that simplifies converting HTML to JPG directly on your server. This guide walks you through installing the SDK, configuring credentials, writing the conversion code, and using the REST API with cURL, so you can embed HTML‑to‑JPG conversion into any Node.JS application.
Steps to Convert HTML to JPG in Node.JS
- Configure Aspose.CAD Cloud credentials: Create a configuration object and set your client ID and secret.
const config = new CadApi.Configuration();
config.clientId = 'YOUR_CLIENT_ID';
config.clientSecret = 'YOUR_CLIENT_SECRET';
- Create the CAD API instance: Use the configuration to instantiate the API client.
const cadApi = new CadApi.CadApi(config);
- Define input HTML and output JPG paths: Resolve absolute paths for the source HTML file and the target JPG file.
const inputPath = path.resolve(__dirname, 'sample.html');
const outputPath = path.resolve(__dirname, 'sample.jpg');
- Prepare the conversion request: Attach a readable stream of the HTML file and specify the output format as
jpg.
const request = new CadApi.ConvertRequest();
request.inputFile = fs.createReadStream(inputPath);
request.format = 'jpg';
- Execute the conversion and save the JPG: Call
convert, receive a Buffer, and write it to disk. This completes the process to convert HTML to JPG in Node.JS.
try {
const resultBuffer = await cadApi.convert(request);
fs.writeFileSync(outputPath, resultBuffer);
console.log('Conversion completed. JPG saved to:', outputPath);
} catch (err) {
console.error('Error during conversion:', err);
}
For detailed class information, refer to the API Reference.
Full Working Example for HTML to JPG Conversion
The following example demonstrates the complete workflow from start to finish.
const fs = require('fs');
const path = require('path');
const CadApi = require('asposecadcloud');
async function convertHtmlToJpg() {
// Configure Aspose.CAD Cloud credentials
const config = new CadApi.Configuration();
config.clientId = 'YOUR_CLIENT_ID';
config.clientSecret = 'YOUR_CLIENT_SECRET';
// Create API instance
const cadApi = new CadApi.CadApi(config);
// Define input HTML and output JPG paths
const inputPath = path.resolve(__dirname, 'sample.html');
const outputPath = path.resolve(__dirname, 'sample.jpg');
// Prepare request with a readable stream and target format
const request = new CadApi.ConvertRequest();
request.inputFile = fs.createReadStream(inputPath);
request.format = 'jpg';
try {
// Perform conversion; result is a Buffer containing JPG data
const resultBuffer = await cadApi.convert(request);
// Write the JPG buffer to the output file
fs.writeFileSync(outputPath, resultBuffer);
console.log('Conversion completed. JPG saved to:', outputPath);
} catch (err) {
console.error('Error during conversion:', err);
}
}
// Execute the conversion
convertHtmlToJpg();
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, 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.
Generate JPG from HTML Using cURL
You can achieve the same conversion via the REST API. Below are the required cURL commands.
- Obtain an access token
ReplaceYOUR_CLIENT_IDandYOUR_CLIENT_SECRETwith your credentials.
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- Upload the source HTML file
Use the access token from the previous step.
curl -X PUT "https://api.aspose.cloud/v3.0/cad/storage/file/sample.html" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: text/html" \
--data-binary "@sample.html"
- Request conversion to JPG
Theformatquery parameter specifies the target format.
curl -X POST "https://api.aspose.cloud/v3.0/cad/convert?format=jpg" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@sample.html" \
-o sample.jpg
- Download the resulting JPG (if you used a different endpoint for conversion).
curl -X GET "https://api.aspose.cloud/v3.0/cad/storage/file/sample.jpg" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o sample.jpg
For more details, see the official API documentation.
Installing and Configuring Aspose.CAD Cloud SDK for Node.JS
Install the SDK via npm:
npm install @asposecloud/aspose-cad-cloud
The package is available for download at the Release page.
Make sure you have Node.js 12 or higher installed and that you have created an Aspose Cloud account to obtain your clientId and clientSecret.
Aspose.CAD Cloud SDK Capabilities for Image Generation
- HTML to JPG conversion - Directly render HTML pages as high‑quality JPG images without intermediate steps.
- Stream‑based processing - Accepts input as a readable stream, which is ideal for large files or when reading from memory.
- Multiple output formats - Besides JPG, the SDK supports PNG, BMP, GIF, and TIFF, allowing flexible image generation.
- Cloud‑hosted rendering engine - Leverages Aspose’s cloud infrastructure for fast and reliable conversions.
- Comprehensive API reference - The online API Reference provides full method signatures, required and optional parameters, authentication flow, request/response schemas, and error codes, plus dozens of language‑specific code snippets (C#, Java, Python, Node.js) that illustrate common tasks such as CAD‑to‑JPG conversion, image quality tuning, and streaming input handling. See the API Reference.
Conclusion
You now know how to convert HTML to JPG in Node.JS using the powerful Aspose.CAD Cloud SDK for Node.js. The SDK handles credential management, stream processing, and format selection, giving you a ready‑to‑run solution for server‑side image generation. Remember to obtain a temporary license from the temporary license page for testing, and purchase a full license for production deployments. Happy coding!
FAQs
How can I convert HTML to JPG in Node.JS using Aspose.CAD?
Use the Aspose.CAD Cloud SDK for Node.js to create a ConvertRequest, set format to 'jpg', and call the convert method. The full code example in this article shows the exact steps.
Is the conversion performed locally or in the cloud?
The SDK sends the request to Aspose.CAD Cloud services, so the processing happens in the cloud while your Node.JS code orchestrates the workflow.
What file formats are supported for input and output?
The SDK supports a wide range of CAD and image formats. For HTML to image conversion, you can output JPG, PNG, BMP, GIF, or TIFF. See the API Reference for the complete list.
Do I need to purchase a license for production use?
A temporary license is available for evaluation at the temporary license page. For production, you must acquire a commercial license from Aspose.