PDF Generator library - Easy way to create PDF from HTML content, URLs, or Asset files.
- Generate PDF from HTML content, URLs, or Asset files
- Kotlin Coroutines support with
createAsync() - Customizable page size, margins, resolution, and orientation
- Built-in timeout handling to prevent hanging
- Modern scoped storage support (no permissions needed)
- Proper memory management and error handling
Add JitPack repository to your root settings.gradle:
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}Add the dependency to your app's build.gradle:
dependencies {
implementation 'com.github.UttamPanchasara:PDF-Generator:2.0.0'
}val savePath = CreatePdf.getDefaultSavePath(context, "MyPDFs")
CreatePdf(context)
.setPdfName("document")
.setContent("<html><body><h1>Hello World</h1></body></html>")
.setFilePath(savePath)
.setCallbackListener(object : CreatePdf.PdfCallbackListener {
override fun onSuccess(filePath: String) {
// PDF created at filePath
}
override fun onFailure(errorMsg: String) {
// Handle error
}
})
.create()lifecycleScope.launch {
val result = CreatePdf(context)
.setPdfName("document")
.setContent("<html><body><h1>Hello World</h1></body></html>")
.setFilePath(CreatePdf.getDefaultSavePath(context))
.createAsync()
result.onSuccess { filePath ->
// PDF created at filePath
}.onFailure { error ->
// Handle error
}
}CreatePdf(context)
.setPdfName("webpage")
.setUrl("https://example.com")
.setTimeout(60_000) // 60 seconds for web pages
.setCallbackListener(listener)
.create()CreatePdf(context)
.setPdfName("invoice")
.setAssetPath("templates/invoice.html")
.setCallbackListener(listener)
.create()CreatePdf(context)
.setPdfName("report")
.setContent(htmlContent)
.setLandscape(true)
.setMarginsFromMm(10f, 10f, 10f, 10f) // 10mm margins
.setCallbackListener(listener)
.create()CreatePdf(context)
.setPdfName("high-quality")
.setContent(htmlContent)
.setResolution(300) // 300 DPI (default is 600)
.setCallbackListener(listener)
.create()| Method | Description |
|---|---|
setContent(html) |
Set HTML/text content to convert |
setUrl(url) |
Load URL and convert to PDF (requires INTERNET permission) |
setAssetPath(path) |
Load HTML from assets folder |
| Method | Default | Description |
|---|---|---|
setPageSize(size) |
ISO_A4 |
Page size (ISO_A4, NA_LETTER, etc.) |
setLandscape(bool) |
false |
Enable landscape orientation |
setMargins(l,t,r,b) |
NO_MARGINS |
Set margins in mils (1/1000 inch) |
setMarginsFromMm(l,t,r,b) |
- | Set margins in millimeters |
setResolution(dpi) |
600 |
PDF resolution/quality |
| Method | Description |
|---|---|
setPdfName(name) |
PDF filename (without .pdf extension) |
setFilePath(path) |
Directory to save PDF |
openPrintDialog(bool) |
Open print dialog after creation |
| Method | Default | Description |
|---|---|---|
setTimeout(ms) |
30000 |
Timeout in milliseconds |
setContentBaseUrl(url) |
null |
Base URL for relative paths |
| Method | Description |
|---|---|
create() |
Create PDF asynchronously with callbacks |
createAsync() |
Create PDF with Kotlin Coroutines (returns Result<String>) |
| Method | Description |
|---|---|
CreatePdf.getDefaultSavePath(context, subdir) |
Get app-specific storage path (no permissions needed) |
- Minimum SDK: 26 (Android 8.0)
- Compile SDK: 35 (Android 15)
- Kotlin: 2.0+
For URL loading, add to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />No storage permissions needed when using getDefaultSavePath().
- Update dependency to use JitPack
- Replace
Environment.getExternalStorageDirectory()withCreatePdf.getDefaultSavePath() - Remove storage permissions if using app-specific storage
setPageSize()is now optional (defaults to A4)
If you found this library helpful, consider buying me a cup of ☕
Copyright 2019-2025 Uttam Panchasara
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.