js直接下载PDF

1、前端引入js

 

<script type="text/javascript"
            src="${pageContext.request.contextPath}/modules/util/downDoc/html2pdf.js"></script>

2、js方法

html定义div

$("#downPrint").click(function () {
    exportPdf();

});
/*下载Pdf文件*/
function exportPdf() {
    var content = document.getElementById('div_print');
    var options = {
        margin: 10,
        filename: '拟文单.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
    };
    html2pdf().from(content).set(options).save();
}

 3、将附件上传后端的处理方法

/*下载ofd文件*/
    window.WordToPdf =function (formData)  {
        var index = layer.load(1, {offset: ['40%','50%'],shade: [0.3, '#EDEDED']});
        $.ajax({
            url: basePath,
            type: 'POST',
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(data);
                if(data.code==0){
                    window.location.href
                    layer.close(index);
                }else{
                    layer.close(index);
                    layer.msg("文件下载失败,请稍后再试!", {anim: 0, time: 500}, function () {});
                }
            }
        }).fail(function (res) {
            alert("系统错误")
        });
    }

 

@RequestMapping(value = "/WordToPdf", method = {RequestMethod.POST})
    public void WordToPdf(MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        CentitUserDetails cud = getLoginUser(request);
        JSONObject obj = new JSONObject();
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            File files = File.createTempFile(UUID.randomUUID().toString(), "." + filename[1]);
            FileOutputStream outputStream = new FileOutputStream(files);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();

            fileClient = FileUploadUtil.getFileClient();
            FileStoreInfo fileStoreInfo = new FileStoreInfo();
            fileStoreInfo.setFileName("拟文单下载.doc");
            fileStoreInfo = fileClient.uploadFile(fileClient.getHttpClient(), fileStoreInfo, files);
            //删除本地生成的文件
            File fileDelete = new File(files.getPath());
            fileDelete.delete();
            String fileId = fileStoreInfo.getFileId();
            String urlNew = FileChangeUtil.officeToOFDByFileId(fileId);
            System.out.println(urlNew);
            obj.put("ofdUrl", urlNew);
        } catch (Exception e) {
            obj.put("code", 500);
            obj.put("msg", "提交失败!");
            e.printStackTrace();
        }
        JsonResultUtils.writeSingleDataJson(obj, response);
    }

 

posted on 2023-07-24 09:15  IT-QI  阅读(2504)  评论(0编辑  收藏  举报