前台点击文件名后,后台通过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是实时转换?
如果是,则为什么不考虑提前转换,因为转换过程需要时间,可能第一次请求时没有转换好啊。