我需要使用以下控制器方法通过 OpenPDF 下载 pdf 作为 Web 服务:
@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);
}
这是导出功能的内容:
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;
}
这里的问题是当我从我的浏览器尝试这个网络服务时,下载只有在我使用 IDM (Internet Download Manager)时才有效,否则它只会在浏览器控制台日志上返回 pdf 的字节码,我尝试了很多东西但没有有效,如果有人可以为我提出解决方案,我会很高兴,谢谢!