其余回应传回包含随机字元的pdf

时间:2018-10-24 19:35:00

标签: java rest pdf

@RequestMapping(value = "/merge", method = RequestMethod.POST, produces = "application/pdf")
    public ResponseEntity<?> getPDF(@RequestBody DocRequest req) {

        try {
        ArrayList<String> urls =  pdf.getFileList(req);
        pdf.mergePdf(urls, req); // <-- this will generate a pdf called "untitled.pdf" on the server (outputstream)
        byte[] pdf = Files.readAllBytes(new File("untitled.pdf").toPath());

        HttpHeaders headers = new HttpHeaders();     
        headers.setContentLength(pdf.length);
        headers.set("Content-Disposition", "attachment; filename=untitled.pdf"); 
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        return new ResponseEntity<>(pdf ,headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //if above did not return a proper response, then request is bad.
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

如果我在服务器上打开untitled.pdf,它将看起来不错。但是,当我用招摇的方式下载pdf时,可以看到PDF包含随机字符。         内容图片还可以,但是pdf中的文本看起来像一堆问号和一些随机字符,例如下面的示例

"7SLHZLYLHKILMVYL`V\LSLJ[`V\YPU]LZ[TLU[VW[PVUZ!
     (YH[PVYLX\PYLTLU[VM!T\Z[ILTHPU[HPULK\ZPUNVULPU]LZ[TLU[VW[PVUMYVT,8(;VY(?(7YLTPLY=07MVYL]LY`VUL
    V\[ZPKLT\[\HSM\UKZLSLJ[LKUV[PUJS\KPUN[OL.06VY:[HISL=HS\L-\UK
;OLTH_PT\TU\TI"

为什么或者是什么原因导致这种情况?

更新

当我尝试通过阅读器打开pdf时出现错误,弹出以下消息:

Cannot extract the embedded font 'CMOHAC+Helvetica-Light'. Some characters may not display or print correctly.

另外,当我通过阅读器打开pdf时,如果我单击pdf来打开,我会看到一堆点而不是那些字符。

1 个答案:

答案 0 :(得分:0)

尝试将Content-type标头设置为application / pdf