鍍金池/ 問答/Java  HTML/ pdf.js首次加載pdf文件時找不到pdf文件,刷新后才能出現(xiàn)pdf文件

pdf.js首次加載pdf文件時找不到pdf文件,刷新后才能出現(xiàn)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路徑截圖
圖片描述

文件已存在截圖
圖片描述

第二次打開截圖
圖片描述

回答
編輯回答
陌離殤

你的word轉換成pdf是實時轉換?
如果是,則為什么不考慮提前轉換,因為轉換過程需要時間,可能第一次請求時沒有轉換好啊。

2018年2月6日 07:48
編輯回答
貓小柒

首先,你首次加載的時候,把控制臺點到『Network』,看看請求pdf文件的時候報的什么錯呢。

2018年3月12日 00:20