【问题标题】:Download pdf from restful service with spring using iText使用iText从带有spring的restful服务下载pdf
【发布时间】:2017-12-15 10:10:15
【问题描述】:

我必须下载一个使用 spring 调用 rest api 的 pdf 文件。我的代码如下:

@RequestMapping(value = "downloadPDF", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE)
public @ResponseBody ResponseEntity<InputStreamResource> getPDF() throws IOException {

    ByteArrayInputStream bis = GeneratePdfReport.createReport();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "inline; filename=migration.pdf");


    return ResponseEntity
            .ok()
            .headers(headers)
            .contentType(MediaType.APPLICATION_PDF)
            .body(new InputStreamResource(bis));

}

这是将简单表格打印到文档中的 createReport 方法

public static ByteArrayInputStream createReport() {


    Document document = new Document();
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {

        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(60);
        table.setWidths(new int[]{1, 3, 3});

        Font headFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD);

        PdfPCell hcell;
        hcell = new PdfPCell(new Phrase("Id", headFont));
        hcell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(hcell);

        hcell = new PdfPCell(new Phrase("Name", headFont));
        hcell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(hcell);

        hcell = new PdfPCell(new Phrase("Population", headFont));
        hcell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(hcell);


        PdfWriter.getInstance(document, out);
        document.open();
        document.add(table);

        document.close();

    } catch (DocumentException ex) {

        System.out.println("error");
    }

    return new ByteArrayInputStream(out.toByteArray());
}

当我调用 rest api 并从服务器收到此错误时出现问题

HTTP Status 406 – Not Acceptable

The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation

【问题讨论】:

标签: rest spring-boot itext


【解决方案1】:

来自服务的 406 响应类型意味着服务返回的响应类型未在您的客户端请求的 Accept HTTP 标头中提供。

由于您的服务产生 MediaType.APPLICATION_PDF_VALUE 的响应类型,因此 spring 会寻找 HttpMessageConverter。 Spring 尝试将错误响应转换为 application/pdf,但找不到合适的支持转换为 PDF 的 HttpMessageConverter。

因此您需要向 RestTemplate 添加一个 MessageConvertor,如下例所示:

ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();

List<MediaType> supportedApplicationTypes = new ArrayList<MediaType>();
MediaType pdfApplication = new MediaType("application","pdf");
supportedApplicationTypes.add(pdfApplication);

byteArrayHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(byteArrayHttpMessageConverter);
restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);

Object result = getRestTemplate().getForObject(url, returnClass, parameters);
byte[] resultByteArr = (byte[])result;

【讨论】:

  • 我尝试添加headers.add("Accept", "application/pdf");,但我遇到了同样的错误。
  • 如果我删除 java.lang.IllegalStateException: getOutputStream() has already been called for this response 我有一个例外 java.lang.IllegalStateException: getOutputStream() has already been called for this response
猜你喜欢
  • 1970-01-01
  • 2019-05-15
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多