将jsp页面转化为图片或pdf(一)(qq:2798641729)
在項(xiàng)目中遇見了將jsp頁面轉(zhuǎn)化為pdf的問題,試過itext,但是itext需要標(biāo)準(zhǔn)的html代碼,我的頁面中的一些屬性是itext所不識(shí)別的,所以努力了一段時(shí)間后就放棄了,后來發(fā)現(xiàn)htmlutil抓取網(wǎng)頁,將jsp頁面轉(zhuǎn)換成html,再將html轉(zhuǎn)化成pdf,問題很容易的解決了。我這里只上傳部分代碼:
jsp轉(zhuǎn)html:
這里用到的技術(shù)是抓取網(wǎng)頁htmlutil,將頁面中的內(nèi)容抓取過來,形成html頁面,這里推薦這篇文章,還是比較好的
http://www.cnblogs.com/luotinghao/p/3800054.html
//filenameTemp 為定義的本地路徑文件File file = new File(filenameTemp); file.createNewFile(); write = new OutputStreamWriter(new FileOutputStream(filenameTemp), "UTF-8"); WebClient webClient = new WebClient(); webClient.getOptions().setJavaScriptEnabled(false);//設(shè)置javascript和css不可用 webClient.getOptions().setCssEnabled(false); //獲得你想要頁面的路徑(網(wǎng)址換成本項(xiàng)目想生成的頁面的請(qǐng)求路徑) HtmlPage page = webClient.getPage("http://localhost:8080/el/eldatamodification/selectsee.do?VERSION_ID=68aa2289f1801f249649f6729f554a59&COM_ID=b1805e8106cdb942fca62ed3798af371"); String str = page.asXml();
//html頭 write.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"+str); write.close();
這樣我們就會(huì)將jsp轉(zhuǎn)化成html,但經(jīng)過我的測(cè)試,jdk1.6好像不支持,或者是不是完全支持,我這里只能用jdk1.7
html轉(zhuǎn)pdf:
NativeInterface.open();SwingUtilities.invokeLater(new Runnable() {public void run() {// SWT組件轉(zhuǎn)Swing組件,不初始化父窗體將無法啟動(dòng)webBrowserJFrame frame = new JFrame("以DJ組件保存指定網(wǎng)頁截圖");// 加載google,最大保存為640x480的截圖 frame.getContentPane().add(newUrlimage("http://localhost:8080/el/"+fileName+".html", //這里是剛才html頁面的請(qǐng)求路徑imgWidth, imgHeight,fileName,path),BorderLayout.CENTER);frame.setSize(800, 600);// 僅初始化,但不顯示 frame.invalidate();frame.pack();frame.setVisible(false);}});NativeInterface.runEventPump(); public class Urlimage extends JPanel {/*** jsp轉(zhuǎn)jpg*/private static final long serialVersionUID = 1L;// 行分隔符final static public String LS = System.getProperty("line.separator", "\n");// 文件分割符final static public String FS = System.getProperty("file.separator", "\\");// 以javascript腳本獲得網(wǎng)頁全屏后大小(建議事先保存網(wǎng)頁的寬高,由于執(zhí)行速度的問題,有時(shí)獲得不到寬高)final static StringBuffer jsDimension;static {jsDimension = new StringBuffer();jsDimension.append("var width = 0;").append(LS);jsDimension.append("var height = 0;").append(LS);jsDimension.append("if(document.documentElement) {").append(LS);jsDimension.append(" width = Math.max(width, document.documentElement.scrollWidth);").append(LS);jsDimension.append(" height = Math.max(height, document.documentElement.scrollHeight);").append(LS);jsDimension.append("}").append(LS);jsDimension.append("if(self.innerWidth) {").append(LS);jsDimension.append(" width = Math.max(width, self.innerWidth);").append(LS);jsDimension.append(" height = Math.max(height, self.innerHeight);").append(LS);jsDimension.append("}").append(LS);jsDimension.append("if(document.body.scrollWidth) {").append(LS);jsDimension.append(" width = Math.max(width, document.body.scrollWidth);").append(LS);jsDimension.append(" height = Math.max(height, document.body.scrollHeight);").append(LS);jsDimension.append("}").append(LS);jsDimension.append("return width + ':' + height;");}public Urlimage(final String url, final int maxWidth, final int maxHeight,final String fileName,final String path) {super(new BorderLayout());JPanel webBrowserPanel = new JPanel(new BorderLayout());final JWebBrowser webBrowser = new JWebBrowser(null);webBrowser.setBarsVisible(false);webBrowser.navigate(url);webBrowserPanel.add(webBrowser, BorderLayout.CENTER);add(webBrowserPanel, BorderLayout.CENTER);JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));webBrowser.addWebBrowserListener(new WebBrowserAdapter() {// 監(jiān)聽加載進(jìn)度public void loadingProgressChanged(WebBrowserEvent e) {// 當(dāng)加載完畢時(shí)if (e.getWebBrowser().getLoadingProgress() == 100) {//建議在這里寫一個(gè)死循環(huán),一直執(zhí)行直到獲得網(wǎng)頁的寬高位置,這里我就不寫程序了
// while(){};String result = (String) webBrowser.executeJavascriptWithResult(jsDimension.toString());//只想js代碼,獲得網(wǎng)頁的寬和高int index = result == null ? -1 : result.indexOf(":");NativeComponent nativeComponent = webBrowser.getNativeComponent();Dimension originalSize = nativeComponent.getSize();Dimension imageSize = new Dimension(Integer.parseInt(result.substring(0, index)),Integer.parseInt(result.substring(index + 1)));imageSize.width = Math.max(originalSize.width, imageSize.width + 50);imageSize.height = Math.max(originalSize.height, imageSize.height + 50);nativeComponent.setSize(imageSize);BufferedImage image = new BufferedImage(imageSize.width, imageSize.height,BufferedImage.TYPE_INT_RGB);//建立一個(gè)寬高顏色固定的圖片容器nativeComponent.paintComponent(image);//繪制圖片,但有時(shí)候會(huì)數(shù)組越界,因?yàn)樽叩氖蔷€程和內(nèi)部拋出異常,所以無法抓住nativeComponent.setSize(originalSize);// 當(dāng)網(wǎng)頁超出目標(biāo)大小時(shí)if (imageSize.width > maxWidth || imageSize.height > maxHeight) {// 截圖部分圖形image = image.getSubimage(0, 0, maxWidth, maxHeight);/** 此部分為使用縮略圖 int width = image.getWidth(), height =* image .getHeight(); AffineTransform tx = new* AffineTransform(); tx.scale((double) maxWidth /* width, (double) maxHeight / height);* AffineTransformOp op = new AffineTransformOp(tx,* AffineTransformOp.TYPE_NEAREST_NEIGHBOR); //縮小 image* = op.filter(image, null);*/}try {// 輸出圖像 // <!-- 需改動(dòng) -->final String fileNameLoc = "d:/huiyou21.jpg";ImageIO.write(image, "jpg", new File(fileNameLoc));//改動(dòng)這里變換格式} catch (IOException ex) {ex.printStackTrace();}}}});add(panel, BorderLayout.SOUTH);} }
這個(gè)方法我認(rèn)為是很好的,執(zhí)行速度一般在一秒到兩秒之間,但是在paintComponent的時(shí)候會(huì)形成全黑或者部分黑的pdf,雖然報(bào)錯(cuò)但是無法抓住,本人建議是隨機(jī)獲得圖片中的一些點(diǎn),來判斷時(shí)候黑色偏多,這樣就可以判斷出來,只是一些建議,希望這篇文章我能夠解決大家的問題,本人還會(huì)繼續(xù)完善這篇文章,本人菜鳥,請(qǐng)大家批評(píng)指點(diǎn),謝謝。
jar包地址: 鏈接:http://pan.baidu.com/s/1boLkYWB 密碼:dhjx
鏈接:http://pan.baidu.com/s/1gf5dXHD 密碼:kod6
?
轉(zhuǎn)載于:https://www.cnblogs.com/ajjava/p/6244817.html
總結(jié)
以上是生活随笔為你收集整理的将jsp页面转化为图片或pdf(一)(qq:2798641729)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [日常训练]变戏法
- 下一篇: Makefile笔记之二------ma