通过window.open下载pdf或直接打开文件
@RestController
@RequestMapping("/print")
public class PrintController {
@GetMapping("/out")
public void out(PrintVo vo, HttpServletResponse response) throws IOException {
response.addHeader("content-disposition", "inline;filename=file.pdf");
response.setContentType("application/pdf");
PrintOutUtil.generatePdf(data, modelPath, outFilePath, response.getOutputStream());
response.getOutputStream().flush();
response.flushBuffer();
}
}
window.open(window.origin + '/dev-api/print/out','_blank')
关键点
- content-disposition 设置
response.addHeader("content-disposition", "attachment;filename=file.pdf")
response.addHeader("content-disposition", "attachment;filename=file.pdf")
- ContentType 设置
ContentType 需要设置成 application/pdf,代表文件类型为pdf