将jsp页面转化为图片或pdf(一)(qq:1324981084)

java高级架构师全套vip教学视频,需要的加我qq1324981084

在项目中遇见了将jsp页面转化为pdf的问题,试过itext,但是itext需要标准的html代码,我的页面中的一些属性是itext所不识别的,所以努力了一段时间后就放弃了,后来发现htmlutil抓取网页,将jsp页面转换成html,再将html转化成pdf,问题很容易的解决了。我这里只上传部分代码:

jsp转html:

这里用到的技术是抓取网页htmlutil,将页面中的内容抓取过来,形成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);//设置javascript和css不可用 webClient.getOptions().setCssEnabled(false); //获得你想要页面的路径(网址换成本项目想生成的页面的请求路径) 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();

这样我们就会将jsp转化成html,但经过我的测试,jdk1.6好像不支持,或者是不是完全支持,我这里只能用jdk1.7

html转pdf:

             NativeInterface.open();
             SwingUtilities.invokeLater(new Runnable() {
             public void run() {
             // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
             JFrame frame = new JFrame("以DJ组件保存指定网页截图");
             // 加载google,最大保存为640x480的截图
             frame.getContentPane().add(
             new
             Urlimage("http://localhost:8080/el/"+fileName+".html",  //这里是刚才html页面的请求路径
                     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转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脚本获得网页全屏后大小(建议事先保存网页的宽高,由于执行速度的问题,有时获得不到宽高)
    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() {

            // 监听加载进度
            public void loadingProgressChanged(WebBrowserEvent e) {
                // 当加载完毕时
                if (e.getWebBrowser().getLoadingProgress() == 100) {
//建议在这里写一个死循环,一直执行直到获得网页的宽高位置,这里我就不写程序了
// while(){}; String result
= (String) webBrowser.executeJavascriptWithResult(jsDimension.toString());//只想js代码,获得网页的宽和高 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);//建立一个宽高颜色固定的图片容器 nativeComponent.paintComponent(image);//绘制图片,但有时候会数组越界,因为走的是线程和内部抛出异常,所以无法抓住 nativeComponent.setSize(originalSize); // 当网页超出目标大小时 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 { // 输出图像 // <!-- 需改动 --> final String fileNameLoc = "d:/huiyou21.jpg"; ImageIO.write(image, "jpg", new File(fileNameLoc));//改动这里变换格式 } catch (IOException ex) { ex.printStackTrace(); } } } } ); add(panel, BorderLayout.SOUTH); } }

这个方法我认为是很好的,执行速度一般在一秒到两秒之间,但是在paintComponent的时候会形成全黑或者部分黑的pdf,虽然报错但是无法抓住,本人建议是随机获得图片中的一些点,来判断时候黑色偏多,这样就可以判断出来,只是一些建议,希望这篇文章我能够解决大家的问题,本人还会继续完善这篇文章,本人菜鸟,请大家批评指点,谢谢。

jar包地址: 链接:http://pan.baidu.com/s/1boLkYWB 密码:dhjx

链接:http://pan.baidu.com/s/1gf5dXHD 密码:kod6

 

posted @ 2017-01-03 14:33  jieya  阅读(8087)  评论(1编辑  收藏  举报