我怎么打印PDF文件?

时间:2017-08-02 12:13:45

标签: javascript spring pdf

我需要在浏览器打印对话框中打开PDF文件。此代码返回PDF,但是当我在浏览器中单击href时,文件保存在磁盘上。我怎样才能改变它?

@RequestMapping(value = "/task/{taskId}/file", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        @ResponseBody
        public CompletableFuture<ResponseEntity<?>> getTaskFile(@PathVariable Long taskId) {
            final String login = SecureUtil.usernameFromSecureContext();
            return CompletableFuture.supplyAsync(() -> {
                Optional<Task> taskOpt = taskService.findOne(taskId, login);

               if (!taskOpt.isPresent()) {
                    return ResponseEntity.notFound().build();
                }

               taskOpt.map((task) -> TaskUtil.getFileId(task).map(ls -> ls.stream().findFirst().get()).get());
                Task task = taskOpt.get();
                Optional<List<String>> fileIdOpts = TaskUtil.getFileId(task);
                if (!fileIdOpts.isPresent()) {
                    return ResponseEntity.notFound().build();
                }

               String filePath = fileIdOpts.map(ls -> ls.stream().findFirst().get()).get();
                FileSystemResource fsr = new FileSystemResource(filePath);

                HttpHeaders headers = new HttpHeaders();
                String fileName = getFileNamePrefix(task.getAnchor()) + task.getId();

                headers.setContentType(new MediaType("application", "pdf"));
                headers.setContentDispositionFormData("attachment", fileName + ".pdf");

               return ResponseEntity.ok().headers(headers).body(fsr);
            }, executorService);
        }

0 个答案:

没有答案