import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.WritableRaster; import java.io.*; import javax.imageio.*; import javax.swing.*; /** * HTML2JPG,HTML页面转图片的实现方法。 * * @author 老紫竹(Java世纪网,java2000.net) */ public class Test extends JFrame { public Test(String url, File file) throws Exception { JEditorPane editorPane = new JEditorPane(); editorPane.setEditable(false); editorPane.setPage(url); JScrollPane jsp = new JScrollPane(editorPane); getContentPane().add(jsp); this.setLocation(0, 0); this.setVisible(true); // 如果这里不设置可见,则里面的图片等无法截取 // 如果不延时,则图片等可能没有时间下载显示 // 具体的秒数需要根据网速等调整 Thread.sleep(5 * 1000); setSize(10000, 10000); pack(); // BufferedImage image = new BufferedImage(editorPane.getWidth(), // editorPane.getHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = image.createGraphics(); editorPane.paint(graphics2D); BufferedImage image1 = resize(image, 600, 400); ImageIO.write(image1, "jpg", file); dispose(); } public static void main(String[] args) throws Exception { new Test("http://www.google.cn", new File("d:/file.jpg")); } public static BufferedImage resize(BufferedImage source, int targetW, int targetH) { // targetW,targetH分别表示目标长和宽 int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放 // 则将下面的if else语句注释即可 if (sx > sy) { sx = sy; targetW = (int) (sx * source.getWidth()); // } else { // sy = sx; // targetH = (int) (sy * source.getHeight()); } if (type == BufferedImage.TYPE_CUSTOM) { // handmade ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, alphaPremultiplied, null); } else target = new BufferedImage(targetW, targetH, type); Graphics2D g = target.createGraphics(); // smoother than exlax: g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; } } package com.enation.javashop.core.utils; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.WritableRaster; import java.io.File; import java.io.FileWriter; import javax.imageio.ImageIO; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; public class HtmlToJpgUtil extends JFrame { public HtmlToJpgUtil(File file,String url,String html) throws Exception { JEditorPane editorPane = new JEditorPane(); editorPane.setEditable(false); String vNewReportFileName = url; File f = new File(vNewReportFileName); f.createNewFile(); FileWriter fw = new FileWriter(f); fw.write(html); /* fw.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> "); fw.write("<html xmlns='http://www.w3.org/1999/xhtml'> "); fw.write("<head> "); fw.write("<style> "); fw.write("body{font-size:12px; background:#ffffff;} "); fw.write("ol li{line-height:180%;list-style:decimal;margin: 0 0 0 20px;} "); fw.write("td{vertical-align:middle;} "); fw.write("</style> "); fw.write("</head> "); fw.write("<body> "); fw.write("<table width='600' border='0' align='center' cellpadding='20' cellspacing='0' style='border:1px solid #333333; padding:20px;'> "); fw.write(" <tr> "); fw.write(" <td><h3 align='center' style='height:55px; line-height:35px;font-size:25px;font-weight:bolder;'>2012年统计从业资格考试准考证 </h3> "); fw.write("<table width='100%'> <tr>"); fw.write(" <td width='50%' style='float:left;width:300px;'> "); fw.write(" <div style='line-height:24px; width:270px; padding-left:40px; color: #0000ff; font-size: 14px;'><strong>姓 名:</strong>王三<br /> "); fw.write(" <strong>身份证号:</strong>455555555555555555<br /> "); fw.write(" <strong>准考证号:</strong>1211013010108<br /> "); fw.write(" <strong>考试地点:</strong>顺义二中 <br /> "); fw.write(" <strong>考试地址:</strong>顺义二中 <br /> "); fw.write(" </div> "); fw.write(" </td> "); fw.write(" <td width='50%'> "); fw.write(" <img src='http://tongji.7east.com:8080/statics/attachment/goods/201202071558296883_thumbnail.jpg' > "); fw.write(" </td> "); fw.write(" "); fw.write("</tr></table><br>"); fw.write(" <div align='center' style='clear:both;'> "); fw.write(" <h3 style='margin-top:10px;font-size:20px;'>考试安排 </h3> "); fw.write(" <table cellpadding='2' width='95%' cellspacing='1' bgcolor='#000000'> "); fw.write(" "); fw.write(" <tr> "); fw.write(" <td width='238' height='30' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>考试时间 </strong></td> "); fw.write(" <td width='192' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>考试科目 </strong></td> "); fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>考场号 </strong></td> "); fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>座位号 </strong></td> "); fw.write(" </tr> "); fw.write(" <tr> "); fw.write(" <td width='238' align='center' height='25' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;''>2012年02月08日09点30分-11点30分</td> "); fw.write(" <td width='192' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>统计基础知识与统计实务</td> "); fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>第一考场</td> "); fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>08</td> "); fw.write(" </tr> "); fw.write(" <tr> "); fw.write(" <td width='238' align='center' height='25' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;''>2012年02月08日14点00分-16点00分</td> "); fw.write(" <td width='192' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>统计法基础知识</td> "); fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>第一考场</td> "); fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>08</td> "); fw.write(" </tr> "); fw.write(" </table> "); fw.write(" </div> "); fw.write(" <p style='color:#ff0000;width:90%;margin:0 auto;margin-top:8px;' > 注:准考证上没有照片信息或照片信息不清晰的考生,务必在参加考试时携带本人近期二寸正面免冠照片一张交给本考场的监考员。</p> "); fw.write(" "); fw.write(" <p > "); fw.write(" <h3 align='center' style='margin-top:20px;font-size:20px;'>考生注意事项 </h3> "); fw.write(" <ol style='width:90%;margin:0 auto;padding-left:3px;'> "); fw.write(" <li>“准考证”由考生使用A4复印纸在规定时间内上网自行打印。“准考证”涂改无效,背面必须为空白。 </li> "); fw.write(" <li>考生凭“准考证”、有效身份证件(与报名时一致),按规定时间进入考场,对号入座。入座后将上述证件放在桌面左上角,以便检查。</li> "); fw.write(" <li>应试时,考生须携带黑色或蓝色字迹钢笔或签字笔(同一科目答卷的字迹必须是一种颜色)、铅笔(2B)、橡皮。</li> "); fw.write(" <li>严禁将各种电子设备、通讯、计算、储存或其他设备带至座位。</li> "); fw.write(" <li>考试时间以北京时间为准。开考15分钟后不得入场。交卷出场时间不得早于各科目考试结束前30分钟。交卷出场后不得再进场续考,也不得在考场附近逗留或交谈。 </li> "); fw.write(" <li>严禁将答题纸(卡)、试卷、草稿纸带离考场。</li> "); fw.write(" <li>考生要严格遵守考试规则,若有作*行为,将取消考试资格。 </li> "); fw.write(" <li>请考生按照准考证上的指定考试地址,提前确认考点的具体位置和乘车路线。</li></ol></p> "); fw.write(" <h3 align='center' style='margin-top:10px;font-size:20px;'>诚信考试 从我做起 </h3> "); fw.write(" <p style='width:90%;margin:0 auto;margin-bottom:10px;color:#000000;'> 该考生在报名确认及下载打印准考证时均已承诺“本人已周知考场规则和报考者违纪违规处理有关规定;考试当天,保证遵守考场规则;若有违反,愿按相关规定接受处理。” </p></td> "); fw.write(" </tr> "); fw.write("</table> "); fw.write("</body> "); fw.write("</html> "); */ fw.flush(); fw.close(); f = null; editorPane.setPage("file:"+vNewReportFileName); JScrollPane jsp = new JScrollPane(editorPane); getContentPane().add(jsp); this.setLocation(0, 0); this.setVisible(true); // 如果这里不设置可见,则里面的图片等无法截取 // 如果不延时,则图片等可能没有时间下载显示 // 具体的秒数需要根据网速等调整 Thread.sleep(100); setSize(700, 1800); pack(); // BufferedImage image = new BufferedImage(editorPane.getWidth(), // editorPane.getHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(),BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = image.createGraphics(); editorPane.paint(graphics2D); ImageIO.write(image, "jpg", file); dispose(); } public static void main(String[] args) throws Exception { new HtmlToJpgUtil( new File("d:/file.jpg"),"d://temp.html","dddd"); } } public String printImage() { //String imageurl="d://temp.jpg"; //File file=new File(imageurl); String filename = ""; String url=""; if ("2".equals(EopSetting.RUNMODE)) { EopSite site = EopContext.getContext().getCurrentSite(); filename = "/user/" + site.getUserid() + "/" + site.getId() + "/printimage"; } else { filename = "/printimage"; } filename = filename + "/printimage" + com.enation.framework.util.DateUtil.getDateline() + ".jpg"; File file = new File(EopSetting.IMG_SERVER_PATH + filename); if (!file.exists()) file.mkdirs(); url = EopSetting.IMG_SERVER_PATH + com.enation.framework.util.DateUtil.getDateline() + ".html"; String filepath = EopSetting.IMG_SERVER_DOMAIN + filename; // return EopSetting.IMG_SERVER_DOMAIN + filename; // url = EopSetting.IMG_SERVER_DOMAIN + url; try { new HtmlToJpgUtil(file,url,html); } catch (Exception e) { e.printStackTrace(); } System.out.println(EopSetting.IMG_SERVER_DOMAIN + filename); this.json="{'result':0,'message':'"+filepath+"'}"; return this.JSON_MESSAGE; } <script type="text/javascript"> $(function(){ $("#btn_print").click(function(){window.print();}); $("#btn_print_image").click(function(){ var html = "<html>"+$("#print_image").html()+"</html>"; $.ajax({ type : "post", url : "shop/uploadPhoto!printImage.do", data : "html=" + encodeURI(html.replace(new RegExp("tbody", 'g'), "div")) + "&rdm=" + (new Date).getTime(), dataType : "json", success : function(result) { window.open(result.message,'_self'); } }) }); }); </script>