Java API

Example

// Instantiate export client
ExportClient exportClient = new ExportClient("enter your export key here");

// Set export properties
exportClient.setSourceUrl("http://news.google.com");
exportClient.enableLinks(true);
exportClient.avoidImageBrake(true);
exportClient.setFormat(Constants.FORMAT_A4);
exportClient.setOrientation(Constants.ORIENTATION_PORTRAIT);
exportClient.setStandard(Constants.STANDARD_PDF);

// Below code demonstrates export HTML to PDF in a web service (Java servlet) 
// with an error handling.
java.io.OutputStream outputStream = null;
try
{
    // Set the method of delivery of the resultant document 
    // ("attachment" or "inline").
    String delivery = "attachment";

    // Set javax.servlet.http.HttpServletResponse headers
    response.addHeader("Content-Type", "application/pdf; charset=utf-8");
    response.addHeader("Content-Disposition", delivery + "; filename=test.pdf");
    response.addHeader("Set-Cookie", "fileDownload=true; path=/");

    // Get OutputStream
    outputStream = response.getOutputStream();

    // Export HTML to PDF
    long documentSize = exportClient.export(outputStream);

    response.setContentLength((int) documentSize);
}
catch (Exception e)
{
    // Get an error message that corresponds to a specific error code 
    // (see com.runpdf.Constants.ERROR_CODE_MESSAGE_MAP).
    String errorMsg = exportClient.getErrorMessage("" + e.getMessage());
            
    // Handle error
}
finally
{
    // Close OutputStream
    if (outputStream != null)
    {
        try
        {
            outputStream.close();
        }
        catch (IOException e)
        {
            // Do nothing: unexpected error closing OutputStream
        }
    }
}
                                                
Visit our Demo pages and see more sample code

Summary

com.runpdf
Class ExportClient
Click here to view the source
Description:
The class is used to access RunPDF web service that lets you create PDF documents from web ages or raw HTML markup in your applications.

Constructors

public ExportClient(String exportKey)
Description:
A constructor that initializes a newly created ExportClient object with the specified export key.
Parameters:
exportKey - The key is used for the request authentication. All accounts are assigned a unique export key. The key is available in the signup confirmation email and the Order Detail page.

public ExportClient(String exportKey, string serviceURL)
Description:
A constructor that initializes a newly created ExportClient object with the specified export key and RunPDF export service URL.
Parameters:
exportKey - The key is used for the request authentication. All accounts are assigned a unique export key. The key is available in the signup confirmation email and the Order Detail page.
serviceURL - The URL to RunPDF export web service

Service

public int export(OutputStream outputStream) throws Exception
Description:
Exports HTML content to PDF, copies the stream of the resultant document to the target OutputStream and returns the size of the stream.
Parameters:
outputStream - The target OutputStream.
Returns:
Size of the resultant document stream.
Throws:
Exception - The exception is thrown in case of failure to execute. The exception message is set to a specific error code (see com.runpdf.Constants.ERROR_CODE_MESSAGE_MAP).

Error messaging

public String getErrorMessage(String errorCode)
Description:
Returns an error message based on the specified error code.
Parameters:
errorCode - The error code.
Returns:
An error message.

Other Methods

public void setSourceUrl(String sourceUrl)
Description:
Sets source web page URL to convert.
Parameters:
sourceUrl - URL of the target web page.
Note:
Ether source Url or source HTML is required.

public void setSourceUrl(String sourceUrl, String sourceUrl2)
Description:
Sets primary and secondary source web page URLs to convert.
Parameters:
sourceUrl - URL of the target web page.
sourceUrl2 - URL of the secondary target web page.

public void setSourceHtml(String sourceHtml)
Description:
Sets HTML source string.
Parameters:
sourceHtml - HTML string to convert.
Note:
Ether source Url or source HTML is required.

public void setSourceHtml(String sourceHtml, String baseUrl)
Description:
Sets HTML source string and the base URL.
Parameters:
sourceHtml - HTML string to convert.
baseUrl - URL of the web page to process CSS when converting HTML source.

public void setMediaTypePrint(boolean value)
Description:
Sets if to present the document on print media style.
Parameters:
value - Boolean value. Default is false.

public void SetSinglePage(boolean value)
Description:
Sets if to render the whole HTML content into a single PDF page.
Parameters:
value - Boolean value. Default is false.

