├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── compiler.xml ├── gradle.xml ├── jarRepositories.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── build.gradle ├── demo-home-screen-ss.png ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml ├── logo.png ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── emon │ │ └── exampleXMLtoPDF │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── emon │ │ │ └── exampleXMLtoPDF │ │ │ ├── MainActivity.java │ │ │ ├── demoInvoice │ │ │ └── DemoInvoiceFragment.java │ │ │ └── dummyList │ │ │ ├── DummyContent.java │ │ │ └── DummyItemRecyclerViewAdapter.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── demo_a4_sized_page.xml │ │ ├── demo_barcode.xml │ │ ├── demo_barcode_90_degree_rotated.xml │ │ ├── fragment_demo_invoice.xml │ │ ├── fragment_item.xml │ │ ├── item_boxes.xml │ │ ├── layout_print.xml │ │ ├── layout_print_horizontal_scroll.xml │ │ ├── layout_test_invoice.xml │ │ └── rv_item_list.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── emon │ └── exampleXMLtoPDF │ └── ExampleUnitTest.java ├── settings.gradle └── xml-to-pdf ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── gkemon │ └── XMLtoPDF │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── com │ │ └── gkemon │ │ └── XMLtoPDF │ │ ├── FileUtils.java │ │ ├── PdfGenerator.java │ │ ├── PdfGeneratorListener.java │ │ ├── Utils.java │ │ └── model │ │ ├── FailureResponse.java │ │ ├── SuccessResponse.java │ │ └── XmlToPDFFileProvider.java └── res │ ├── values │ └── strings.xml │ └── xml │ └── provider_paths.xml └── test └── java └── com └── gkemon └── XMLtoPDF └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 20 | 21 | 22 | 23 | 25 | 26 | 27 |
28 | 29 | 30 | 31 | xmlns:android 32 | 33 | ^$ 34 | 35 | 36 | 37 |
38 |
39 | 40 | 41 | 42 | xmlns:.* 43 | 44 | ^$ 45 | 46 | 47 | BY_NAME 48 | 49 |
50 |
51 | 52 | 53 | 54 | .*:id 55 | 56 | http://schemas.android.com/apk/res/android 57 | 58 | 59 | 60 |
61 |
62 | 63 | 64 | 65 | .*:name 66 | 67 | http://schemas.android.com/apk/res/android 68 | 69 | 70 | 71 |
72 |
73 | 74 | 75 | 76 | name 77 | 78 | ^$ 79 | 80 | 81 | 82 |
83 |
84 | 85 | 86 | 87 | style 88 | 89 | ^$ 90 | 91 | 92 | 93 |
94 |
95 | 96 | 97 | 98 | .* 99 | 100 | ^$ 101 | 102 | 103 | BY_NAME 104 | 105 |
106 |
107 | 108 | 109 | 110 | .* 111 | 112 | http://schemas.android.com/apk/res/android 113 | 114 | 115 | ANDROID_ATTRIBUTE_ORDER 116 | 117 |
118 |
119 | 120 | 121 | 122 | .* 123 | 124 | .* 125 | 126 | 127 | BY_NAME 128 | 129 |
130 |
131 |
132 |
133 |
134 |
-------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | 34 | 35 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Logo 3 |

4 |

XML to PDF Generator For Android

5 |

6 | Maintained 7 | Maintained 8 |

9 |

10 | Maintained 11 | version - 2.6.8 12 |

13 | 14 |

15 | Automatically generate PDF file from XML file or Java's View object in Android
16 |

Make PDF from Android layout resources (e.g - R.layout.myLayout,R.id.viewID) ids or directly views objects

17 |

18 |

Run the sample app and see it's or youtube video below for getting more clearance.

19 | 20 |

21 | Demo 22 |

