Class HtmlToPdfConverter
-
- All Implemented Interfaces:
public final class HtmlToPdfConverter
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 to
false
.Note: This API requires HTML-to-PDF conversion feature in your license.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interface
HtmlToPdfConverter.PageLoadingProgressListener
Listener for page loading progress updates used when loading HTML page in .
-
Method Summary
Modifier and Type Method Description static boolean
doesDeviceSupportConversion(@NonNull() Context context)
Checks whether PSPDFKit can perform HTML-to-PDF conversion on this device. static HtmlToPdfConverter
fromUri(@NonNull() Context context, @NonNull() Uri uri)
Creates HTML-to-PDF converter from Uri. static HtmlToPdfConverter
fromHTMLString(@NonNull() Context context, @NonNull() String htmlString)
Creates HTML-to-PDF converter from HTML string. static HtmlToPdfConverter
fromHTMLString(@NonNull() Context context, @NonNull() String htmlString, @Nullable() String baseUrl)
Creates HTML-to-PDF converter from HTML string. HtmlToPdfConverter
pageSize(@NonNull() Size pageSize)
Sets the size of the created PDF. HtmlToPdfConverter
density(@IntRange(from = 1) int densityDpi)
Sets the density that should be used when converting HTML images to PDF. HtmlToPdfConverter
title(@Nullable() String title)
Sets the document title (set via setTitle). HtmlToPdfConverter
timeout(@IntRange(from = 0) long timeout)
Sets the timeout for loading the HTML document when converting. HtmlToPdfConverter
setJavaScriptEnabled(boolean isEnabled)
Enables or disables JavaScript processing. Single<File>
convertToPdfAsync()
Performs HTML-to-PDF conversion to a temporary file in application's cache directory. Completable
convertToPdfAsync(@NonNull() File outputFile)
Performs HTML-to-PDF conversion to specified file. HtmlToPdfConverter
setResourceInterceptor(@Nullable() ResourceInterceptor resourceInterceptor)
Sets resource interceptor that will be called whenever the HTML converter wants to load any page resource (image, stylesheet, JavaScript etc.). HtmlToPdfConverter
setPageLoadingProgressListener(@Nullable() HtmlToPdfConverter.PageLoadingProgressListener pageLoadingProgressListener)
Sets listener that will be called whenever page loading progress changes. -
-
Method Detail
-
doesDeviceSupportConversion
@UiThread() static boolean doesDeviceSupportConversion(@NonNull() 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.
-
fromUri
@NonNull() static HtmlToPdfConverter fromUri(@NonNull() Context context, @NonNull() 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.
- Parameters:
context
- The context to use.uri
- Base URI for the HTML content that should be converted to PDF.
-
fromHTMLString
@NonNull() static HtmlToPdfConverter fromHTMLString(@NonNull() Context context, @NonNull() 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. In this case, you'll need to provide base URI for resolving relative URIs of these resources via fromHTMLString.
- Parameters:
context
- The context to use.htmlString
- String with HTML content to convert to PDF.
-
fromHTMLString
@NonNull() static HtmlToPdfConverter fromHTMLString(@NonNull() Context context, @NonNull() String htmlString, @Nullable() 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.
Configured
baseUrl
will be used as the base URL for resolving any relative URLs for any resources. Note that the ResourceInterceptor set via setResourceInterceptor is called for relative URLs only when thebaseUrl
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.
-
pageSize
@NonNull() HtmlToPdfConverter pageSize(@NonNull() Size pageSize)
Sets the size of the created PDF. Defaults to PAGE_SIZE_A4.
- Parameters:
pageSize
- Page size in PDF points.
-
density
@NonNull() HtmlToPdfConverter density(@IntRange(from = 1) 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.
-
title
@NonNull() HtmlToPdfConverter title(@Nullable() String title)
Sets the document title (set via setTitle). 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, ornull
to set default title.
-
timeout
@NonNull() HtmlToPdfConverter timeout(@IntRange(from = 0) long timeout)
Sets the timeout for loading the HTML document when converting. Defaults to 30000 ms.
- Parameters:
timeout
- Timeout in ms.
-
setJavaScriptEnabled
@NonNull() 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.
-
convertToPdfAsync
@NonNull() 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.
-
convertToPdfAsync
@NonNull() Completable convertToPdfAsync(@NonNull() 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.
-
setResourceInterceptor
@NonNull() HtmlToPdfConverter setResourceInterceptor(@Nullable() 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 ornull
to reset the interceptor.
-
setPageLoadingProgressListener
@NonNull() HtmlToPdfConverter setPageLoadingProgressListener(@Nullable() HtmlToPdfConverter.PageLoadingProgressListener pageLoadingProgressListener)
Sets listener that will be called whenever page loading progress changes.
- Parameters:
pageLoadingProgressListener
- Listener to set ornull
to reset the listener.
-
-
-
-