Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
7.8 kB
1
Indexable
Never
   public String generateExpenseReportInPdf(ExpenseReportRequest expenseReportRequest) {
        Account account = accountUtils.getAccount();
        FIO = "%s %s".formatted(account.getName(), account.getSurname());
        PROFESSION = account.getPosition();
        ID = String.valueOf(account.getId());
        COMPANY_NAME = expenseReportRequest.getCompanyName();
        Account manager = accountRepository.findById(expenseReportRequest.getManagerId()).get();
        MANAGER_FIO = "%s %s".formatted(manager.getName(), manager.getSurname());
        Account admin = accountRepository.findById(1L).get();
        ADMIN_FIO = "%s %s".formatted(admin.getName(), admin.getSurname());

        try {
            String fontPath = "TimesNewRomanRegular.ttf";
            File existingPdf = new File("pathname.pdf");
            PDDocument document = PDDocument.load(existingPdf);

            PDPage firstPage = document.getPage(0);
            PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, PDPageContentStream.AppendMode.APPEND, true, true);

            org.apache.pdfbox.pdmodel.font.PDType0Font font = org.apache.pdfbox.pdmodel.font.PDType0Font.load(document, new File(fontPath));

            contentStream.setFont(font, 10);
            contentStream.beginText();
            contentStream.newLineAtOffset(100, 750);
            contentStream.showText(COMPANY_NAME);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(185, 590);
            contentStream.showText(DIVISION);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(150, 575);
            contentStream.showText(FIO);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(480, 575);
            contentStream.showText(ID);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(150, 550);
            contentStream.showText(PROFESSION);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(430, 550);
            contentStream.showText("Командировка");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(255, 518);
            contentStream.showText(REMAINS);
            contentStream.endText();


            contentStream.beginText();
            contentStream.newLineAtOffset(250, 487);
            contentStream.showText(COUNTER_OF_STOCKS);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(255, 457);
            contentStream.showText("0.00");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(250, 427);
            contentStream.showText(COUNTER_OF_STOCKS);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(250, 412);
            contentStream.showText(SPENT);
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(248, 400);
            contentStream.showText("Остаток");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(240, 385);
            contentStream.showText("Перерасход");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(240, 385);
            contentStream.showText("Перерасход");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(240, 345);
            contentStream.showText("5000р");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(320, 300);
            contentStream.showText("Главный бухгалтер %s".formatted(MANAGER_FIO));
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(320, 300);
            contentStream.showText("Главный бухгалтер %s".formatted(MANAGER_FIO));
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(200, 248);
            contentStream.showText("5000");
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(205, 168);
            contentStream.showText(account.getSurname());
            contentStream.endText();

            contentStream.beginText();
            contentStream.newLineAtOffset(375, 168);
            contentStream.showText("24");
            contentStream.endText();

            contentStream.close();


            PDPage secondPage = document.getPage(1);
            contentStream = new PDPageContentStream(document, secondPage, PDPageContentStream.AppendMode.APPEND, true, true);
            font = org.apache.pdfbox.pdmodel.font.PDType0Font.load(document, new File(fontPath));

            contentStream.setFont(font, 10);

            int y = 730;
            List<ExpenseInfo> infoList = expenseReportRequest.getInfoList();
            Long finalSum = 0L;
            for (int i = 0; i < infoList.size(); i++) {
                contentStream.beginText();
                contentStream.newLineAtOffset(57, y);
                contentStream.showText(String.valueOf(i + 1));
                contentStream.endText();

                contentStream.beginText();
                contentStream.newLineAtOffset(80, y);
                contentStream.showText(String.valueOf(infoList.get(i).getLocalDate()));
                contentStream.endText();

                contentStream.beginText();
                contentStream.newLineAtOffset(200, y);
                contentStream.showText(infoList.get(i).getNameOfExpense());
                contentStream.endText();

                contentStream.beginText();
                contentStream.newLineAtOffset(330, y);
                contentStream.showText(String.valueOf(infoList.get(i).getSum()));
                contentStream.endText();

                contentStream.beginText();
                contentStream.newLineAtOffset(390, y);
                contentStream.showText("-");
                contentStream.endText();

//                contentStream.beginText();
//                contentStream.newLineAtOffset(425, y);
//                contentStream.showText("5800");
//                contentStream.endText();
//
//                contentStream.beginText();
//                contentStream.newLineAtOffset(485, y);
//                contentStream.showText("-");
//                contentStream.endText();

                finalSum += infoList.get(i).getSum();
                y -= 19;
            }

            contentStream.beginText();
            contentStream.newLineAtOffset(300, 425);
            contentStream.showText(FIO);
            contentStream.endText();

            contentStream.close();
            UUID randomUUID = UUID.randomUUID();
            String fileName = randomUUID + ".pdf";
            document.save(fileName);
            document.close();
            return fileName;

        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return "";
    }
Leave a Comment