執法文書打印的實現(word^png的實現)
寫這篇博客,心里很忐忑和沉重。由于知識結構等多方面的原因,redhat上啟動openoffice環境浪費了許多時間和精力。一些驗證通過的命令、方法等稍微改變就慌手慌腳卡了進度,如果有得選擇,我應該不會用openoffice實現。畢竟我的興趣和預職業方向是jee和前端,做這些工作有點偏了,實在提不起來勁頭。
這部分的代碼都是網上抄的,大部分都一樣,算是固定格式,沒有多少個人發揮的余地。Openoffice實現word to pdf,icepdf實現pdf to png。Icepdf是收費軟件,使用有風險,轉換出的圖片效果確實比較驚艷,也可以自由調整精度。
Openoffice的安裝與服務啟動
下載安裝包:http://www.openoffice.org/download/index.html
在redhat下解壓安裝文件:tar -zxvf 壓縮文件名.tar.gz 解壓后的文件只能放在當前目錄
安裝依賴項:cd zh-CN/RPMS/ rpm –ivh *.rpm
安裝桌面:cd desktop-integration/ rpm -ivh openoffice4.1.1-redhat-menus-4.1.1-9775.noarch.rpm
進入安裝目錄:cd /opt/openoffice4/PRogram/
啟動服務:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard & (注意:這里有個坑,命令最后的"&"符最好添加,上星期kill 掉soffice.bin進程后一直啟動不起來,原因就是沒有添加"&",不知道什么原因,希望知道的園友能解惑)
查看監聽是否創建成功:netstat -apn|grep 8100
卸載:rpm -e `rpm -qa |grep openoffice` `rpm -qa |grep ooobasis`
具體word→png:java代碼
/** * * @param sourceName doc文件名 * @param zoom 圖片清晰度 * @return * @throws Exception */ public String[] opt2png(String sourceName,float zoom) throws Exception { //圖片磁盤絕對路徑:可以作為方法的返回值 String imgFilePaths []; //每次生成一個隨機的pdf,每天凌晨啟動一個自動任務清空 pdfFile目錄 File inputFile = new File(wordFilePath+File.separator+sourceName); if (!inputFile.exists()) { //return -1;// 找不到源文件, 則返回-1 System.out.println("找不到word文件"+wordFilePath+File.separator+sourceName); return null; } //生成隨機的pdf文件 String pdfname=UUID.randomUUID().toString(); pdfFilePath=pdfFilePath+File.separator+pdfname+".pdf"; File outputFile = new File(pdfFilePath); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } //啟動服務:不是很好用,需要手動啟動 String command =officeHome+ "program"+File.separator+"soffice -headless -accept=/"socket,host=127.0.0.1,port=8100;urp;/""; Process pro=null; try { pro = Runtime.getRuntime().exec(command); } catch (IOException e) { e.printStackTrace(); } //建立連接 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (Exception e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } DocumentConverter converter = new OpenOfficeDocumentConverter(connection); try { converter.convert(inputFile, outputFile); //這個方法的執行不可調試 } catch (Exception e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); }finally{ // close the connection connection.disconnect(); // 關閉OpenOffice服務的進程 pro.destroy(); //生成pdf成功 } //icepdf work Document document = null; float rotation = 0f; document = new Document(); document.setFile(pdfFilePath); //獲得文檔的頁數 int pageNum=document.getNumberOfPages(); imgFilePaths=new String[pageNum]; String imgFileName; for (int i = 0; i < pageNum; i++) { //下面這行的代碼執行不可調試 BufferedImage img = (BufferedImage) document.getPageImage(i,GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation,zoom); Iterator iter = ImageIO.getImageWritersBySuffix("png"); ImageWriter writer = (ImageWriter) iter.next(); imgFileName=UUID.randomUUID().toString(); File outFile = new File(pngFilePath+File.separator+imgFileName+".png"); FileOutputStream out = new FileOutputStream(outFile); ImageOutputStream outImage = ImageIO.createImageOutputStream(out); writer.setOutput(outImage); writer.write(new IIOImage(img, null, null)); imgFilePaths[i]=pngFilePath+File.separator+imgFileName+".png"; } return imgFilePaths; }新聞熱點
疑難解答