public void setAuthentication(String username, String password)
Description:
Sets username and password credentials for NTLM authentication.
Parameters:
username - The username credential.
password - The password credential.
Note:
Only NTLM authentication is supported.

public void setStandard(String standard)
Description:
Sets the standard of the resultant PDF document.
Parameters:
standard - The standard of the resultant PDF document. The values are available in com.runpdf.Constants (Example: Constants.STANDARD_PDF_A;). Default format is STANDARD_PDF.

public void avoidImageBrake(boolean value)
Description:
Sets if to avoid image break between pages.
Parameters:
value - Boolean value. Default is false.

public void enableLinks(boolean value)
Description:
Sets if to enable hyper links on the resultant PDF document.
Parameters:
value - Boolean value. Default is false.

public void setFormat(String format)
Description:
Sets the format of the document pages.
Parameters:
format - The format of the document pages. The values are available in com.runpdf.Constants (Example: Constants.FORMAT_A0;). Default format is FORMAT_A4 - "A4".

public void setOrientation(String orientation)
Description:
Sets the orientation of the document pages.
Parameters:
orientation - The orientation of the document pages. The values are available in com.runpdf.Constants (Example: Constants.ORIENTATION_LANDSCAPE;). Default format is Constants.ORIENTATION_PORTRAIT.


public void setHeaderSourceHtml(String headerSourceHtml)
Description:
Sets the HTTP source for the header.
Parameters:
headerSourceHtml - The HTTP source for the header.

public void setFooterSourceUrl(String footerSourceUrl)
Description:
Sets the URL source for the footer.
Parameters:
footerSourceUrl - The URL source for the footer.

public void setHeaderSourceUrl(String headerSourceUrl)
Description:
Sets the URL source for the header.
Parameters:
headerSourceUrl - The URL source for the header.

public void setHeaderHeight(String headerHeight)
Description:
Sets the height of the header.
Parameters:
headerHeight - The height of the header.

public void setFooterHeight(String footerHeight)
Description:
Sets the height of the footer.
Parameters:
footerHeight - The height of the footer.

public void showPageNumberOnFooter(boolean value)
Description:
Sets if to show page number on the footer.
Parameters:
value - Boolean value. Default is false.

public void showSecondDocumentOnNewPage(boolean value)
Description:
Sets if to start contents of the second web page on new page of the document.
Parameters:
value - Boolean value. Default is false.

public void setOwnerPassword(String ownerPassword)
Description:
Sets password of the document owner.
Parameters:
ownerPassword - Password of the document owner.

public void setUserPassword(String userPassword)
Description:
Sets password of the document user.
Parameters:
userPassword - Password of the document user.

public void allowPrint(boolean value)
Description:
Sets if to allow the document user to print the document.
Parameters:
value - Boolean value. Default is true.

public void allowEdit(boolean value)
Description:
Sets if to allow the document user to edit the document.
Parameters:
value - Boolean value. Default is true.

public void allowCopy(boolean value)
Description:
Sets if to allow the document user to copy contents of the document.
Parameters:
value - Boolean value. Default is true.

public void setWatermarkImageUrl(String watermarkImageUrl)
Description:
Sets URI to the watermark image. PNG format only.
Parameters:
watermarkImageUrl - URI to the watermark image.

public void setWatermarkText(String watermarkText)
Description:
Sets the watermark text.
Parameters:
watermarkText - Text to show as the watarmark.

public void setWatermarkOpacity(String watermarkOpacity)
Description:
Sets the watermark opacity.
Parameters:
watermarkOpacity - Opacity of the watarmark. A number between 0 and 100. Default is 50.

public void setWatermarkTextColor(String watermarkTextColor)
Description:
Sets the watermark text color.
Parameters:
watermarkTextColor - Color of the watermark text. A valid name of the color. Default is Gray.

public void setWatermarkTextFontSize (String watermarkTextFontSize)
Description:
Sets the size of the watarmark text font.
Parameters:
watermarkTextFontSize - Size of the watarmark text font. A number between 0 and 100. Default is 50.

public void setWatermarkTextPosition (String watermarkTextPosition)
Description:
Sets the watermark text position.
Parameters:
watermarkText - Position of the watermark. The values are available in com.runpdf.Constants (Example: Constants.WM_TEXT_POSITION_DIAGONAL;). Default position is Constants.WM_TEXT_POSITION_HORIZONTAL.