public final class

HtmlToPdfConverter

extends Object
java.lang.Object
   ↳ com.pspdfkit.document.html.HtmlToPdfConverter

Class Overview

Converts HTML to PDF document.

Note: HTML-to-PDF conversion internally relies on the system WebView implementation. This means that there are certain aspects of this conversion process that PSPDFKit can't support. This includes, for example, any possible WebView bugs.

Note: JavaScript execution is enabled by default. This could cause security and performance issues. Please review your JavaScripts carefully. If you wish to disable JavaScript execution while performing the HTML conversion, set setJavaScriptEnabled(boolean) to false.

Note: This API requires HTML-to-PDF conversion feature in your license.

Summary

Nested Classes
interface HtmlToPdfConverter.PageLoadingProgressListener Listener for page loading progress updates used when loading HTML page in HtmlToPdfConverter
Public Methods
Single<File> convertToPdfAsync()
Performs HTML-to-PDF conversion to a temporary file in application's cache directory.
Completable convertToPdfAsync(File outputFile)
Performs HTML-to-PDF conversion to specified file.
HtmlToPdfConverter density(int densityDpi)
Sets the density that should be used when converting HTML images to PDF.
static boolean doesDeviceSupportConversion(Context context)
Checks whether PSPDFKit can perform HTML-to-PDF conversion on this device.
static HtmlToPdfConverter fromHTMLString(Context context, String htmlString, String baseUrl)
Creates HTML-to-PDF converter from HTML string.
static HtmlToPdfConverter fromHTMLString(Context context, String htmlString)
Creates HTML-to-PDF converter from HTML string.
static HtmlToPdfConverter fromUri(Context context, Uri uri)
Creates HTML-to-PDF converter from Uri.
HtmlToPdfConverter pageSize(Size pageSize)
Sets the size of the created PDF.
HtmlToPdfConverter setJavaScriptEnabled(boolean isEnabled)
Enables or disables JavaScript processing.
HtmlToPdfConverter setPageLoadingProgressListener(HtmlToPdfConverter.PageLoadingProgressListener pageLoadingProgressListener)
Sets listener that will be called whenever page loading progress changes.
HtmlToPdfConverter setResourceInterceptor(ResourceInterceptor resourceInterceptor)
Sets resource interceptor that will be called whenever the HTML converter wants to load any page resource (image, stylesheet, JavaScript etc.).
HtmlToPdfConverter timeout(long timeout)
Sets the timeout for loading the HTML document when converting.
HtmlToPdfConverter title(String title)
Sets the document title (set via setTitle(String)).
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public Single<File> convertToPdfAsync ()

Performs HTML-to-PDF conversion to a temporary file in application's cache directory.

This method operates asynchronously on the io() scheduler.

Returns
  • Single emitting file with the converted PDF or an error if conversion failed.

public Completable convertToPdfAsync (File outputFile)

Performs HTML-to-PDF conversion to specified file.

This method operates asynchronously on the io() scheduler.

Parameters
outputFile Output file to write converted PDF.
Returns
  • Completable emitting completion if PDF conversion finished or an error if conversion failed.

public HtmlToPdfConverter density (int densityDpi)

Sets the density that should be used when converting HTML images to PDF. Defaults to 300 dpi.

Parameters
densityDpi Density to use in dots per inch.

public static boolean doesDeviceSupportConversion (Context context)

Checks whether PSPDFKit can perform HTML-to-PDF conversion on this device. This checks for availability of the WebView system packages. This method returns false, if the WebView package is not installed or has been disabled by the user.

Note that this method must be called from the main thread.

Parameters
context Context used to check installed WebView packages.
Returns
  • true if the conversion is available, false if not.
Throws
IllegalStateException if not called from the main thread.

public static HtmlToPdfConverter fromHTMLString (Context context, String htmlString, String baseUrl)

Creates HTML-to-PDF converter from HTML string.

You can provide any resources (images, stylesheets, JavaScript etc.) required by the HTML by setting custom ResourceInterceptor via setResourceInterceptor(ResourceInterceptor).

Configured baseUrl will be used as the base URL for resolving any relative URLs for any resources. Note that the ResourceInterceptor set via setResourceInterceptor(ResourceInterceptor) is called for relative URLs only when the baseUrl is set.

Parameters
context The context to use.
htmlString String with HTML content to convert to PDF.
baseUrl Base url to use to load relative URLs in the HTML. May be null.

public static HtmlToPdfConverter fromHTMLString (Context context, String htmlString)

Creates HTML-to-PDF converter from HTML string.

You can provide any resources (images, stylesheets, JavaScript etc.) required by the HTML by setting custom ResourceInterceptor via setResourceInterceptor(ResourceInterceptor). In this case, you'll need to provide base URI for resolving relative URIs of these resources via fromHTMLString(Context, String, String).

Parameters
context The context to use.
htmlString String with HTML content to convert to PDF.

public static HtmlToPdfConverter fromUri (Context context, Uri uri)

Creates HTML-to-PDF converter from Uri. Supported URIs are:

  • Local URIs with schemes "file://" and "content://". This includes Android resources and assets (i.e. "file:///android_res/" and "file:///android_asset/").
  • Remote URIs with schemes "http://" and "https://".

Converter will try to load all linked resources (images, stylesheets, JavaScript etc.), if they are accessible. You can provide otherwise inaccessible resources or override default ones by setting custom ResourceInterceptor via setResourceInterceptor(ResourceInterceptor).

Parameters
context The context to use.
uri Base URI for the HTML content that should be converted to PDF.

public HtmlToPdfConverter pageSize (Size pageSize)

Sets the size of the created PDF. Defaults to PAGE_SIZE_A4.

Parameters
pageSize Page size in PDF points.

public HtmlToPdfConverter setJavaScriptEnabled (boolean isEnabled)

Enables or disables JavaScript processing. Defaults to true.

Note: JavaScript execution could cause security and performance issues. Please review your JavaScripts carefully.

Parameters
isEnabled true to enable JavaScript processing for loaded HTML documents.

public HtmlToPdfConverter setPageLoadingProgressListener (HtmlToPdfConverter.PageLoadingProgressListener pageLoadingProgressListener)

Sets listener that will be called whenever page loading progress changes.

Parameters
pageLoadingProgressListener Listener to set or null to reset the listener.

public HtmlToPdfConverter setResourceInterceptor (ResourceInterceptor resourceInterceptor)

Sets resource interceptor that will be called whenever the HTML converter wants to load any page resource (image, stylesheet, JavaScript etc.). Use this if you want to inject custom resources that are otherwise not accessible from the base URL or when you want to override default resource resolution.

Parameters
resourceInterceptor Interceptor to use or null to reset the interceptor.

public HtmlToPdfConverter timeout (long timeout)

Sets the timeout for loading the HTML document when converting. Defaults to 30000 ms.

Parameters
timeout Timeout in ms.

public HtmlToPdfConverter title (String title)

Sets the document title (set via setTitle(String)). Defaults to HTML title if present (i.e. contents of the <title> tag); sets no title if the HTML document has no title.

Parameters
title Title that should be set for the converted document, or null to set default title.