(spring boot) can't generate pdf using openPdf library

22 views Asked by At

i created a pdf generator function using openpdf library , and created an end point to generate the pdf , but the problem is that it doesn't get generated even tough i get an ok status , i tried to debugge the code , but everything is okey . this is the pdf generator functions i created :

    private final List<Inscription> inscriptionList;


    private void writeTableHeader(PdfPTable table) {
        PdfPCell cell = new PdfPCell();
        cell.setBackgroundColor(Color.BLUE);
        cell.setPadding(5);

        Font font = FontFactory.getFont(FontFactory.HELVETICA);
        font.setColor(Color.WHITE);

        cell.setPhrase(new Phrase("Student Cin", font));

        table.addCell(cell);

        cell.setPhrase(new Phrase("Student Cne", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("Last Name", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("First Name", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("E-mail", font));
        table.addCell(cell);
    }

    private void writeTableData(PdfPTable table) {
        for (Inscription user : inscriptionList) {
            table.addCell(user.getCin());
            table.addCell(user.getCne());
            table.addCell(user.getNom());
            table.addCell(user.getPrenom());
            table.addCell(user.getEmail());
        }
    }

    public void export(HttpServletResponse response,String title) throws DocumentException, IOException {
        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, response.getOutputStream());

        document.open();
        Font font = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
        font.setSize(18);
        font.setColor(Color.BLUE);

        Paragraph p = new Paragraph(title, font);
        p.setAlignment(Paragraph.ALIGN_CENTER);

        document.add(p);

        PdfPTable table = new PdfPTable(5);
        table.setWidthPercentage(100f);
        table.setWidths(new float[] {1.5f, 3.5f, 3.0f, 3.0f, 1.5f});
        table.setSpacingBefore(10);

        writeTableHeader(table);
        writeTableData(table);

        document.add(table);

        document.close();

    }

and the end point :

    @GetMapping("/pdf/all/{reference}")
    public void getAllChoixByConcourAllPdf(@PathVariable("reference") String reference,HttpServletResponse response) throws IOException {
        List<Inscription> inscriptions = choixService.findByConcours(Concour.builder().reference(reference).build());
        generatePDF(response,inscriptions,"Preselection List for "+reference);
    }

    public void generatePDF(HttpServletResponse response, List<Inscription> inscriptions,String title)throws DocumentException, IOException {
        response.setContentType("application/pdf");
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        String currentDateTime = dateFormatter.format(new Date());

        String headerKey = "Content-Disposition";
        String headerValue = "attachment; filename=student_" + currentDateTime + ".pdf";
        response.setHeader(headerKey, headerValue);

        studentPdf exporter = new studentPdf(inscriptions);
        exporter.export(response,title);
    }

please help

0

There are 0 answers