Skip to content

Instantly share code, notes, and snippets.

Embed
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;
import java.io.*;
public class conversionCode {
public static void main(String[] args) {
com.aspose.html.Configuration.setAPP_SID("c235e685-1aab-4cda-a95b-54afd63eb87f");
com.aspose.html.Configuration.setAPI_KEY("b8da4ee37494f2ef8da3c727f3a0acb9");
com.aspose.html.Configuration.setBasePath("https://api.aspose.cloud/v3.0");
com.aspose.html.Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
com.aspose.html.Configuration.setUserAgent("WebKit");
com.aspose.html.Configuration.setDebug(true);
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);
if(!f.exists()){
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...");
e.printStackTrace();
}
} // 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 = inputStream.read(fileReader);
if (read == -1) break;
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
}
outputStream.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} // saveToDisc ends here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment