我想在 html、thymeleaf 或其他内容的对象标签或 iframe 标签上显示 PDF 文件,我只想让它工作。 PDF 文件来自数据库。我尝试在 Controller 上使用此代码:
@GetMapping("/document/{cod}")
@ResponseBody
public ResponseEntity getFile(@PathVariable("cod") Integer cod) {
Document document = documentRepository.find(cod);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/pdf"))
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + document.getFileName() + "\"")
.body(document.getFile());
}
但这会打开另一个带有浏览器 PDF 预览的窗口,占据整个页面。那么如何在我搜索的同一页面上显示 pdf 呢?由于我有一个搜索表单,并且我希望在下面显示 PDF,但不更改页面并且仅占据页面的一部分,并且表单将占据页面的另一部分。我正在使用 Spring Boot 和 thymeleaf。
最佳答案
假设您的 UI 代码已经就位并且正在运行
您可以在以下任一元素中加载您的回复。您还可以管理这些元素的大小。
选项 1:
<div>
<object data="test.pdf" type="application/pdf" width="300" height="200">
alt : <a href="test.pdf">test.pdf</a>
</object>
</div>
选项 2:
<div>
<iframe src="/pdf/test.pdf" style="width:100%;height:700px;"></iframe>
</div>
<div>
</div>
选项 3:
<embed src="pdfFiles/interfaces.pdf" width="600" height="500" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html">
关于java - 如何使用Java Spring仅在浏览器的一部分中显示pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091076/