-1

i need to download a pdf with OpenPDF using the following controller method as a web service :

@CrossOrigin
    @ApiOperation(value = "Generate Customer Request PDF ")
    @PostMapping(path = { "/customer/generatePDF" }, 
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_PDF_VALUE)
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful"),
            @ApiResponse(code = 400, message = "Bad Request, Validation Errors, ...", response = BadRequest.class),
            @ApiResponse(code = 401, message = "Bad Request, Validation Errors, ...", response = Unauthorized.class),
            @ApiResponse(code = 500, message = "Internal server error", response = InternalError.class) })
    public ResponseEntity<?> generateCustomerRequestPDF(@RequestBody CustomerRequestDto CustomersRequestDto,
                                                        HttpServletResponse response) throws BusinessException, DocumentException, IOException, ParseException, TransformerException{
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        
        // create PDF object
        List<CustomerRequestPDF> requests = new ArrayList<CustomerRequestPDF>();
        
        CustomerRequestPDF c = new CustomerRequestPDF();
        
        c.setName(CustomersRequestDto.getCusName());
        c.setAmount(CustomersRequestDto.getAmount()+" "+CustomersRequestDto.getCurrencySymbol());
        c.setPaymentDate( CustomersRequestDto.getCompleteAt().format(formatter) );
        c.setPaymentType(CustomersRequestDto.getOperationTypeName());
        c.setPaymentReference(CustomersRequestDto.getRrn());
        c.setPaymentNature(CustomersRequestDto.getGroupStatus());
        
        requests.add(c);
        
        CustomerPDFExporter exporter = new CustomerPDFExporter(requests);
        
        // create http response as pdf 
        String headerKey = "Content-Disposition";
        String headerValue = "attachment; filename=recu_" + 
                             c.getPaymentReference() + "_"+ LocalDateTime.now() + ".pdf";
        
        response.setContentType("application/pdf");
        response.setHeader(headerKey, headerValue);
        
        exporter.export(response);
        
        return new ResponseEntity<>("Generation du recu", HttpStatus.OK);
        
       
        
    }

and here is the content of export function :

public Document export(HttpServletResponse response) throws DocumentException, IOException {
        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, response.getOutputStream());
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
         
        document.open();
        Font font = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
        font.setSize(25);
        font.setColor(Color.BLUE);
        
        // titre du recu 
        Paragraph p = new Paragraph("Recu du paiement client", font);
        p.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(p);
        
        // date du recu
        font.setSize(12);
        font.setColor(Color.BLACK);
        Paragraph p2 = new Paragraph("Recu le : "+LocalDateTime.now().format(formatter), font);
        p2.setAlignment(Paragraph.ALIGN_LEFT);
        p2.setSpacingBefore(50);
        document.add(p2);
        

        String nature = "";
        String nom = "";
        String montant = "";
        String date = "";
        String type = "";
        String rrn = "";
        for(CustomerRequestPDF c : requests) {
            if(c.getPaymentNature().equals("L")) {
                nature = "Paiement effectué en masse";
            }
            else {
                nature = "Paiement effectué individuel";
            }
            nom = c.getName();
            montant = c.getAmount();
            date = c.getPaymentDate();
            type = c.getPaymentType();
            rrn = c.getPaymentReference();
        }
        
        Paragraph p3 = new Paragraph(nature, font);
        p3.setAlignment(Paragraph.ALIGN_LEFT);
        document.add(p3);
        
        // informations du paiement
        font.setSize(13);
        Paragraph p4 = new Paragraph("Nom Client    :    "+nom, font);
        p4.setSpacingBefore(50);
        document.add(p4);
        Paragraph p5 = new Paragraph("Montant    :    "+montant, font);
        document.add(p5);
        Paragraph p6 = new Paragraph("Date Paiement    :    "+date, font);
        document.add(p6);
        Paragraph p7 = new Paragraph("Nature Paiement    :    "+type, font);
        document.add(p7);
        Paragraph p8 = new Paragraph("Reference Paiement    :    "+rrn, font);
        document.add(p8);
        
        document.close();
        return document;
         
    }

the problem here is when i try this web service from my browser , the download only works if i use IDM ( Internet Download Manager ) , otherwhise it only returns the byte code of the pdf on the browser console log , i tried many things but nothing works, i ll be glad if someone can suggest a solution for me , Thank you !

1 Answer 1

-1

I found the solution , it was simply that the web service's client should precise the response type as array buffer , In my case it was VueJS , so i added the following on axios call :

axios.post(SERVER_URL+"/customer/generatePDF",customerRequest,{responseType: 'arraybuffer'})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.