1. 程式人生 > >生成pdf並展示出來

生成pdf並展示出來

前端請求程式碼:

function preview(custcode,tempId){
		  layer.open({
	 	        type: 2,
	 	        title: '簡歷預覽',
	 	        maxmin: true,
	 			scrollbar: false,
	 	        shadeClose: true, //點選遮罩關閉層
	 	        area : ['800px' , '600px'],
	 	        content: '<%=path %>/demandFollow/previewWordsByCode.do?custcode='+custcode+'&tempId='+tempId,
	 	       /* success: function(layero, index) {
 		        	var body = layer.getChildFrame('html', index);
 		            $(body).height("600px");
 		            layer.iframeAuto(index);
 			     } */
	 	    })
			
	  }

後臺處理:

@RequestMapping("/previewWordsByCode")
public ModelAndView previewWordsByCode(@RequestParam(value = "custcode", defaultValue = "") String custcode,
@RequestParam(value = "tempId", defaultValue = "0") Integer tempId, HttpServletRequest request,
			HttpServletResponse response) {
			ModelAndView mv = new ModelAndView("pdf2");
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("custcode", custcode);
		paramMap.put("tempId", tempId);
		String url = personalinMapper.selresumeUrlByMap(paramMap);
		String contextPath = request.getSession().getServletContext().getRealPath("/");
		response.setContentType("text/html;charset=UTF-8");
		// 呼叫已流的方式傳輸檔案的方法
		String fullPath = "";
		fullPath = contextPath + url;
		if (new File(fullPath).exists() == false) {
			mv.addObject("msg", "使用者還沒有簡歷<br>" + fullPath);
			mv.addObject("status", 1);
			} else {// 如果有這個檔案先轉換
			String savePath = fullPath.substring(0, fullPath.lastIndexOf(".")) + ".pdf";
			if (new File(savePath).exists() == false) {
				WordPdfUtil.doc2pdf(fullPath, savePath);
			}
			url = url.replace("\\", "/");
			url = url.substring(0, url.lastIndexOf(".")) + ".pdf";
			mv.addObject("msg", url);
			mv.addObject("status", 0);
		}
		return mv;
	}

工具類:

package com.shzqoa.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
 
import org.aspectj.weaver.ast.Test;
 
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
public class WordPdfUtil {
public static void doc2pdf(String inPath, String outPath) {
 try {
            //long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一個空白pdf文件
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是將要被轉化的word文件
            doc.save(os, SaveFormat.PDF);// 全面支援DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                         // EPUB, XPS, SWF 相互轉換
                                         //long now = System.currentTimeMillis();
            //System.out.println(outPath+"生成共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時
            if (os != null) {
            	os.close();
    		}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

所需jar包: aspose-words-15.8.0-jdk16.jar

將pdf反映到jsp頁面上:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<title>Insert title here</title>
</head>
<body>
<c:if test="${status==0 }">
<iframe name="resource" src="<%=path %>${msg }" width="800px"   height="600px" scrolling="yes" frameborder="0"></iframe>
</c:if>
<c:if test="${status==1 }">
	<h3>還沒有生成簡歷</h3>
</c:if>
</body>
</html>