简体   繁体   中英

Spring Java : PDF download only works with idm

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 !

我找到了解决方案,只是 Web 服务的客户端应该将响应类型精确为数组缓冲区,就我而言,它是 VueJS,所以我在 axios 调用中添加了以下内容:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM