解決pdf.js路徑問題

ZHOU_VIP發表於2018-06-07

1.現在要做成這種效果


2.點選預覽按鈕,需要傳stuffid


3.用pdf.js,我們發現一個URL帶兩個?號,導致瀏覽器不能正常解析

"${ctx}/pdfjs-1.5.188-dist/web/viewer.html?file=${ctx}/powerruntime/generalOperator!downStuffInfoZW.do?

stuffInfo.stuffid="+stuffid;

這時候我們要用到encodeURIComponent函式


參考:http://www.cnblogs.com/kagome2014/p/kagome2014001.html

4.後臺獲取file,轉換為流



public void downStuffInfoZW() throws IOException {
        //stuffInfo = optProcInfoManager.getStuffById("ebe69e8b4f1c4e6a904a89f0d16c549f");
        stuffInfo = optProcInfoManager.getStuffById(stuffInfo.getStuffid());
        //d:/oa.home/upload/workflowaffix/fw/FW00000000013872/619dede4b8df402faa2b1aa5aa449676.doc
        String stufile = (SysParametersUtils.getWorkflowAffixHome() + stuffInfo.getStuffpath()).replaceAll("\\\\", "/");
        String stufiledir = (SysParametersUtils.getWorkflowAffixHome() + stuffInfo.getStuffpath()).substring(0,stufile.lastIndexOf("/"));
        //619dede4b8df402faa2b1aa5aa449676.doc
        String fileP = stufile.substring(stufile.lastIndexOf("/") + 1, stufile.length());
        File f = new File(stufile);
        EfileStore efileStore = new EfileStore(f, fileP,  stufiledir);
        EfileManager.store(efileStore);
        String converfilename = efileStore.getStoreDir().replaceAll("\\\\", "/")+"/"+efileStore.getStoreName();;//d:/oa.home/upload/workflowaffix/fw/FW00000000013872
        //word轉pdf
        DocConverter d = new DocConverter(converfilename);
        d.conver();
        
        //讀取pdf檔案
        try {
        String filePath = converfilename;
        //d:/oa.home/upload/workflowaffix/fw/FW00000000013872/
        String filePath2 = filePath.substring(0,filePath.lastIndexOf("/") + 1);
        //d:/oa.home/upload/workflowaffix/fw/FW00000000013872/
        String pdfFileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length()).split("\\.")[0];
        //d:/oa.home/upload/workflowaffix/fw/FW00000000013872/af3e50ca013c485cb9b33d50b98b1836.pdf
        String pdfFileNamePath = filePath2+pdfFileName+".pdf";
        File file = new File(pdfFileNamePath);
        byte[] data = null;
            FileInputStream input = new FileInputStream(file);
            data = new byte[input.available()];
            input.read(data);
            response.getOutputStream().write(data);
            input.close();
        } catch (Exception e) {
            log.error("pdf檔案處理異常:" + e.getMessage());
        }

    }

DocConverter.java


package com.centit.powerruntime.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**
 * doc docx格式轉換
 */
public class DocConverter {
	private String fileName;
	private File pdfFile;
	private File swfFile;
	private File docFile;

	public DocConverter(String fileString) {
		ini(fileString);
	}

	/**
	 * 重新設定file
	 * 
	 * @param fileString
	 */
	public void setFile(String fileString) {
		ini(fileString);
	}

	/**
	 * 初始化
	 * 
	 * @param fileString
	 */
	private void ini(String fileString) {
		fileName = fileString.substring(0, fileString.lastIndexOf("."));
		docFile = new File(fileString);
		pdfFile = new File(fileName + ".pdf");
		swfFile = new File(fileName + ".swf");
	}

	/**
	 * 轉為PDF
	 * 
	 * @param file
	 */
	private void doc2pdf() throws Exception {
		if (docFile.exists()) {
			if (!pdfFile.exists()) {
				OpenOfficeConnection connection = new SocketOpenOfficeConnection(
						8100);
				try {
					connection.connect();
					DocumentConverter converter = new OpenOfficeDocumentConverter(
							connection);
					converter.convert(docFile, pdfFile);
					// close the connection
					connection.disconnect();
					System.out.println("****pdf轉換成功,PDF輸出:" + pdfFile.getPath()
							+ "****");
				} catch (java.net.ConnectException e) {
					e.printStackTrace();
					System.out.println("****swf轉換器異常,openoffice服務未啟動!****");
					throw e;
				} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
					e.printStackTrace();
					System.out.println("****swf轉換器異常,讀取轉換檔案失敗****");
					throw e;
				} catch (Exception e) {
					e.printStackTrace();
					throw e;
				}
			} else {
				System.out.println("****已經轉換為pdf,不需要再進行轉化****");
			}
		} else {
			System.out.println("****swf轉換器異常,需要轉換的文件不存在,無法轉換****");
		}
	}

	static String loadStream(InputStream in) throws IOException {

		int ptr = 0;
		in = new BufferedInputStream(in);
		StringBuffer buffer = new StringBuffer();

		while ((ptr = in.read()) != -1) {
			buffer.append((char) ptr);
		}

		return buffer.toString();
	}

	/**
	 * 檢查作業系統型別
	 * 
	 * @return
	 */
	private boolean isWin() {
		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name");
		return os.startsWith("win") || os.startsWith("Win");
	}

	/**
	 * 轉換主方法
	 */
	public boolean conver() {

		if (swfFile.exists()) {
			System.out.println("****swf轉換器開始工作,該檔案已經轉換為swf****");
			return true;
		}

		if (isWin()) {
			System.out.println("****swf轉換器開始工作,當前設定執行環境windows****");
		} else {
			System.out.println("****swf轉換器開始工作,當前設定執行環境linux****");
		}
		try {
			doc2pdf();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		if (swfFile.exists()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 返回檔案路徑
	 * 
	 * @param s
	 */
	public String getswfPath() {
		if (swfFile.exists()) {
			String tempString = swfFile.getPath();
			tempString = tempString.replaceAll("\\\\", "/");
			return tempString;
		} else {
			return "";
		}

	}

	/**
	 * 
	 * @return
	 */
	public String getPdfPath() {
		if (pdfFile.exists()) {
			String tempString = pdfFile.getPath();
			tempString = tempString.replaceAll("\\\\", "/");
			return tempString;
		} else {
			return "";
		}
	}

	/**
	 * 設定輸出路徑
	 */
	public void setOutputPath(String outputPath) {
		if (!outputPath.equals("")) {
			String realName = fileName.substring(fileName.lastIndexOf("/"),
					fileName.lastIndexOf("."));
			if (outputPath.charAt(outputPath.length()) == '/') {
				swfFile = new File(outputPath + realName + ".swf");
			} else {
				swfFile = new File(outputPath + realName + ".swf");
			}
		}
	}

}

5.pom.xml中的jar包


6.


參考:https://www.cnblogs.com/vijayblog/p/6126335.html


相關文章