PDDocument issue for PDF doc printing

I am trying to print a document in PDF format using PDDocument to load the file from the byte array, however, PDDocument is not in my project library, I have tried to add it through the IDE - not manually but it has failed. Could you help me resolve this?

below is my sample code for any corrections:

private void printDocument(byte[] documentContent) {
try {
// Create a PDF document from the byte array
PDDocument document = PDDocument.load(documentContent);

        // Get the default print service
        PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();

        // Create a PDF printer job
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        printerJob.setPrintService(defaultPrintService);

        // Set the printable document
        PDFPrintable printable = new PDFPrintable(document);
        printerJob.setPrintable(printable);

        // Show the print dialog to the user 
        if (printerJob.printDialog()) {
            // Perform the printing operation
            printerJob.getPrinter();
        }

        // Close the PDF document
        document.close();
    } catch (IOException | PrinterException e) {
        // Handle any exceptions that occur during printing
        e.printStackTrace();
    }
}

Hi,
Dependencies in CUBA projects should be added as a declaration to build.gradle, to one of modules.
So if your printing logic resides in the core module, then in the build.gradle should be added:


configure(coreModule) {
...
    dependencies {
          compile 'org.apache.pdfbox:pdfbox:2.0.28'
          ...

1 Like