我的环境:前端: thymeleaf 后台springmvc 前端代码: 引入相关包: <script th:src="@{/assets/js/pdf/pdfobject.js}"></script>
html容器:
<div id="pdfDiv" style="height:600px;width:1200px">
</div>
js调用:
window.onload = function (){
var mid=getUrlParam("mid");
var success = new PDFObject({ url: "http://localhost:9080/ymlib/administrator/manuscriptManagement/getPdf?mid="+mid}).embed("pdfDiv");
}
控制层代码:
@RequestMapping(value = "/getPdf", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getPdf(@RequestParam(value = "mid") String mid) {
Manuscript manuscript=manuscriptRepository.findOne(mid);
Query query = new Query(Criteria.where("_id").is(new ObjectId(manuscript.getFileId())));
GridFSDBFile file_check = gridFsOps.findOne(query);
InputStream in = file_check.getInputStream();
return ResponseEntity.ok().contentLength(file_check.getLength())
.cacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS)).contentType(MediaType.valueOf("application/pdf"))
.body(new InputStreamResource(in));
}
效果图如下:

|