Generating PDF Files from HTML

You can use HtmlToPdfConverter to generate PDFs from an HTML file or HTML string. PSPDFKit utilizes full power of the system WebView when generating PDF files from HTML, allowing you to use CSS, embed images, use JavaScript, or take advantage of a variety of modern HTML features.

ℹ️ Note: Using these APIs requires HTML to PDF Conversion feature in your license.

Performing the Conversion

Let’s look at an example that shows how to convert a simple HTML string to PDF:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
val html = """
<html>
    <head>
        <style type="text/css">
            h1 {
                color: red;
            }
        </style>
    </head>
    <body>
        <h1>Hello, world!</h1>
    </body>
</html>
"""

val outputFile: File = // Output file for the converted PDF.

HtmlToPdfConverter.fromHTMLString(context, html)
    // Configure title for the created document.
    .title("Converted document")
    // Perform the conversion.
    .convertToPdfAsync(outputFile)
    // Subscribe to the conversion result.
    .subscribe({
        // Open and process the converted document.
        val document = PdfDocumentLoader.openDocument(context, Uri.fromFile(outputFile))
    }, {
        // Handle the error.
    })
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
final String html =
"<html>\n" +
"    <head>\n" +
"        <style type=\"text/css\">\n" +
"            h1 {\n" +
"                color: red;\n" +
"            }\n" +
"        </style>\n" +
"    </head>\n" +
"    <body>\n" +
"        <h1>Hello, world!</h1>\n" +
"    </body>\n" +
"</html>";

final File outputFile = // Output file for the converted PDF.

// Perform the conversion.
HtmlToPdfConverter.fromHTMLString(context, html)
    // Configure title for the created document.
    .title("Converted document")
    // Perform the conversion.
    .convertToPdfAsync(outputFile)
    // Subscribe to the conversion result.
    .subscribe(() -> {
        // Open and process the converted document.
        PdfDocument document = PdfDocumentLoader.openDocument(context, Uri.fromFile(outputFile));
    }, throwable -> {
        // Handle the error.
    });

In addition to providing source HTML from a string, you can also load your data from URIs by creating a converter via the factory method HtmlToPdfConverter#fromUri(). Both local and remote URIs are supported:

  • Local URIs with the file scheme for local files and content scheme for data are provided by the content provider. Note that the file scheme can also refer to the Android resources (file:///android_res/) and assets (file:///android_asset) that are also supported.
  • Remote URIs with the schemes http and https.

For more details about how to generate PDF files from HTML strings and URLs, take a look at HtmlToPdfConverter documentation and ConvertHtmlToPdfExample from our Catalog app.

Controlling the Conversion

HtmlToPdfConverter provides basic control over the generated PDF. This includes:

  • pageSize() to configure the page size of the created PDF. Defaults to A4 page size.
  • density() to control density used when converting HTML images to PDF. Defaults to 300dpi.
  • title() to configure the document title of the created PDF. Defaults to null title.

There are also multiple methods for controlling the conversion itself:

  • timeout() configures a timeout for loading the HTML page before conversion. Once this timeout is reached, the conversion fails with an error. Defaults to 30,000 ms (or 30 seconds).
  • setJavaScriptEnabled() controls whether or not JavaScript execution should be enabled while converting. Defaults to enabled. Note that the JavaScript execution could cause security and performance issues. Please review your JavaScript carefully or consider disabling this property.

Observing the Conversion Progress

If you wish to observe the HTML page loading progress (for example, when loaded from the network), use setPageLoadingProgressListener() to register the page loading progress listener:

Copy
1
2
3
4
5
6
HtmlToPdfConverter.fromUri(context, sourceUri)
    .setPageLoadingProgressListener { progress ->
        // Use the page loading progress to update your UI.
    }
     .convertToPdfAsync()
    .subscribe(...)
Copy
1
2
3
4
5
6
HtmlToPdfConverter.fromUri(context, sourceUri)
    .setPageLoadingProgressListener((progress) -> {
        // Use the page loading progress to update your UI.
    })
    .convertToPdfAsync()
    .subscribe(...);

Intercepting Loaded Resources

HtmlToPdfConverter will try to resolve and load all resources referenced by the source HTML document. This includes but is not limited to images, stylesheets, and JavaScript (if enabled).

You can provide otherwise inaccessible resources or override default resource loading by registering ResourceInterceptor via setResourceInterceptor():

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 HtmlToPdfConverter.fromUri(context, sourceUri)
    .setResourceInterceptor { request ->
        if (request.url.lastPathSegment == "image.jpg") {
            // You can override the page resource with a different resource.
            ResourceResponse(FileDataProvider(customImage), "image/jpeg")
        } else if (request.url.lastPathSegment == "other-image.jpg") {
            // You can skip loading the resource by returning an empty resource.
            ResourceResponse.skipResource()
        } else {
            // Or you can return `null` to proceed with default resource loading.
            null
        }
    }
    .convertToPdfAsync()
    .subscribe(...)
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HtmlToPdfConverter.fromUri(context, sourceUri)
    .setResourceInterceptor((request) -> {
        if ("image.jpg".equals(request.getUrl().getLastPathSegment())) {
            // You can override the page resource with a different resource.
            return new ResourceResponse(new FileDataProvider(customImage), "image/jpeg");
        } else if ("other-image.jpg".equals(request.getUrl().getLastPathSegment())) {
            // You can skip loading the resource by returning an empty resource.
            return ResourceResponse.skipResource();
        } else {
            // Or you can return `null` to proceed with default resource loading.
            return null;
        }
    })
    .convertToPdfAsync()
    .subscribe(...);

ℹ️ Note: The resource interceptor’s method shouldInterceptRequest() is invoked for most supported URI schemes (http(s), file, etc.) and is not limited to requests made over the network. This is not called for javascript, data, and blob schemes or for Android assets (file:///android_asset/) or Android resources (file:///android_res/).