Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/26534f6c36117a13e5f91160aeaeff5b to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/26534f6c36117a13e5f91160aeaeff5b to your computer and use it in GitHub Desktop.
Convert HTML to PDF in Java using REST API
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// Java convert HTML to PDF using additional options.
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.html");
settings.setFormat("pdf");
PdfConvertOptions convertOptions = new PdfConvertOptions();
convertOptions.setFromPage(1);
convertOptions.setPagesCount(1);
convertOptions.setZoom(50);
convertOptions.setDpi(124.0);
convertOptions.setCenterWindow(true);
convertOptions.setCompressImages(false);
convertOptions.setDisplayDocTitle(true);
convertOptions.setFitWindow(false);
convertOptions.setGrayscale(false);
convertOptions.setImageQuality(100);
convertOptions.setLinearize(false);
convertOptions.setMarginTop(5);
convertOptions.setMarginLeft(5);
convertOptions.setPassword("password");
convertOptions.setUnembedFonts(true);
convertOptions.setRemoveUnusedStreams(true);
convertOptions.setRemoveUnusedObjects(true);
convertOptions.setRemovePdfaCompliance(false);
convertOptions.setHeight(1024);
settings.setConvertOptions(convertOptions);
settings.setOutputPath("java-testing/output-sample-file.pdf");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document conveted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// How to convert HTML File to PDF in Java.
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.html");
settings.setFormat("pdf");
settings.setOutputPath("java-testing/output-sample-file.pdf");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document converted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Download File from the Cloud Storage in Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
DownloadFileRequest request = new DownloadFileRequest("java-testing\\output-sample-file.pdf", MyStorage, null);
File response = apiInstance.downloadFile(request);
System.err.println("Expected response type is File: " + response.length());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}

You can generate PDF from HTML online programmatically on the cloud. In this article, you will learn how to convert HTML to PDF in Java using REST API.

The following topics shall be covered in this article:

  1. Java HTML to PDF Converter REST API – Java SDK Installation
  2. Convert HTML Document to PDF File in Java using REST API
  3. Convert HTML to PDF Online in Java using Advanced Options

Read the complete article on how to convert HTML to PDF in Java using REST API: https://blog.groupdocs.cloud/2022/11/18/convert-html-to-pdf-in-java-using-rest-api/

package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Upload File to Cloud Storage using Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
File fileStream = new File("H:\\groupdocs-cloud-data\\input-sample-file.html");
UploadFileRequest request = new UploadFileRequest("java-testing\\input-sample-file.html", fileStream, MyStorage);
FilesUploadResult response = apiInstance.uploadFile(request);
System.out.println("Expected response type is FilesUploadResult: " + response.getUploaded().size());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
String ClientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String MyStorage = "test-internal-storage";
Configuration configuration = new Configuration(ClientId, ClientSecret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment