[英]How to display pdf file only in a portion of the browser using Java Spring?
I want to display PDF file on the object tag or iframe tag of the html, thymeleaf or whatever, I just want this to work. 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());
}
但这会打开另一个 window 和浏览器 PDF 预览,占据整个页面。 那么如何在我搜索它的同一页面上显示 pdf? 由于我有一个搜索表单,并且在下面我希望出现 PDF ,但不更改页面并仅占用页面的一部分,而表单将占据页面的另一部分。 我正在使用 spring 引导和 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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.