@@ -70,127 +70,44 @@ Add this dependency to your project's POM:
7070```
7171
7272
73- ### Sample usage
73+ ## HTML to JPG in Java
74+
7475``` java
75- package com.aspose.test_package ;
76-
77- import java.io.File ;
78- import java.io.FileOutputStream ;
79- import java.io.IOException ;
80- import java.io.InputStream ;
81- import java.io.OutputStream ;
82- import com.aspose.html.ApiClient ;
83- import com.aspose.html.Configuration ;
84- import com.aspose.html.api.ConversionApi ;
85- import com.aspose.html.api.StorageApi ;
86- import com.aspose.html.model.FilesUploadResult ;
87- import okhttp3.MediaType ;
88- import okhttp3.MultipartBody ;
89- import okhttp3.RequestBody ;
90- import okhttp3.ResponseBody ;
91- import retrofit2.Call ;
92- import retrofit2.Response ;
93-
94- public class App {
95-
96- // Helper method save the response body to the destination directory
97- public static long saveToDisc (ResponseBody body , String fileName ) {
98-
99- File savedFile = new File (Configuration . getTestDstDir() + File . separator + fileName);
100- long fileSizeDownloaded = 0 ;
101-
102- try (InputStream inputStream = body. byteStream();
103- OutputStream outputStream = new FileOutputStream (savedFile))
104- {
105- byte [] fileReader = new byte [4096 ];
106-
107- while (true ) {
108- int read = inputStream. read(fileReader);
109- if (read == - 1 ) break ;
110-
111- outputStream. write(fileReader, 0 , read);
112- fileSizeDownloaded += read;
113- }
114- outputStream. flush();
115- } catch (Exception e) {
116- e. printStackTrace();
117- }
118- return fileSizeDownloaded;
119- }
120-
121-
122-
123- public static void main (String [] args ) {
124-
125- // Get keys from aspose site.
126- // There is free quota available.
127- // For more details, see https://purchase.aspose.cloud/pricing
128-
129- Configuration . setAPI_KEY(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" );
130- Configuration . setAPP_SID(" XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" );
131- Configuration . setBasePath(" https://api.aspose.cloud/v3.0" );
132- Configuration . setAuthPath(" https://api.aspose.cloud/connect/token" );
133- Configuration . setUserAgent(" WebKit" );
134- Configuration . setDebug(true );
135- Configuration . setTestSrcDir(" testdata" );
136- Configuration . setTestDstDir(" testresult" );
137-
138- String name = " test.html" ;// Document name. Place the html document in the folder "testdata".
139- String outFormat = " jpg" ; // Convert to jpg
140- Integer width = 800 ; // Resulting image width.
141- Integer height = 1000 ; // Resulting image height.
142- Integer leftMargin = 10 ; // Left resulting image margin.
143- Integer rightMargin = 10 ; // Right resulting image margin.
144- Integer topMargin = 10 ; // Top resulting image margin.
145- Integer bottomMargin = 10 ; // Bottom resulting image margin.
146- Integer resolution = 300 ; // Resolution of resulting image.
147- String folder = " /" ; // The folder in the storage. Should exist.
148- String storage = null ; // Name of the storage. null
149-
150- // Creating API for need operations
151- ConversionApi conversionApi = new ApiClient (). createService(ConversionApi . class);
152- StorageApi storageApi = new ApiClient (). createService(StorageApi . class);
153-
154-
155- try {
156-
157- // Upload file to storage
158- // Test file in the "/testdata" folder in the root of the project
159- File f = new File (Configuration . getTestSrcDir(), name);
160-
161- if (! f. exists()) {
162- System . out. println(" file not found" );
163- throw new RuntimeException (" Test file not found" );
164- }
165-
166- RequestBody requestBody = RequestBody . create( MediaType . parse(" multipart/form-data" ), f);
167- MultipartBody . Part fileToUpload = MultipartBody . Part . createFormData(" file" , f. getName(), requestBody);
168-
169- // Upload document to storage
170- Call<FilesUploadResult > callUpload = storageApi. uploadFile(folder + File . separator + name, fileToUpload, null );
171- Response<FilesUploadResult > res = callUpload. execute();
172- System . out. println(" Executed is successful = " + res. isSuccessful());
173-
174- // Prepare call execute
175- Call<ResponseBody > call = conversionApi. GetConvertDocumentToImage (name, outFormat, width, height, leftMargin, rightMargin, topMargin, bottomMargin, resolution, folder, storage);
176-
177- // Execute request
178- Response<ResponseBody > img = call. execute();
179-
180- // Get body from response
181- ResponseBody answer = img. body();
182-
183- // Save to test directory
184- long result = saveToDisc(answer, " test.zip" );
185- System . out. println(" Result size = " + result);
186-
187- } catch (IOException e) {
188- e. printStackTrace();
189- }
190- }
191- }
76+ // Get your ClientId and ClientSecret from https://dashboard.aspose.cloud (free registration required).
77+
78+ Configuration . setAPP_SID(" MY_CLIENT_ID" );
79+ Configuration . setAPI_KEY(" MY_CLIENT_SECRET" );
80+ Configuration . setBasePath(" https://api.aspose.cloud/v3.0" );
81+ Configuration . setAuthPath(" https://api.aspose.cloud/connect/token" );
82+ Configuration . setUserAgent(" WebKit" );
83+ Configuration . setDebug(true );
84+ Configuration . setTestSrcDir(" My_Source_Folder" );
85+ Configuration . setTestDstDir(" My_Output_Folder" );
86+
87+ ConversionApi conversionApi = new ApiClient (). createService(ConversionApi . class);
88+
89+ String name = " test.html" ;// Document name. Place the html document in the folder "testdata".
90+ String outFormat = " jpg" ; // Convert to jpg
91+
92+ Integer width = 800 ; // Resulting image width.
93+ Integer height = 1000 ; // Resulting image height.
94+ Integer leftMargin = 10 ; // Left resulting image margin.
95+ Integer rightMargin = 10 ; // Right resulting image margin.
96+ Integer topMargin = 10 ; // Top resulting image margin.
97+ Integer bottomMargin = 10 ; // Bottom resulting image margin.
98+ Integer resolution = 300 ; // Resolution of resulting image.
99+ String folder = " /" ; // The folder in the storage. Should exist.
100+ String storage = " My_Storage_Name" ; // Name of the storage. null
101+
102+ // Prepare call execute
103+ Call<ResponseBody > call = conversionApi. GetConvertDocumentToImage (name, outFormat, width, height, leftMargin, rightMargin, topMargin, bottomMargin, resolution, folder, storage);
104+
105+ // Execute request
106+ Response<ResponseBody > img = call. execute();
107+
108+ // Get body from response
109+ ResponseBody answer = img. body();
192110```
193-
194111# Documentation for API Endpoints
195112All URIs are relative to * https://api.aspose.cloud/v3.0 *
196113
0 commit comments