Shebbi
Shebbi

Reputation: 1

PdfBox, pdf file not being saved when using the program as a JAR file

I'm trying to create a JAVA program that will generate invoices. I am using PDFBox to create PDF files. Everything works fine in the Netbeans, however, when I compile the program into a JAR file, the PDF file doesn't get created. Moreover, as soon as the file is created, it is executed at the same time. everything happens on 1 click. As I mentioned before, it does work in Netbeans but doesn't work when the file is compiled into a JAR file. I am a beginner in JAVA, so I don't really know much about it. An easy and quick solution or idea will be really helpful and highly appreciated. Thanks in advance.

private void createPDF() throws IOException {
        File file = new File("invoice.pdf");
        PDDocument doc = PDDocument.load(file);
        PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
        String logoImage = "logo.jpg";
        try {
            PDXObjectImage imageLogo = new PDJpeg(doc, new FileInputStream(logoImage));


              PDPageContentStream content = new PDPageContentStream(doc, page, true, true);
                content.drawImage(imageLogo, 175, 675);

            content.close();
            File f = new File("D:/invoiceGenerator/invoices/" + invoiceNo + ".pdf");
            FileOutputStream fOut = new FileOutputStream(f);
            try {
                doc.save(fOut);
                JOptionPane.showMessageDialog(null,"file created");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }

            doc.close();
            rowCount = 0;
            String fileName = "D:/invoiceGenerator/invoices/" + invoiceNo + ".pdf";
            try {
                File fileToOpen = new File(fileName);
                if (fileToOpen.exists()) {
                    Desktop.getDesktop().open(fileToOpen);
                } else {
                    JOptionPane.showMessageDialog(null, "File not exits -> " + fileToOpen.getAbsolutePath());
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
    }

Upvotes: 0

Views: 741

Answers (0)

Related Questions