Откройте файл Pdf на веб-странице в Spring boot
@GetMapping(value = "/pdf",produces= MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<InputStreamResource> getTermsConditions() throws Exception {
String filePath = "C:\\";
String fileName = "PDF.pdf";
File file = new File(filePath+fileName);
HttpHeaders headers = new HttpHeaders();
// headers.add("content-disposition", "inline;filename=" +fileName);
headers.add("Content-Disposition", "attachment; filename=" + fileName);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(resource);
}
Файл открыт, но формат неверный.
6 0 obj << / Type / ExtGState / BM / Normal / ca 1 >> endobj 7 0 obj << / Type / ExtGState / BM / Normal / CA 1 >> endobj 10 0 obj << / Filter / FlateDecode / Длина 156465 / Длина eP} &֓
Пожалуйста помогите. Я хочу открыть книгу в формате pdf в браузере, например в онлайн-библиотеке
1 ответ
Использование значения «attachment» для заголовка ответа «Content-Disposition» загрузит PDF-файл. Вместо того, чтобы отображать его в новой вкладке.
Значение «inline» для заголовка ответа «Content-Disposition» откроет PDF-файл в новой вкладке браузера.
Например:
v1
headers.add("Content-Disposition", "inline;filename=PDF.pdf");
v2
headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=PDF.pdf");
Вы можете просмотреть: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition