Open Christiandrossel opened 9 months ago
Export Services für excel (xls), csv und pdf implementiert TODO:
Check Permissions: To implement PDF generation in your Android Kotlin app using Apache PDFBox, follow these steps:
build.gradle
(Module: app) file:dependencies {
implementation 'org.apache.pdfbox:pdfbox:2.0.29' // Use the latest version available
}
Product
objects as input and output the PDF file.Here's an example function that creates a simple PDF with product details:
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.pdmodel.PDPageContentStream
import org.apache.pdfbox.pdmodel.font.PDType1Font
fun generatePDF(products: List<Product>) {
try {
// Create a new PDF document
val document = PDDocument()
// Create a new page
val page = PDPage()
document.addPage(page)
// Create a new content stream which will "hold" the to be created content
val contentStream = PDPageContentStream(document, page)
// Define a font
val font = PDType1Font.HELVETICA
// Set the font size
contentStream.setFont(font, 12)
// Start writing the PDF content
var yPosition = page.mediaBox.height - 50
products.forEach { product ->
val barcodeText = product.barcode ?: "No Barcode"
val nameText = product.name ?: "No Name"
val quantityText = "Quantity: ${product.quantity}"
contentStream.beginText()
contentStream.newLineAtOffset(50, yPosition)
contentStream.showText("Barcode: $barcodeText")
contentStream.newLineAtOffset(0, -20)
contentStream.showText("Name: $nameText")
contentStream.newLineAtOffset(0, -20)
contentStream.showText(quantityText)
contentStream.endText()
yPosition -= 50 // Move down for next product
}
// Close the content stream
contentStream.close()
// Save the document
val file = File(Environment.getExternalStorageDirectory(), "Products.pdf")
document.save(file)
// Close the document
document.close()
// Notify user or show a toast that the PDF has been created
Toast.makeText(this, "PDF Created", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
e.printStackTrace()
Toast.makeText(this, "Error Creating PDF", Toast.LENGTH_SHORT).show()
}
}
private const val REQUEST_CODE_PERMISSIONS = 100
fun checkAndRequestPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_CODE_PERMISSIONS)
} else {
generatePDF(listOf(Product())) // Call your generatePDF function with actual data
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
generatePDF(listOf(Product())) // Call your generatePDF function with actual data
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
generatePDF()
with the list of Product
objects you want to include in the PDF.Remember to handle exceptions properly and inform the user if something goes wrong during the PDF creation process. Also, ensure that you handle runtime permissions correctly since Android requires explicit user consent for writing to external storage [1][2].
Es ist möglich das alle daten als Dokument zu Exportieren Entweder als Excel oder PDF was besser geht
Dazu muss es ein Menüpunkt in den drei Punkten geben Ein Dialogfenster sollte sich öffnen mit möglichen (zukünftigen Einstellungen) Im Dialogfenster soll erstmal ein Text stehen Das die Daten in das Format exportiert werden. und zwei Buttons zum Exportieren und zum Abbrechen