This library uses iText internally to generate PDF file. Custom layout has been implemented to get the look and purpose of an invoice
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the gradle dependency to your app
module build.gradle
file:
dependencies {
implementation 'com.github.kariot:pdf-invoice-generator:1.0.0'
}
Before generating an invoice, permission to READ/WRITE external storage should be granted by the user to save the generated PDF File
The invoice has been splitted into different portions for the convinience to provide data. The different data source. The splitup is shown in the image below
Create an instance of ModelInvoiceHeader
to provide data for header.
eg:
val headerData = ModelInvoiceHeader(
"Phone Number",
"Email Address",
"Web Site",
ModelInvoiceHeader.ModelAddress
)
Icon for invoice doesn't need any data class. It can be provided directly via method call while creating PDF file.
The address shown in header has to be provided with this model class. Create an object of ModelInvoiceHeader.ModelAddress
and provide it as the last parameter for ModelInvoiceHeader
object that we created above
The additional details in invoice such as customer name, invoice number,total price,etc.. has to be provided with ModelInvoiceInfo
object.
eg:
val invoiceInfo = ModelInvoiceInfo(
ModelInvoiceInfo.ModelCustomerInfo,
"Invoice Number",
"Invoice Date",
"Invoice Amount"
)
an object of ModelInvoiceInfo.ModelCustomerInfo
has to be provided as parameter as it provides the customer information
val customerInfo =
ModelInvoiceInfo.ModelCustomerInfo(
"Customer name",
"Address Line 1",
"Address Line 2",
"Address Line 3"
)
The header for table has to be provided via ModelTableHeader
class
eg:
val tableHeader =
ModelTableHeader(
Header1,
Header2,
Header3,
Header4,
Header5
)
The data for the table is provided through list of ModelInvoiceItem
eg:
val tableData = ModelInvoiceItem(
"Item 1",
"Item 1 Description",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
)
Three values has to be provided via ModelInvoicePriceInfo
object
eg:
val invoicePriceInfo = ModelInvoicePriceInfo(
"Sub Total",
"Tax Total",
"Grand Total"
)
The footer has a single text message shown to provide the same provide an instance of ModelInvoiceFooter
eg:
val footerData = ModelInvoiceFooter("Footer message")
To generate a PDF invoice create an instance of InvoiceGenerator
pass required data parameters.
eg:
val pdfGenerator = InvoiceGenerator(this).apply {
setInvoiceLogo(R.drawable.invoice_icon) // to set invoice logo
setCurrency(currency) //to set invoice currency
setInvoiceColor(invoiceColor) //to set invoice color(HEX CODE)
setInvoiceHeaderData(headerData) //data for header
setInvoiceInfo(invoiceInfo) //header info data
setInvoiceTableHeaderDataSource(tableHeader) //data for table header
setInvoiceTableData(
/* List of data for invoice table */
) //data for table
setPriceInfoData(invoicePriceInfo) //data for price info
setInvoiceFooterData(footerData) //data for footer
}
val fileUri = pdfGenerator.generatePDF("FILE_NAME.pdf") // returns pdf file Uri