使用iText从spring下载restf服务中的pdf

时间:2017-12-15 10:10:15

标签: rest spring-boot itext

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

@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());
}

当我调用其余的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

1 个答案:

答案 0 :(得分:0)

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

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

因此,您需要将MessageConvertor添加到RestTemplate,如以下示例所示:

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;