23 | 24 | * **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. 25 | * **Powerful**: Customize almost everything. 26 | * **Transparent**: It shows logs,success-responses, failure-responses , that's why developer will nofity any event inside the process. 27 | 28 |
29 | 📖 Table of Contents 30 |
31 | 32 | [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#table-of-contents) 33 | 34 | ## ➤ Table of Contents 35 | 36 | * [➤ Installation](#-installation) 37 | * [➤ Getting Started](#-getting-started) 38 | * [➤ License](#-license) 39 |
40 | 41 | 42 | [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#installation) 43 | 44 | ## ➤ Installation 45 | 46 | **Step 1**. Add the JitPack repository to your root ```build.gradle``` at the end of repositories 47 | ``` 48 | android { 49 | . 50 | . 51 | 52 | /*Need Java version 1.8 as Rx java is used for file write underneath for preventing UI freezing*/ 53 | compileOptions { 54 | sourceCompatibility JavaVersion.VERSION_1_8 55 | targetCompatibility JavaVersion.VERSION_1_8 56 | } 57 | . 58 | . 59 | } 60 | . 61 | . 62 | allprojects { 63 | repositories { 64 | // ... 65 | maven { url 'https://jitpack.io' } 66 | } 67 | } 68 | . 69 | . 70 | ``` 71 | 72 | **Step 2**. Add the dependency 73 | ``` 74 | dependencies { 75 | implementation 'com.github.Gkemon:Android-XML-to-PDF-Generator:2.6.8' 76 | } 77 | ``` 78 | [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#getting-started-quick) 79 | 80 | 81 | 82 | ## ➤ Getting Started 83 | 84 | You can generate PDF from many sources. 85 | * Layout resources (i.e: ```R.layout.myLayout```) 86 | * View ids (i.e: ```R.id.viewID```) 87 | * Java view objects (i.e ```View```,```TextView```,```LinearLayout```) because sometimes we need to change the content of the XML and then dealing this java view object is only way to do this. 88 | 89 | ## ➤ Important Note 90 | ![#f03c15](https://via.placeholder.com/15/f03c15/f03c15.png) 91 | ![#f03c15](https://via.placeholder.com/15/f03c15/f03c15.png) 92 | ![#f03c15](https://via.placeholder.com/15/f03c15/f03c15.png) 93 | For better output and make the PDF more responsive,please try to set ```android:layout_width``` of the top most view of XML a fixed value in pixel or ```px``` (Recommandation is not use ```dp``` as it depends on device screen) instead of ```wrap_content``` and ```match_parent``` otherwise sizing could be malformed in PDF for different device screen.Suppose if you want to print an A4 sized pdf so you can see my example from [where](https://github.com/Gkemon/Android-XML-to-PDF-Generator/blob/master/sample/src/main/res/layout/demo_a4_sized_page.xml). You just need to make your XML's width:hight aspected ratio 1:1.14142. Referece is [here](https://www.papersizes.org/sp/a/a4#:~:text=The%20aspect%20ratio%20(width%3Aheight,(1%3A%E2%88%9A2) ). 94 | ![#f03c15](https://via.placeholder.com/15/f03c15/f03c15.png) 95 | ![#f03c15](https://via.placeholder.com/15/f03c15/f03c15.png) 96 | ![#f03c15](https://via.placeholder.com/15/f03c15/f03c15.png) 97 | 98 | ### From layout resources : 99 | ( Only static content in XML will be printed by this approach. If you want to change the content of the XML ,suppose there is a 100 | text view in the XML and you want to populate it with a data then try the [approach](https://github.com/Gkemon/Android-XML-to-PDF-Generator/edit/master/README.md#from-views) ) 101 | 102 | 103 | ```java 104 | PdfGenerator.getBuilder() 105 | .setContext(context) 106 | .fromLayoutXMLSource() 107 | .fromLayoutXML(R.layout.layout_print,R.layout.layout_print) 108 | /* "fromLayoutXML()" takes array of layout resources. 109 | * You can also invoke "fromLayoutXMLList()" method here which takes list of layout resources instead of array. */ 110 | .setFileName("Test-PDF") 111 | /* It is file name */ 112 | .setFolderName("FolderA/FolderB/FolderC") 113 | /* It is folder name. If you set the folder name like this pattern (FolderA/FolderB/FolderC), then 114 | * FolderA creates first.Then FolderB inside FolderB and also FolderC inside the FolderB and finally 115 | * the pdf file named "Test-PDF.pdf" will be store inside the FolderB. */ 116 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.SHARE) 117 | /*If you want to save your pdf in shared storage (where other apps can also see your pdf even after the app is uninstall). 118 | * You need to pass an xmt to pdf lifecycle observer by the following method. To get complete overview please see the MainActivity of 'sample' folder */ 119 | .savePDFSharedStorage(xmlToPDFLifecycleObserver) 120 | /* It true then the generated pdf will be shown after generated. */ 121 | .build(new PdfGeneratorListener() { 122 | @Override 123 | public void onFailure(FailureResponse failureResponse) { 124 | super.onFailure(failureResponse); 125 | /* If pdf is not generated by an error then you will findout the reason behind it 126 | * from this FailureResponse. */ 127 | } 128 | @Override 129 | public void onStartPDFGeneration() { 130 | /*When PDF generation begins to start*/ 131 | } 132 | 133 | @Override 134 | public void onFinishPDFGeneration() { 135 | /*When PDF generation is finished*/ 136 | } 137 | 138 | @Override 139 | public void showLog(String log) { 140 | super.showLog(log); 141 | /*It shows logs of events inside the pdf generation process*/ 142 | } 143 | 144 | @Override 145 | public void onSuccess(SuccessResponse response) { 146 | super.onSuccess(response); 147 | /* If PDF is generated successfully then you will find SuccessResponse 148 | * which holds the PdfDocument,File and path (where generated pdf is stored)*/ 149 | 150 | } 151 | }); 152 | ``` 153 | 154 | ### From view IDs : 155 | 156 | ```java 157 | PdfGenerator.getBuilder() 158 | .setContext(context) 159 | .fromViewIDSource() 160 | .fromViewID(R.layout.hostLayout,activity,R.id.tv_print_area,R.id.tv_print_area) 161 | /* "fromViewID()" takes array of view ids and the host layout xml where the view ids are belonging. 162 | * You can also invoke "fromViewIDList()" method here which takes list of view ids instead of array.*/ 163 | .setFileName("Test-PDF") 164 | .setFolderName("Test-PDF-folder") 165 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.OPEN) 166 | .build(new PdfGeneratorListener() { 167 | @Override 168 | public void onFailure(FailureResponse failureResponse) { 169 | super.onFailure(failureResponse); 170 | } 171 | 172 | @Override 173 | public void onStartPDFGeneration() { 174 | /*When PDF generation begins to start*/ 175 | } 176 | 177 | @Override 178 | public void onFinishPDFGeneration() { 179 | /*When PDF generation is finished*/ 180 | } 181 | 182 | @Override 183 | public void showLog(String log) { 184 | super.showLog(log); 185 | } 186 | 187 | @Override 188 | public void onSuccess(SuccessResponse response) { 189 | super.onSuccess(response); 190 | } 191 | }); 192 | ``` 193 | 194 | ### From views: 195 | ( This approach is perfect when you need to change the XML content. You can change the content getting them by ```findViewById``` and change them and finally print them. Other example is [here](https://github.com/Gkemon/Android-XML-to-PDF-Generator/issues/4#issuecomment-712690701) ) 196 | ```java 197 | 198 | 199 | TextView tvText = view.findViewByID(R.id.tv_text_1); 200 | tvText.setText("My changed content"); 201 | //By the following statements, we are changing the text view inside of our target "view" which is going to be changed. 202 | //So if we now print the "view" then you will see the changed text in the pdf. 203 | 204 | PdfGenerator.getBuilder() 205 | .setContext(MainActivity.this) 206 | .fromViewSource() 207 | .fromView(view) 208 | .setFileName("Test-PDF") 209 | .setFolderName("Test-PDF-folder") 210 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.OPEN) 211 | .build(new PdfGeneratorListener() { 212 | @Override 213 | public void onFailure(FailureResponse failureResponse) { 214 | super.onFailure(failureResponse); 215 | } 216 | 217 | @Override 218 | public void showLog(String log) { 219 | super.showLog(log); 220 | } 221 | 222 | @Override 223 | public void onStartPDFGeneration() { 224 | /*When PDF generation begins to start*/ 225 | } 226 | 227 | @Override 228 | public void onFinishPDFGeneration() { 229 | /*When PDF generation is finished*/ 230 | } 231 | 232 | @Override 233 | public void onSuccess(SuccessResponse response) { 234 | super.onSuccess(response); 235 | } 236 | }); 237 | ``` 238 | 239 | ### Multi-paged PDF creation: 240 | 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. 241 | You can insert multiple xml or views object even view id in the parameter of the following methods to create multi-paged pdf: 242 | 243 | 244 | If you want create multi-paged pdf from xmls- 245 | 246 | `.fromLayoutXML(R.layout.layout_1,R.layout.layout_2)` 247 | 248 | If you want create multi-paged pdf from view ids - 249 | 250 | `.fromViewID(activity,R.id.viewId1,R.id.viewId2)` 251 | 252 | If you want create multi-paged pdf from views- 253 | 254 | `.fromViewID(view1,view1)` 255 | 256 | ### How to print an Invoice Or Report ? 257 | 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/master/sample/src/main/java/com/emon/exampleXMLtoPDF/demoInvoice/DemoInvoiceFragment.java) 258 | 259 | 260 | ### How to deal with generated PDF? 261 | 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. 262 | 263 | ### Troubleshoot 264 | * Try to avoid to provide `match_parent` and `wrap_content` height/width in XML. So it specifically. 265 | * 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) 266 | 267 | So if you find any trouble,then you are also welcomed again to knock me.Thank you so much. 268 | 269 | 270 | [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#templates) 271 | 272 |

273 | 274 | linkedin LinkedIn 275 |   276 | 277 | inbox Inbox 278 | 279 |

280 | 281 | #### Logo credit: [kirillmazin](https://www.behance.net/kirillmazin) 282 | 283 | ## ➤ License 284 | 285 | The source code is licensed under the [Apache License 2.0](https://github.com/Gkemon/XML-to-PDF-generator/blob/master/LICENSE). 286 | 287 | 288 | [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#license) 289 | 290 | 291 | ## ➤ App using it 292 | * [Codeddit Programmer Community]( 293 | https://play.google.com/store/apps/details?id=com.codedditapp.codeddit) 294 | * [হাজিরা খাতা - Attendance sheet - HaziraKhata]( 295 | https://play.google.com/store/apps/details?id=com.Teachers.HaziraKhataByGk) 296 | * [Trust Axiata Pay]( 297 | https://play.google.com/store/apps/details?id=com.tad.bdkepler&hl=en&gl=US) 298 | * [Prideboard](https://play.google.com/store/apps/details?id=com.pridesys.prideboard) 299 | 300 | * [Treatbook - Your Health Coach and Medical Tracker]( 301 | https://play.google.com/store/apps/details?id=com.newlit.treatbook&hl=en&gl=US) 302 | 303 | * [Mobflow]( 304 | https://play.google.com/store/apps/details?id=com.brazzo.mobflow) 305 | 306 | * [Front Benchers](https://play.google.com/store/apps/details?id=com.mr_17.front_benchers) 307 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:7.4.2' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | maven { url 'https://jitpack.io' } 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /demo-home-screen-ss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gkemon/Android-XML-to-PDF-Generator/033719775de5b3336f8308beca601dbc365a21fe/demo-home-screen-ss.png -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gkemon/Android-XML-to-PDF-Generator/033719775de5b3336f8308beca601dbc365a21fe/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Feb 13 15:47:55 BDT 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk11 3 | before_install: 4 | - sdk install java 11.0.10-open 5 | - sdk use java 11.0.10-open -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gkemon/Android-XML-to-PDF-Generator/033719775de5b3336f8308beca601dbc365a21fe/logo.png -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 33 5 | defaultConfig { 6 | applicationId "com.emon.XMLtoPDF" 7 | minSdkVersion 21 8 | targetSdkVersion 33 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | compileOptions { 20 | sourceCompatibility JavaVersion.VERSION_11 21 | targetCompatibility JavaVersion.VERSION_11 22 | } 23 | namespace 'com.emon.exampleXMLtoPDF' 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | implementation 'androidx.appcompat:appcompat:1.5.1' 29 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 30 | implementation 'androidx.legacy:legacy-support-v4:1.0.0' 31 | testImplementation 'junit:junit:4.13.2' 32 | androidTestImplementation 'androidx.test:runner:1.5.1' 33 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' 34 | implementation 'com.intuit.ssp:ssp-android:1.1.0' 35 | implementation 'com.intuit.sdp:sdp-android:1.1.0' 36 | implementation 'androidx.recyclerview:recyclerview:1.2.1' 37 | implementation 'com.github.bumptech.glide:glide:4.14.1' 38 | annotationProcessor 'com.github.bumptech.glide:compiler:4.14.1' 39 | /*Comment out the line below if you want to use library locally*/ 40 | implementation project(path: ':xml-to-pdf') 41 | 42 | /*Comment out the line below if you want to use library from jitpack repo*/ 43 | //implementation 'com.github.Gkemon:Android-XML-to-PDF-Generator:2.6.7' 44 | } 45 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/emon/exampleXMLtoPDF/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.emon.exampleXMLtoPDF; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.runner.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.emon.XMLtoPDF", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /sample/src/main/java/com/emon/exampleXMLtoPDF/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.emon.exampleXMLtoPDF; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import androidx.appcompat.app.AlertDialog; 14 | import androidx.appcompat.app.AppCompatActivity; 15 | import androidx.fragment.app.Fragment; 16 | import androidx.recyclerview.widget.LinearLayoutManager; 17 | import androidx.recyclerview.widget.RecyclerView; 18 | 19 | import com.emon.exampleXMLtoPDF.demoInvoice.DemoInvoiceFragment; 20 | import com.emon.exampleXMLtoPDF.dummyList.DummyContent; 21 | import com.emon.exampleXMLtoPDF.dummyList.DummyItemRecyclerViewAdapter; 22 | import com.gkemon.XMLtoPDF.PdfGenerator; 23 | import com.gkemon.XMLtoPDF.PdfGeneratorListener; 24 | import com.gkemon.XMLtoPDF.model.FailureResponse; 25 | import com.gkemon.XMLtoPDF.model.SuccessResponse; 26 | 27 | public class MainActivity extends AppCompatActivity { 28 | Button btnPrint, btnPrintMultiPage, btnInvoice, btnDemoBarcode, btnPrintDemoList, 29 | btnPrintMultiPageDynamic, btnPrintHorizontalScrollView, btnPrintLandscape; 30 | private Fragment demoInvoiceFragment; 31 | private PdfGenerator.XmlToPDFLifecycleObserver xmlToPDFLifecycleObserver; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_main); 37 | btnPrint = findViewById(R.id.bt_print); 38 | btnPrintMultiPage = findViewById(R.id.bt_print_multi_page); 39 | btnInvoice = findViewById(R.id.bt_invoice); 40 | btnDemoBarcode = findViewById(R.id.bt_demo_barcode); 41 | btnPrintDemoList = findViewById(R.id.bt_print_list); 42 | btnPrintMultiPageDynamic = findViewById(R.id.bt_print_multi_page_dynamic); 43 | btnPrintHorizontalScrollView = findViewById(R.id.bt_horizontal_scroll_view); 44 | btnPrintLandscape = findViewById(R.id.bt_print_landscape); 45 | demoInvoiceFragment = new DemoInvoiceFragment(); 46 | 47 | xmlToPDFLifecycleObserver = new PdfGenerator.XmlToPDFLifecycleObserver(this); 48 | getLifecycle().addObserver(xmlToPDFLifecycleObserver); 49 | 50 | 51 | btnPrint.setOnClickListener(new View.OnClickListener() { 52 | @Override 53 | public void onClick(View v) { 54 | 55 | /** 56 | * MUST NEED TO set android:layout_width A FIXED 57 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 58 | * IN PDF FOR DIFFERENT DEVICE SCREEN 59 | */ 60 | 61 | PdfGenerator.getBuilder() 62 | .setContext(MainActivity.this) 63 | .fromViewIDSource() 64 | .fromViewID(MainActivity.this, R.id.tv_print_area) 65 | .setFileName("Demo-Text") 66 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.SHARE) 67 | .savePDFSharedStorage(xmlToPDFLifecycleObserver) 68 | .build(new PdfGeneratorListener() { 69 | @Override 70 | public void onFailure(FailureResponse failureResponse) { 71 | super.onFailure(failureResponse); 72 | } 73 | 74 | @Override 75 | public void onStartPDFGeneration() { 76 | 77 | } 78 | 79 | @Override 80 | public void onFinishPDFGeneration() { 81 | 82 | } 83 | 84 | @Override 85 | public void showLog(String log) { 86 | super.showLog(log); 87 | Log.d("PDF-generation",log); 88 | } 89 | 90 | @Override 91 | public void onSuccess(SuccessResponse response) { 92 | super.onSuccess(response); 93 | } 94 | }); 95 | } 96 | }); 97 | 98 | btnPrintHorizontalScrollView.setOnClickListener(new View.OnClickListener() { 99 | @Override 100 | public void onClick(View v) { 101 | 102 | /** 103 | * MUST NEED TO set android:layout_width A FIXED 104 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 105 | * IN PDF FOR DIFFERENT DEVICE SCREEN 106 | */ 107 | 108 | PdfGenerator.getBuilder() 109 | .setContext(MainActivity.this) 110 | .fromLayoutXMLSource() 111 | .fromLayoutXML(R.layout.layout_print_horizontal_scroll) 112 | .setFileName("Demo-Horizontal-Scroll-View-Text") 113 | .setFolderNameOrPath("MyFolder/MyDemoHorizontalText/") 114 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.NONE) 115 | .build(new PdfGeneratorListener() { 116 | @Override 117 | public void onFailure(FailureResponse failureResponse) { 118 | super.onFailure(failureResponse); 119 | } 120 | 121 | @Override 122 | public void onStartPDFGeneration() { 123 | 124 | } 125 | 126 | @Override 127 | public void onFinishPDFGeneration() { 128 | 129 | } 130 | 131 | @Override 132 | public void showLog(String log) { 133 | super.showLog(log); 134 | } 135 | 136 | @Override 137 | public void onSuccess(SuccessResponse response) { 138 | super.onSuccess(response); 139 | Toast.makeText(MainActivity.this,"Generation is done",Toast.LENGTH_LONG).show(); 140 | } 141 | }); 142 | } 143 | }); 144 | 145 | btnPrintLandscape.setOnClickListener(new View.OnClickListener() { 146 | @Override 147 | public void onClick(View v) { 148 | 149 | /** 150 | * MUST NEED TO set android:layout_width A FIXED 151 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 152 | * IN PDF FOR DIFFERENT DEVICE SCREEN 153 | */ 154 | 155 | PdfGenerator.getBuilder() 156 | .setContext(MainActivity.this) 157 | .fromViewIDSource() 158 | .fromViewID(MainActivity.this, R.id.tv_print_area) 159 | .setFileName("Demo-Landscape") 160 | .setFolderNameOrPath("MyFolder/MyDemoLandscape/") 161 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.SHARE) 162 | .build(new PdfGeneratorListener() { 163 | @Override 164 | public void onFailure(FailureResponse failureResponse) { 165 | super.onFailure(failureResponse); 166 | } 167 | 168 | @Override 169 | public void onStartPDFGeneration() { 170 | 171 | } 172 | 173 | @Override 174 | public void onFinishPDFGeneration() { 175 | 176 | } 177 | 178 | @Override 179 | public void showLog(String log) { 180 | super.showLog(log); 181 | } 182 | 183 | @Override 184 | public void onSuccess(SuccessResponse response) { 185 | super.onSuccess(response); 186 | } 187 | }); 188 | } 189 | }); 190 | 191 | 192 | btnPrintMultiPage.setOnClickListener(new View.OnClickListener() { 193 | @Override 194 | public void onClick(View v) { 195 | 196 | /** 197 | * MUST NEED TO set android:layout_width A FIXED 198 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 199 | * IN PDF FOR DIFFERENT DEVICE SCREEN 200 | */ 201 | 202 | PdfGenerator.getBuilder() 203 | .setContext(MainActivity.this) 204 | .fromViewIDSource() 205 | .fromViewID(MainActivity.this, 206 | R.id.tv_print_area, R.id.tv_print_area, R.id.tv_print_area) 207 | .setFileName("Demo-Text-Multi-Page") 208 | .setFolderNameOrPath("MyFolder/MyDemoTextMultiPage/") 209 | .savePDFSharedStorage(xmlToPDFLifecycleObserver) 210 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.OPEN) 211 | .build(new PdfGeneratorListener() { 212 | @Override 213 | public void onFailure(FailureResponse failureResponse) { 214 | super.onFailure(failureResponse); 215 | } 216 | 217 | @Override 218 | public void onStartPDFGeneration() { 219 | 220 | } 221 | 222 | @Override 223 | public void onFinishPDFGeneration() { 224 | 225 | } 226 | 227 | @Override 228 | public void showLog(String log) { 229 | super.showLog(log); 230 | } 231 | 232 | @Override 233 | public void onSuccess(SuccessResponse response) { 234 | super.onSuccess(response); 235 | } 236 | }); 237 | } 238 | }); 239 | btnPrintMultiPageDynamic.setOnClickListener(new View.OnClickListener() { 240 | @Override 241 | public void onClick(View v) { 242 | final EditText editText = new EditText(v.getContext()); 243 | 244 | new AlertDialog.Builder(v.getContext()) 245 | .setTitle("Input dynamic text") 246 | .setMessage("Please input your demo text. It will populate a multi " + 247 | "paged pdf with your demo text") 248 | .setView(editText) 249 | .setPositiveButton("Input", (dialog, whichButton) -> { 250 | LayoutInflater inflater = (LayoutInflater) 251 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); 252 | 253 | /** 254 | * MUST NEED TO set android:layout_width A FIXED 255 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 256 | * IN PDF FOR DIFFERENT DEVICE SCREEN 257 | */ 258 | View content = inflater.inflate(R.layout.activity_main, null); 259 | TextView tvDynamicText = content.findViewById(R.id.tv_print_area); 260 | tvDynamicText.setText(editText.getText().toString()); 261 | 262 | PdfGenerator.getBuilder() 263 | .setContext(MainActivity.this) 264 | .fromViewSource() 265 | .fromView(tvDynamicText, tvDynamicText, tvDynamicText) 266 | .setFileName("Demo-Text-Multi-Page-With-Dynamic-Text") 267 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.SHARE) 268 | .build(new PdfGeneratorListener() { 269 | @Override 270 | public void onFailure(FailureResponse failureResponse) { 271 | super.onFailure(failureResponse); 272 | } 273 | 274 | @Override 275 | public void onStartPDFGeneration() { 276 | 277 | } 278 | 279 | @Override 280 | public void onFinishPDFGeneration() { 281 | 282 | } 283 | 284 | @Override 285 | public void showLog(String log) { 286 | super.showLog(log); 287 | } 288 | 289 | @Override 290 | public void onSuccess(SuccessResponse response) { 291 | super.onSuccess(response); 292 | } 293 | }); 294 | }) 295 | .setNegativeButton("Cancel", (dialog, whichButton) -> { 296 | dialog.dismiss(); 297 | }) 298 | .show(); 299 | 300 | } 301 | }); 302 | 303 | 304 | btnDemoBarcode.setOnClickListener(new View.OnClickListener() { 305 | @Override 306 | public void onClick(View v) { 307 | LayoutInflater inflater = (LayoutInflater) 308 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); 309 | /** 310 | * MUST NEED TO set android:layout_width A FIXED 311 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 312 | * IN PDF FOR DIFFERENT DEVICE SCREEN 313 | */ 314 | View content = inflater.inflate(R.layout.demo_barcode, null); 315 | PdfGenerator.getBuilder() 316 | .setContext(MainActivity.this) 317 | .fromViewSource() 318 | .fromView(content, content, content) 319 | .setFileName("Demo-Barcode") 320 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.OPEN) 321 | .build(new PdfGeneratorListener() { 322 | @Override 323 | public void onFailure(FailureResponse failureResponse) { 324 | super.onFailure(failureResponse); 325 | } 326 | 327 | @Override 328 | public void onStartPDFGeneration() { 329 | 330 | } 331 | 332 | @Override 333 | public void onFinishPDFGeneration() { 334 | 335 | } 336 | 337 | @Override 338 | public void showLog(String log) { 339 | super.showLog(log); 340 | } 341 | 342 | @Override 343 | public void onSuccess(SuccessResponse response) { 344 | super.onSuccess(response); 345 | } 346 | }); 347 | } 348 | }); 349 | 350 | btnPrintDemoList.setOnClickListener(new View.OnClickListener() { 351 | @Override 352 | public void onClick(View v) { 353 | 354 | LayoutInflater inflater = (LayoutInflater) 355 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); 356 | 357 | View content = inflater.inflate(R.layout.rv_item_list, null); 358 | 359 | RecyclerView recyclerView = content.findViewById(R.id.list); 360 | recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); 361 | recyclerView.setAdapter(new DummyItemRecyclerViewAdapter(DummyContent.ITEMS)); 362 | 363 | PdfGenerator.getBuilder() 364 | .setContext(MainActivity.this) 365 | .fromViewSource() 366 | .fromView(content) 367 | .setFileName("Demo-List") 368 | .setFolderNameOrPath("MyFolder/MyDemoList/") 369 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.OPEN) 370 | .build(new PdfGeneratorListener() { 371 | @Override 372 | public void onFailure(FailureResponse failureResponse) { 373 | super.onFailure(failureResponse); 374 | } 375 | 376 | @Override 377 | public void onStartPDFGeneration() { 378 | 379 | } 380 | 381 | @Override 382 | public void onFinishPDFGeneration() { 383 | 384 | } 385 | 386 | @Override 387 | public void showLog(String log) { 388 | super.showLog(log); 389 | } 390 | 391 | @Override 392 | public void onSuccess(SuccessResponse response) { 393 | super.onSuccess(response); 394 | } 395 | }); 396 | 397 | } 398 | }); 399 | 400 | btnInvoice.setOnClickListener(v -> { 401 | if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) { 402 | getSupportFragmentManager().beginTransaction() 403 | .add(android.R.id.content, demoInvoiceFragment) 404 | .commit(); 405 | } 406 | }); 407 | 408 | } 409 | 410 | @Override 411 | public void onBackPressed() { 412 | getSupportFragmentManager().beginTransaction().remove(demoInvoiceFragment).commit(); 413 | } 414 | 415 | } 416 | -------------------------------------------------------------------------------- /sample/src/main/java/com/emon/exampleXMLtoPDF/demoInvoice/DemoInvoiceFragment.java: -------------------------------------------------------------------------------- 1 | package com.emon.exampleXMLtoPDF.demoInvoice; 2 | 3 | import android.os.Bundle; 4 | 5 | import androidx.fragment.app.Fragment; 6 | import androidx.recyclerview.widget.LinearLayoutManager; 7 | import androidx.recyclerview.widget.RecyclerView; 8 | 9 | import android.text.Html; 10 | import android.util.Log; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.Button; 15 | import android.widget.ImageView; 16 | import android.widget.TextView; 17 | import android.widget.Toast; 18 | 19 | import com.bumptech.glide.Glide; 20 | import com.emon.exampleXMLtoPDF.R; 21 | import com.emon.exampleXMLtoPDF.dummyList.DummyContent; 22 | import com.emon.exampleXMLtoPDF.dummyList.DummyItemRecyclerViewAdapter; 23 | import com.gkemon.XMLtoPDF.PdfGenerator; 24 | import com.gkemon.XMLtoPDF.PdfGeneratorListener; 25 | import com.gkemon.XMLtoPDF.model.FailureResponse; 26 | import com.gkemon.XMLtoPDF.model.SuccessResponse; 27 | 28 | import java.text.DateFormat; 29 | import java.text.SimpleDateFormat; 30 | import java.util.Calendar; 31 | 32 | public class DemoInvoiceFragment extends Fragment { 33 | 34 | private static final String TAG = "InvoiceFragment"; 35 | //invoice views edit items 36 | TextView customer_shop_name_tv, customer_address_tv, customer_phone_tv, 37 | customer_order_date_tv, our_delivery_date_tv; 38 | ImageView ivLogo; 39 | 40 | private View finalInvoiceViewToPrint; 41 | 42 | public DemoInvoiceFragment() { 43 | } 44 | 45 | 46 | /** 47 | * IMPORTANT:: We just need to print a invoice_layout LinearLayout,Not the whole RelativeLayout 48 | * So we need to make a view something like "finalInvoiceViewToPrint" which will be print out ignoring 49 | * the rest part of the root layout. 50 | * 51 | * @param root the main view group which is holding the invoice layout. We need to make a 52 | * final invoice view from it (View root) which will be print out ignoring the rest part. 53 | * @return the final invoice view generating from the root view ignoring rest part 54 | * ( generate_invoice_btn button because we don't want to print it.We just print the main part of 55 | * the invoice) 56 | */ 57 | private View createInvoiceViewFromRootView(View root) { 58 | 59 | finalInvoiceViewToPrint = root.findViewById(R.id.invoice_layout); 60 | RecyclerView invoice_rv = finalInvoiceViewToPrint.findViewById(R.id.invoice_rv); 61 | customer_shop_name_tv = finalInvoiceViewToPrint.findViewById(R.id.customer_shop_name_tv); 62 | customer_address_tv = finalInvoiceViewToPrint.findViewById(R.id.customer_address_tv); 63 | customer_phone_tv = finalInvoiceViewToPrint.findViewById(R.id.customer_phone_tv); 64 | customer_order_date_tv = finalInvoiceViewToPrint.findViewById(R.id.customer_order_date_tv); 65 | our_delivery_date_tv = finalInvoiceViewToPrint.findViewById(R.id.our_delivery_date_tv); 66 | customer_shop_name_tv.setText(Html.fromHtml("Name: Demo shop name")); 67 | customer_address_tv.setText(Html.fromHtml("Address: " + "Demo shop address")); 68 | customer_phone_tv.setText(Html.fromHtml("Cell No: " + "1234567")); 69 | DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 70 | final String orderDate = format.format(1617976800); 71 | final String deliveryDate = format.format(Calendar.getInstance().getTime()); 72 | customer_order_date_tv.setText(Html.fromHtml("Order Date: " + orderDate)); 73 | our_delivery_date_tv.setText(Html.fromHtml("Delivery Date: " + deliveryDate)); 74 | RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false); 75 | invoice_rv.setLayoutManager(layoutManager); 76 | ivLogo=finalInvoiceViewToPrint.findViewById(R.id.iv_logo); 77 | Glide.with(this).load("https://lh6.ggpht.com/9SZhHdv4URtBzRmXpnWxZcYhkgTQurFuuQ8OR7WZ3R7fyTmha77dYkVvcuqMu3DLvMQ=w30").into(ivLogo); 78 | invoice_rv.setAdapter(new DummyItemRecyclerViewAdapter(DummyContent.ITEMS)); 79 | 80 | return finalInvoiceViewToPrint; 81 | } 82 | 83 | @Override 84 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 85 | Bundle savedInstanceState) { 86 | View root = inflater.inflate(R.layout.fragment_demo_invoice, container, false); 87 | Button generate_invoice_btn = root.findViewById(R.id.generate_invoice_btn); 88 | 89 | finalInvoiceViewToPrint = createInvoiceViewFromRootView(root); 90 | 91 | generate_invoice_btn.setOnClickListener(v -> { 92 | generatePdf(); 93 | }); 94 | return root; 95 | } 96 | 97 | public void generatePdf() { 98 | PdfGenerator.getBuilder() 99 | .setContext(requireActivity()) 100 | .fromViewSource() 101 | .fromView(finalInvoiceViewToPrint) 102 | /* "fromLayoutXML()" takes array of layout resources. 103 | * You can also invoke "fromLayoutXMLList()" method here which takes list of layout resources instead of array. */ 104 | /* It takes default page size like A4,A5. You can also set custom page size in pixel 105 | * by calling ".setCustomPageSize(int widthInPX, int heightInPX)" here. */ 106 | .setFileName("demo-invoice") 107 | /* It is file name */ 108 | .setFolderNameOrPath("demo-invoice-folder/") 109 | /* It is folder name. If you set the folder name like this pattern (FolderA/FolderB/FolderC), then 110 | * FolderA creates first.Then FolderB inside FolderB and also FolderC inside the FolderB and finally 111 | * the pdf file named "Test-PDF.pdf" will be store inside the FolderB. */ 112 | .actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.OPEN) 113 | /* It true then the generated pdf will be shown after generated. */ 114 | .build(new PdfGeneratorListener() { 115 | @Override 116 | public void onFailure(FailureResponse failureResponse) { 117 | super.onFailure(failureResponse); 118 | Log.d(TAG, "onFailure: " + failureResponse.getErrorMessage()); 119 | /* If pdf is not generated by an error then you will findout the reason behind it 120 | * from this FailureResponse. */ 121 | //Toast.makeText(MainActivity.this, "Failure : "+failureResponse.getErrorMessage(), Toast.LENGTH_SHORT).show(); 122 | Toast.makeText(getContext(), "" + failureResponse.getErrorMessage(), Toast.LENGTH_SHORT).show(); 123 | } 124 | 125 | @Override 126 | public void showLog(String log) { 127 | super.showLog(log); 128 | Log.d(TAG, "log: " + log); 129 | /*It shows logs of events inside the pdf generation process*/ 130 | } 131 | 132 | @Override 133 | public void onStartPDFGeneration() { 134 | 135 | } 136 | 137 | @Override 138 | public void onFinishPDFGeneration() { 139 | 140 | } 141 | 142 | @Override 143 | public void onSuccess(SuccessResponse response) { 144 | super.onSuccess(response); 145 | /* If PDF is generated successfully then you will find SuccessResponse 146 | * which holds the PdfDocument,File and path (where generated pdf is stored)*/ 147 | //Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show(); 148 | Log.d(TAG, "Success: " + response.getPath()); 149 | 150 | } 151 | }); 152 | 153 | } 154 | 155 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/emon/exampleXMLtoPDF/dummyList/DummyContent.java: -------------------------------------------------------------------------------- 1 | package com.emon.exampleXMLtoPDF.dummyList; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * Helper class for providing sample content for user interfaces created by 10 | * Android template wizards. 11 | *

12 | * TODO: Replace all uses of this class before publishing your app. 13 | */ 14 | public class DummyContent { 15 | 16 | /** 17 | * An array of sample (dummy) items. 18 | */ 19 | public static final List ITEMS = new ArrayList(); 20 | 21 | /** 22 | * A map of sample (dummy) items, by ID. 23 | */ 24 | public static final Map ITEM_MAP = new HashMap(); 25 | 26 | private static final int COUNT = 100; 27 | 28 | static { 29 | // Add some sample items. 30 | for (int i = 1; i <= COUNT; i++) { 31 | addItem(createDummyItem(i)); 32 | } 33 | } 34 | 35 | private static void addItem(DummyItem item) { 36 | ITEMS.add(item); 37 | ITEM_MAP.put(item.id, item); 38 | } 39 | 40 | private static DummyItem createDummyItem(int position) { 41 | return new DummyItem(String.valueOf(position), "Item " + position, makeDetails(position)); 42 | } 43 | 44 | private static String makeDetails(int position) { 45 | StringBuilder builder = new StringBuilder(); 46 | builder.append("Details about Item: ").append(position); 47 | for (int i = 0; i < position; i++) { 48 | builder.append("\nMore details information here."); 49 | } 50 | return builder.toString(); 51 | } 52 | 53 | /** 54 | * A dummy item representing a piece of content. 55 | */ 56 | public static class DummyItem { 57 | public final String id; 58 | public final String content; 59 | public final String details; 60 | 61 | public DummyItem(String id, String content, String details) { 62 | this.id = id; 63 | this.content = content; 64 | this.details = details; 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return content; 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/emon/exampleXMLtoPDF/dummyList/DummyItemRecyclerViewAdapter.java: -------------------------------------------------------------------------------- 1 | package com.emon.exampleXMLtoPDF.dummyList; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.recyclerview.widget.RecyclerView; 5 | 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | 11 | import com.emon.exampleXMLtoPDF.R; 12 | import com.emon.exampleXMLtoPDF.dummyList.DummyContent.DummyItem; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * {@link RecyclerView.Adapter} that can display a {@link DummyItem}. 18 | * TODO: Replace the implementation with code for your data type. 19 | */ 20 | public class DummyItemRecyclerViewAdapter extends 21 | RecyclerView.Adapter { 22 | 23 | private final List mValues; 24 | 25 | public DummyItemRecyclerViewAdapter(List items) { 26 | mValues = items; 27 | } 28 | 29 | @NonNull 30 | @Override 31 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 32 | /** 33 | * MUST NEED TO set android:layout_width A FIXED 34 | * VALUE INSTEAD OF "wrap_content" and "match_parent" OTHERWISE SIZING COULD BE MALFORMED 35 | * IN PDF FOR DIFFERENT DEVICE SCREEN 36 | */ 37 | View view = LayoutInflater.from(parent.getContext()) 38 | .inflate(R.layout.fragment_item, parent, false); 39 | return new ViewHolder(view); 40 | } 41 | 42 | @Override 43 | public void onBindViewHolder(final ViewHolder holder, int position) { 44 | holder.mItem = mValues.get(position); 45 | holder.mIdView.setText(mValues.get(position).id); 46 | holder.mContentView.setText(mValues.get(position).content); 47 | } 48 | 49 | @Override 50 | public int getItemCount() { 51 | return mValues.size(); 52 | } 53 | 54 | public static class ViewHolder extends RecyclerView.ViewHolder { 55 | public final View mView; 56 | public final TextView mIdView; 57 | public final TextView mContentView; 58 | public DummyItem mItem; 59 | 60 | public ViewHolder(View view) { 61 | super(view); 62 | mView = view; 63 | mIdView = (TextView) view.findViewById(R.id.item_number); 64 | mContentView = (TextView) view.findViewById(R.id.content); 65 | } 66 | 67 | @NonNull 68 | @Override 69 | public String toString() { 70 | return super.toString() + " '" + mContentView.getText() + "'"; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 28 | 29 |