PDFCreatorAndroid
Simple library to generate and view PDF in Android.
A simple library to create and view PDF with zero dependency Or native code.
Add it in your root build.gradle at the end of repositories:
Step 2. Add the dependency
Try It
Concept
- Android has capability to print documents to PDF, this library simplifies those API's to generate PDF easily.
- At basic level, API renders Views to PDF. To create A PDF with pages we need to submit views exactly height of one page, any view larges then that that will be trimmed.
- This library creates pages by adding views to a parent view unitil the next view is about to exceed current page. If next view exceeds current page, that view will be added to new page.
Implementation
- First thing first, Look at MainActivity of app.
- PDF creater uses views which can be rendered, So we need to exted an activity in order to create activity.
- Create a Empty
Activity
without any layout and extend it withPDFCreatorActivity
. Do not set usesetContentView(int resourceId)
inside your created activity. - There are 3 abstract methods you have to override.
getHeaderView()
- This will be header for PDF and will be added to each page. (Use PDFHeaderView if )
getBodyViews()
- This will return a PDFBody which consist of list of views which can be broken between pages.
onNextClicked()
- This is a handler method to get callback when user taps on Next.
- In
onCreate
of you activity, you have to callcreatePDF(String fileName, PDFUtilListener listener)
. It will generate PDF and give you a PDF file in callback (if success). After receiving callback you can close activity and do whatever you need to do with PDF. - This library also provides
PDFUtil.pdfToBitmap(File pdfFile)
method to get image preview of all pages of sepcified PDF file.
Available Views
TextView
->PDFTextView
VerticalView
->PDFVerticalView
HorizontalView
->PDFHorizontalView
ImageView
->PDFImageView
TableView
->PDFTableView
Saperator
->PDFLineSaperatorView
Advanced
If you cannot find some methods of a View
Class inside PDFView
class you can get view by calling pdfView.getView()
on any available PDFView class and then update view properties.
For example Android TextView
support setting html to TextView which is not available in PDFTextView
, to do that see example below:
Another example, Set gravity to View
Advanced, Proceed with caution :warning:
This is a unfinished feature, Use only for basic cases [After using this feature you cannot add child view to your custom view].
If you want to add a custom view to PDF (such as chart or icon), you just can create your own like this:
Example:
An example is created, Look at MainActivity of app.
VIEWS
- PDFVerticalView
- PDFHorizontalView
- PDFTextView
- PDFImageView
- PDFTableView
- PDFLineSeparatorView