# Android-XML-to-PDF-Generator
**Repository Path**: 1960176680/Android-XML-to-PDF-Generator
## Basic Information
- **Project Name**: Android-XML-to-PDF-Generator
- **Description**: No description available
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-02-13
- **Last Updated**: 2022-02-13
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
XML to PDF Generator For Android
Automatically generate PDF file from XML file or Java's View object in Android
Make PDF from Android layout resources (e.g - R.layout.myLayout,R.id.viewID), Java's view ids or directly views objects
* **Simple**: Extremely simple to use. For using Step Builder Design Patten undernath,here IDE greatly helps developers to complete the steps for creating a PDF from XMLs.
* **Powerful**: Customize almost everything.
* **Transparent**: It shows logs,success-responses, failure-responses , that's why developer will nofity any event inside the process.
📖 Table of Contents
[](#table-of-contents)
## ➤ Table of Contents
* [➤ Installation](#-installation)
* [➤ Getting Started](#-getting-started)
* [➤ License](#-license)
[](#installation)
## ➤ Installation
**Step 1**. Add the JitPack repository to your root ```build.gradle``` at the end of repositories
```
android {
.
.
/*Need Java version 1.8 as Rx java is used for file write underneath for preventing UI freezing*/
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
.
.
}
.
.
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
.
.
```
**Step 2**. Add the dependency
```
dependencies {
implementation 'com.github.Gkemon:XML-to-PDF-generator:2.5'
}
```
[](#getting-started-quick)
## ➤ Getting Started
You can generate PDF from many sources.
* Layout resources (i.e: ```R.layout.myLayout```)
* View ids (i.e: ```R.id.viewID```)
* Java view objects (i.e ```View```,```TextView```,```LinearLayout```)
### From layout resources :
```java
PdfGenerator.getBuilder()
.setContext(context)
.fromLayoutXMLSource()
.fromLayoutXML(R.layout.layout_print,R.layout.layout_print)
/* "fromLayoutXML()" takes array of layout resources.
* You can also invoke "fromLayoutXMLList()" method here which takes list of layout resources instead of array. */
.setFileName("Test-PDF")
/* It is file name */
.setFolderName("FolderA/FolderB/FolderC")
/* It is folder name. If you set the folder name like this pattern (FolderA/FolderB/FolderC), then
* FolderA creates first.Then FolderB inside FolderB and also FolderC inside the FolderB and finally
* the pdf file named "Test-PDF.pdf" will be store inside the FolderB. */
.openPDFafterGeneration(true)
/* It true then the generated pdf will be shown after generated. */
.build(new PdfGeneratorListener() {
@Override
public void onFailure(FailureResponse failureResponse) {
super.onFailure(failureResponse);
/* If pdf is not generated by an error then you will findout the reason behind it
* from this FailureResponse. */
}
@Override
public void onStartPDFGeneration() {
/*When PDF generation begins to start*/
}
@Override
public void onFinishPDFGeneration() {
/*When PDF generation is finished*/
}
@Override
public void showLog(String log) {
super.showLog(log);
/*It shows logs of events inside the pdf generation process*/
}
@Override
public void onSuccess(SuccessResponse response) {
super.onSuccess(response);
/* If PDF is generated successfully then you will find SuccessResponse
* which holds the PdfDocument,File and path (where generated pdf is stored)*/
}
});
```
### From view IDs :
```java
PdfGenerator.getBuilder()
.setContext(context)
.fromViewIDSource()
.fromViewID(R.layout.hostLayout,activity,R.id.tv_print_area,R.id.tv_print_area)
/* "fromViewID()" takes array of view ids and the host layout xml where the view ids are belonging.
* You can also invoke "fromViewIDList()" method here which takes list of view ids instead of array.*/
.setFileName("Test-PDF")
.setFolderName("Test-PDF-folder")
.openPDFafterGeneration(true)
.build(new PdfGeneratorListener() {
@Override
public void onFailure(FailureResponse failureResponse) {
super.onFailure(failureResponse);
}
@Override
public void onStartPDFGeneration() {
/*When PDF generation begins to start*/
}
@Override
public void onFinishPDFGeneration() {
/*When PDF generation is finished*/
}
@Override
public void showLog(String log) {
super.showLog(log);
}
@Override
public void onSuccess(SuccessResponse response) {
super.onSuccess(response);
}
});
```
### From views:
```java
PdfGenerator.getBuilder()
.setContext(MainActivity.this)
.fromViewSource()
.fromView(view)
.setFileName("Test-PDF")
.setFolderName("Test-PDF-folder")
.openPDFafterGeneration(true)
.build(new PdfGeneratorListener() {
@Override
public void onFailure(FailureResponse failureResponse) {
super.onFailure(failureResponse);
}
@Override
public void showLog(String log) {
super.showLog(log);
}
@Override
public void onStartPDFGeneration() {
/*When PDF generation begins to start*/
}
@Override
public void onFinishPDFGeneration() {
/*When PDF generation is finished*/
}
@Override
public void onSuccess(SuccessResponse response) {
super.onSuccess(response);
}
});
```
### Multi-paged PDF creation:
Users of the library, sometimes have doubts that how to create multi-paged PDF. Though I mentioned it above but I need to show it again for more clearance.
You can insert multiple xml or views object even view id in the parameter of the following methods to create multi-paged pdf:
If you want create multi-paged pdf from xmls-
`.fromLayoutXML(R.layout.layout_1,R.layout.layout_2)`
If you want create multi-paged pdf from view ids -
`.fromViewID(activity,R.id.viewId1,R.id.viewId2)`
If you want create multi-paged pdf from views-
`.fromViewID(view1,view1)`
### How to print an Invoice Or Report ?
Sometimes people gets stuck to print invoice or report via this library.So I wrote an example invoice/report printing fragment to visualise how to print an Invoice or Report. Here is [the link also with an important documentation](https://github.com/Gkemon/Android-XML-to-PDF-Generator/blob/608b873e9f21ed1dbf345a191337a5a548fd3517/sample/src/main/java/com/emon/exampleXMLtoPDF/demoInvoice/DemoInvoiceFragment.java#L61)
### How to deal with generated PDF?
With a method calling named `openPDFafterGeneration(true)`, the generated file will be automatically opened automatically.So you DON'T NEED TO BE BOTHER FOR IT. [FileProvider](https://developer.android.com/reference/androidx/core/content/FileProvider) is used to open file here. To get a good insight about it please see the [tutorial](https://vladsonkin.com/how-to-share-files-with-android-fileprovider/). The `android:authorities` name in the app is `${applicationId}.xmlToPdf.provider` which might be needed if you want to deal with generated file CUSTOMLY,not letting the app open the generated file. you will get the generated file path in `onSuccess(SuccessResponse response)` response.
### Troubleshoot
* For WRAP_CONTENT page size, try to avoid to provide `match_parent` and `wrap_content` height/width in XML. So it specifically.
* If any of your footer view is not placed the footer position then you need adjust it using `marginTop` and keep it in a `ScrollView`.For example this [issue](https://github.com/Gkemon/Android-XML-to-PDF-Generator/issues/16) is fixed by rearranging XML like [this](https://github.com/Gkemon/Android-XML-to-PDF-Generator/blob/master/sample/src/main/res/layout/layout_test_invoice.xml)
So if you find any trouble,then you are also welcomed again to knock me.Thank you so much.
[](#templates)
LinkedIn
Inbox
#### Logo credit: [kirillmazin](https://www.behance.net/kirillmazin)
## ➤ License
The source code is licensed under the [Apache License 2.0](https://github.com/Gkemon/XML-to-PDF-generator/blob/master/LICENSE).
[](#license)