Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This gist contains the code snippets related to Convert HTML to PDF using C# .NET API using Aspose.PDF for .NET

The Gist contains code snippets related to Aspose.HTML Cloud SDK for Java. For more information on usage of these code snippets, please visit the following links

This gist contains the code snippets related to Convert HTML to PDF using Aspose.HTML Cloud SDK for Java API.
import com.aspose.html.api.ConversionApi;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Call;
public class conversionCode {
public static void main(String[] args) {
String name = "Simple.html";// inpit Document name.
Integer width = 800; // Resulting image width.
Integer height = 1000; // Resulting image height.
Integer leftMargin = 10; // Left resulting image margin.
Integer rightMargin = 10; // Right resulting image margin.
Integer topMargin = 10; // Top resulting image margin.
Integer bottomMargin = 10; // Bottom resulting image margin.
String storage = null; // Name of the storage.
File f = new File("/Users/nayyershahbaz/Documents/"+name);
System.out.println("file not found");
RequestBody requestBody = RequestBody.create( MediaType.parse("multipart/form-data"), f);
MultipartBody.Part file = MultipartBody.Part.createFormData("file", f.getName(), requestBody);
try {
ConversionApi api = new com.aspose.html.ApiClient().createService(ConversionApi.class);
com.aspose.html.api.StorageApi storageApi = new com.aspose.html.ApiClient().createService(com.aspose.html.api.StorageApi.class);
Call<ResponseBody> call2 = api.PostConvertDocumentInRequestToPdf("resultantFile.pdf", file, width, height, leftMargin, rightMargin, topMargin, bottomMargin);
retrofit2.Response<ResponseBody> res = call2.execute();
ResponseBody resultant = res.body();
call2 = storageApi.downloadFile("resultantFile.pdf", null, storage);
checkAndSave(call2, "resultantFile.pdf");
} catch (Exception e) {
System.err.println("Exception during file processing...");
} // main ends here
public static void checkAndSave(Call<ResponseBody> call, String fileName) throws IOException
retrofit2.Response<ResponseBody> res = call.execute();
ResponseBody answer = res.body();
//Save to test directory
boolean result = saveToDisc(answer, fileName);
public static boolean saveToDisc(ResponseBody body, String fileName)
File savedFile = new File("/Users/nayyershahbaz/Documents/"+fileName);
try (InputStream inputStream = body.byteStream();
OutputStream outputStream = new FileOutputStream(savedFile))
byte[] fileReader = new byte[4096];
long fileSizeDownloaded = 0;
while (true) {
int read =;
if (read == -1) break;
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
return true;
} catch (Exception e) {
return false;
} // saveToDisc ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment