Html2Bitmap


License

License

MIT License
GroupId

GroupId

com.izettle
ArtifactId

ArtifactId

html2bitmap
Last Version

Last Version

1.10
Release Date

Release Date

Type

Type

aar
Description

Description

Html2Bitmap
Html2Bitmap
Project URL

Project URL

https://github.com/iZettle/android-html2bitmap
Source Code Management

Source Code Management

https://github.com/iZettle/android-html2bitmap

Download html2bitmap

How to add to project

<!-- https://jarcasting.com/artifacts/com.izettle/html2bitmap/ -->
<dependency>
    <groupId>com.izettle</groupId>
    <artifactId>html2bitmap</artifactId>
    <version>1.10</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.izettle/html2bitmap/
implementation 'com.izettle:html2bitmap:1.10'
// https://jarcasting.com/artifacts/com.izettle/html2bitmap/
implementation ("com.izettle:html2bitmap:1.10")
'com.izettle:html2bitmap:aar:1.10'
<dependency org="com.izettle" name="html2bitmap" rev="1.10">
  <artifact name="html2bitmap" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.izettle', module='html2bitmap', version='1.10')
)
libraryDependencies += "com.izettle" % "html2bitmap" % "1.10"
[com.izettle/html2bitmap "1.10"]

Dependencies

compile (1)

Group / Artifact Type Version
androidx.annotation » annotation jar 1.0.2

Project Modules

There are no modules declared in this project.

Generate a bitmap from html on Android

Generates a bitmap of a defined width from html by rendering the content inside an off screen webview.

Does not block main thread.

Uses the webView available on the system so it's possibilities and limitations will be mirrored by the library.

Height will be calculated using WebView.getContentHeight()

Usage:

dependencies {
    implementation 'com.izettle:html2bitmap:1.9'
}
Bitmap bitmap = new Html2Bitmap.Builder(context: Context, content: WebViewContent).build().getBitmap();
  • context Used to create webview - application context is fine.
  • html The html that should be rendered to a bitmap.
  • width The requested width of the generated bitmap.
  • timeout Seconds before the job will terminate - if timeout is reached will call stopLoading() on the webview and return null.

Calls are blocking and hence recommended to be moved into a background thread.

Under the hood

Will wait for remote and local resources to load before generating bitmap i.e. "http://www.sample.com/image.jpg" will be downloaded before the screenshot is taken - take this into account before defining a timeout for the whole process.

Can be used anywhere you have access to a context. The purpose of the library was to perform its job inside a background thread and not to be tied to an activity.

Calls are synchronous to simplify usage in a background thread.

new AsyncTask<Void, Void, Bitmap>() {
    @Override
    protected Bitmap doInBackground(Void... voids) {
        String html = "<html><body><p>Hello world!</p><br/>Html bitmap</body><html>";
        
        return new Html2Bitmap.Builder().setContext(context).setContent(WebViewContent.html(html)).build().getBitmap();   
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}.execute();

Note: Some of the methods on the webview are required to be run on the mainThread of the application - so some operations will be performed on that thread.

screenshot_1523608598

com.izettle

Zettle

Versions

Version
1.10
1.9
1.8
1.7
1.6
1.5
1.4