PDF Generator library, easy way to create PDF from String Content or Any HTML Content.
// Not getting much time to maintain this code, there might be some issues with latest Android SDK.
// If you're facing any please take pull and resolve offline for your case or raise the PR for others.
// Thanks.
dependencies {
implementation 'com.uttampanchasara.pdfgenerator:pdfgenerator:1.3'
}
In order to start using PdfGenerator, Just copy below code to your project and just pass the required values and that's all you done!
CreatePdf(this)
.setPdfName("FirstPdf")
.openPrintDialog(false)
.setContentBaseUrl(null)
.setPageSize(PrintAttributes.MediaSize.ISO_A4)
.setContent("Your Content")
.setFilePath(Environment.getExternalStorageDirectory().absolutePath + "/MyPdf")
.setCallbackListener(object : CreatePdf.PdfCallbackListener {
override fun onFailure(errorMsg: String) {
Toast.makeText(this@MainActivity, errorMsg, Toast.LENGTH_SHORT).show()
}
override fun onSuccess(filePath: String) {
Toast.makeText(this@MainActivity, "Pdf Saved at: $filePath", Toast.LENGTH_SHORT).show()
}
})
.create()
new CreatePdf(this)
.setPdfName("FirstPdf")
.openPrintDialog(false)
.setContentBaseUrl(null)
.setPageSize(PrintAttributes.MediaSize.ISO_A4)
.setContent("Your Content")
.setFilePath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyPdf")
.setCallbackListener(new CreatePdf.PdfCallbackListener() {
@Override
public void onFailure(@NotNull String s) {
// handle error
}
@Override
public void onSuccess(@NotNull String s) {
// do your stuff here
}
})
.create();
In library, I'm not handling any storage permission related exception, If you are providing your custom filePath then your application must have STORAGE READ-WRITE Permission in order to store Pdf in provided path.
Provide Your Pdf name, Library will use to save pdf with this name.
Default is false
, If you set true
it will send your pdf for print and open the android default pdf print view.
If you are loading content from assets folder in that case you can pass your base url here, same as we passed in webview.
To set custom page size for your pdf, you will have to pass the mediaSize as argument.
Example - For A4 size: PrintAttributes.MediaSize.ISO_A4 | ISO_A0 | ISO_A1
Provide your String content, which you want to generate Pdf.
Provide custom file path to save pdf in your own directory, default will be the cache directory of Application
Set this callback listener to get callback on pdf generated.
- Easily Generate Pdf
- No Extra codes
- Time saving
- Lightweight
If you have any suggestions or you can make this library better write me, create issue, or you can also write code and send pull request.
If you found this library helpful, consider buying me a cup of ☕
Copyright 2019 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.