随笔 - 144  文章 - 0  评论 - 2  阅读 - 92190

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   IT-QI  阅读(2989)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示