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.

    • Constructor Detail

    • 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.
      • 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, or null 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 or null to reset the interceptor.