Java Examples
Synchronous conversion of HTML to PDF
You can convert a string of HTML or a URL to a PDF. The config.setDocument(String) method can be either a String of HTML or a URL to a page that you want converted. If you are converted a page that requires Basic authentication, you can set the authentication headers for the request with setAuthenticationCredentials().
java
import com.realobjects.pdfreactor.webservice.client.Configuration;
import com.realobjects.pdfreactor.webservice.client.PDFreactor;
import com.realobjects.pdfreactor.webservice.client.Result;
private static byte[] convertHtml() throws IOException {
//Your HTML to be converted
String html = "<html><head><title>My document</title></head><body><h1>Foo bar</h1><p>Lorem Ipsum!</p></body></html>";
//Create the PDFreactor instance
PDFreactor pdfReactor = new PDFreactor("https://pdf.ais.its.uiowa.edu/service/rest");
pdfReactor.setApiKey("my-super-secure-api-key");
//Network settings for timeouts, etc.
Configuration.NetworkSettings networkSettings = new Configuration.NetworkSettings();
networkSettings.setConnectTimeout(2000); //timeout waiting for css/js/images to download
//Basic configuration
Configuration config = new Configuration();
config.setDocument(html);
config.setDocumentDefaultLanguage("en-US"); //The document language improves accessibility
config.setNetworkSettings(networkSettings);
config.setTitle("The University of Iowa"); //A title is required for accessibility
config.setConformance(Configuration.Conformance.PDFUA1); //This sets the conformance to be accessible
//Do the conversion
try {
Result result = pdfReactor.convert(config);
if (result != null) {
return result.getDocument();
}
} catch (Exception e) {
log.error("PDF service returned error: " + e.getMessage(), e);
throw new IOException("PDF Service returned a failure.");
}
}Converting HTML to an image
PDFreactor is also capable of converting HTML to an image. This might be useful if you need to automatically generate a screenshot of a page. You can output to a PNG (optional transparent background) GIF, JPEG, TIFF, or BMP. The Image Output documentation contains more details and options.
java
import com.realobjects.pdfreactor.webservice.client.Configuration;
import com.realobjects.pdfreactor.webservice.client.PDFreactor;
import com.realobjects.pdfreactor.webservice.client.Result;
private static byte[] convertHtml() throws IOException {
//Your HTML to be converted
String html = "<html><head><title>My document</title></head><body><h1>Foo bar</h1><p>Lorem Ipsum!</p></body></html>";
//Create the PDFreactor instance
PDFreactor pdfReactor = new PDFreactor("https://pdf.ais.its.uiowa.edu/service/rest");
pdfReactor.setApiKey("my-super-secure-api-key");
//Network settings for timeouts, etc.
Configuration.NetworkSettings networkSettings = new Configuration.NetworkSettings();
networkSettings.setConnectTimeout(2000); //timeout waiting for css/js/images to download
//Basic configuration
Configuration config = new Configuration();
config.setDocument(html);
config.setNetworkSettings(networkSettings);
config.setOutputFormat(new OutputFormat()
.setType(OutputType.PNG)
.setWidth(512) //Give it a width
.setHeight(-1)); //-1 means use proportional scaling
//Do the conversion
try {
Result result = pdfReactor.convert(config);
if (result != null) {
return result.getDocument();
}
} catch (Exception e) {
log.error("PDF service returned error: " + e.getMessage(), e);
throw new IOException("PDF Service returned a failure.");
}
}Other conversion topics
Some other key conversion topics include: