pdf.js首次加载pdf文件时找不到pdf文件,刷新后才能出现pdf文件

前台点击文件名后,后台通过openoffice将doc文件转为pdf格式文件,然后存放在服务器tomcat'中,pdf.js首次加载服务器中tomcat中的转化后pdf文件会显示pdf文件不存在,在第二次刷新后可以成功显示pdf文件。如何能使第一次加载时就显示pdf文件的预览。

<iframe id="product-doc" style="display: none;" src=""></iframe>
function fileView(fileId) {
    $.ajax({
        url: MMBS_IP + '/resources/rest/climate/alarm/showDocOnlineView',
        data: {
            fileId: fileId
        },
        type: 'get',
        async: true,
        beforeSend: ajaxLoading,
        success: function(data) {
            console.log(data);
            var name = data.name.split(".",1)
            if(data.type == "img"){
                var strHtml = "";
                var imgUrl = MMBS_IP + "/resources/rest/getImageStream?imgPath=" + data.name;
                var downLoadPath = '"'+ data.name +'"'
                strHtml += "<button id='img-download' onclick='imgDownload(";
                strHtml += downLoadPath
                strHtml += ")'>下载</button>"
                strHtml += "<img id='product-img' style='width: 100%;height: 100%;' src='"+ imgUrl +"'/>";
                var imgName = data.name.split("/")
                $('#product-win-img').show();
                $('#product-win-img').html(strHtml);
                $('#product-win-img').window({
                    title: imgName[imgName.length-1].split(".",1),
                    width: 1400,
                    height: 800
                });
            }else {
                var path = MMBS_IP + "/resource/generic/web/viewer.html?file=" + data.name
                console.log(path);
                $("#product-doc").attr("src", path);
                $('#product-doc').show();
                $('#product-doc').window({
                    title: name,
                    width: 1400,
                    height: 800
                });
            }
        },
        error: function(xhr, textStatus) {
            $.messager.alert('温馨提示', '访问出错!', 'info');
        },
        complete: ajaxLoadEnd
    });
};
@RequestMapping(value = "showDocOnlineView", method = RequestMethod.GET)
    public @ResponseBody Map<String, Object> showDocOnlineView(HttpServletRequest request, HttpServletResponse response) 
    {
        Map<String,Object> file = FileMgrService.findFileById(Integer.parseInt(request.getParameter("fileId")));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String path = request.getServletContext().getRealPath("/resource/generic/web/" + file.get("display_name").toString() +".pdf");
        String name = "";
        if (file.get("file_type").toString().equals("img")) {
            name = file.get("file_path").toString();
        }else {
            try {
                name = file.get("display_name").toString() +".pdf";
                File inputFile = new File(file.get("file_path").toString());  
                File outputFile = new File(path);  
                DOC2PDFUtil dp = new DOC2PDFUtil(inputFile,outputFile);
                dp.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        resultMap.put("type", (file.get("file_type")));
        resultMap.put("name", name);
        return resultMap;
    }

首次加载失败截图
图片描述

iframe路径截图
图片描述

文件已存在截图
图片描述

第二次打开截图
图片描述

阅读 8.1k
3 个回答

你的word转换成pdf是实时转换?
如果是,则为什么不考虑提前转换,因为转换过程需要时间,可能第一次请求时没有转换好啊。

首先,你首次加载的时候,把控制台点到『Network』,看看请求pdf文件的时候报的什么错呢。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题