java實現將PDF轉換爲圖片

/** 具體實現 替換如下路徑 */
private static void parsePdfToHtml(final String pdfBasePath, String pdfName, String htmlBasePath, String uploadBasePath,
			String uploadPath) throws Exception {
		 	StringBuffer buffer = new StringBuffer();
	        FileOutputStream fos;
	        PDDocument document;
	        File pdfFile;
	        int size;
	        BufferedImage image;
	        FileOutputStream out;
	        Long randStr = 0l;
	        //PDF轉換成HTML保存的文件夾
	        String path = htmlBasePath;
	        File htmlsDir = new File(path);
	        if(!htmlsDir.exists()){
	            htmlsDir.mkdirs();
	        }
	        File htmlDir = new File(path+"/");
	        if(!htmlDir.exists()){
	            htmlDir.mkdirs();
	        }
	        try{
	            // 遍歷處理pdf附件
	            randStr = System.currentTimeMillis();
	            buffer.append("<!doctype html>\r\n");
	            buffer.append("<head>\r\n");
	            buffer.append("<meta charset=\"UTF-8\">\r\n");
	            buffer.append("</head>\r\n");
	            buffer.append("<body style=\"background-color:gray;\">\r\n");
	            buffer.append("<style>\r\n");
	            buffer.append("img {background-color:#fff; text-align:center; width:100%; max-width:100%;margin-top:6px;}\r\n");
	            buffer.append("</style>\r\n");
	            document = new PDDocument();
	            // pdf附件
	            pdfFile = new File(pdfBasePath + pdfName);
	            document = PDDocument.load(pdfFile, (String) null);
	            size = document.getNumberOfPages();
	            Long start = System.currentTimeMillis(), end = null;
	            System.out.println("===>pdf : " + pdfFile.getName() +" , size : " + size);
	            PDFRenderer reader = new PDFRenderer(document);
	            final String fileName = System.currentTimeMillis() + "";
	            for(int i=0 ; i < size; i++){
	                //image = new PDFRenderer(document).renderImageWithDPI(i,130,ImageType.RGB);
	                image = reader.renderImage(i, 1.5f);
	                // 生成圖片,保存位置
	                out = new FileOutputStream(path + "/"+ "image" + "_" + i + ".jpg");
	                // 使用png的清晰度
                    ImageIO.write(image, "png", out); 
	                // 將圖片路徑追加到網頁文件裏
	                buffer.append("<img src=\"" + path +"/"+ "image" + "_" + i + ".jpg\"/>\r\n");
	                image = null; out.flush(); out.close();
	            }
	            reader = null;
	            document.close();
	            buffer.append("</body>\r\n");
	            buffer.append("</html>");
	            end = System.currentTimeMillis() - start;
	            System.out.println("===> Reading pdf times: " + (end/1000));
	            start = end = null;
	            // 生成網頁文件
	            fos = new FileOutputStream(path+ pdfName.substring(0,pdfName.lastIndexOf("."))+".html");
	            System.out.println(path+ pdfName.substring(0,pdfName.lastIndexOf("."))+".html");
	            fos.write(buffer.toString().getBytes());
	            fos.flush(); fos.close();
	 
	        }catch(Exception e){
	            System.out.println("===>Reader parse pdf to jpg error : " + e.getMessage());
	            e.printStackTrace();
	        }
	    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章