Christiandrossel / WarehouseAndInventory

With this Android app it should be possible to record the product by scanning a barcode. This allows a list of inventory levels to be quickly recorded and exported as an Excel table.
GNU General Public License v3.0
0 stars 0 forks source link

Export der daten als Dokument #5

Open Christiandrossel opened 9 months ago

Christiandrossel commented 9 months ago

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

Christiandrossel commented 9 months ago

Export Services für excel (xls), csv und pdf implementiert TODO:

Christiandrossel commented 9 months ago
Christiandrossel commented 9 months ago

Check Permissions: To implement PDF generation in your Android Kotlin app using Apache PDFBox, follow these steps:

  1. Add Apache PDFBox to Your Project: First, you need to include the Apache PDFBox library in your project. Add the dependency to your build.gradle (Module: app) file:
dependencies {
    implementation 'org.apache.pdfbox:pdfbox:2.0.29' // Use the latest version available
}
  1. Create a Function to Generate PDF: Next, create a function in your activity or fragment where you will generate the PDF. The function should take a list of 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()
    }
}
  1. Request Permissions: Make sure your app has the necessary permissions to write to external storage. Request these permissions at runtime if needed. Here's an example of how to check and request permissions:
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()
        }
    }
}
  1. Call the Function: Finally, call 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].

https://www.phind.com/search?cache=j1xhvdbrtramzwyxsxrwamsf