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

将jsp页面转化为图片或pdf(一)(qq:1324981084)的更多相关文章

  1. 将jsp页面转化为图片或pdf升级版(一)(qq:1324981084)

    java高级架构师全套vip教学视频,需要的加我qq1324981084 前面我利用httputil将jsp转化为html,之后转化为pdf,但我发现这样错误率比较高,且成功后有得图片没有完全形成.所 ...

  2. 将jsp页面转化为图片或pdf(一)

    在项目中遇见了将jsp页面转化为pdf的问题,试过itext,但是itext需要标准的html代码,我的页面中的一些属性是itext所不识别的,所以努力了一段时间后就放弃了,后来发现htmlutil抓 ...

  3. 将jsp页面转化为图片或pdf升级版(二)(qq:1324981084)

    java高级架构师全套vip教学视频,需要的加我qq1324981084 上面我们已经将jsp页面转化成html页面了,那么接下来我们的目标是利用这个html页面形成pdf或图片格式.这里我用到的是w ...

  4. JSP 页面中插入图片

    第一步 在 JSP 页面中插入图片 EL 表达式 ${pageContext.request.contextPath } 的值为当前的项目名称 <html> ... <body> ...

  5. 关于springmvc 只能在index.jsp页面显示图片的处理办法jsp页面无法显示图片

    首先,已经配置好了mvc对静态资源的处理 只有index,jsp可以显示图片 其他页面同样的代码则不显示 后来折腾了半天,发现 index是static的父目录的级别文件 可以向下访问 但是其他的js ...

  6. hml页面转化成图片

    <!DOCTYPE html><html><head><meta charset="utf-8"><meta name=&qu ...

  7. Vue使用html2canvas将页面转化为图片

    需求是微信端将页面截屏之后保存到本地,使用了html2canvas插件 先引入插件 npm install --save html2canvas 之后在你所需要使用的页面引入 import html2 ...

  8. 把html页面转化成图片——html2canvas

    test.html <div class="fx_zhezhao"></div> <div class="myImg"> & ...

  9. JSP 页面中用绝对路径显示图片

    首先,图片和工程不在一个盘符下.图片也不能放到工程下.  在JSP 文件中 <img src="E:/图片/1.jpg"/>  这样是引不到图片的.因为,JSP页面在引 ...

随机推荐

  1. DataFrame分组和聚合

    一.分组 1.语法 grouped= df.groupby(by='columns name') # grouped是一个DataFrameGroupBy对象,是可迭代的(遍历) # grouped中 ...

  2. 代码审计之CVE-2018-7600-Drupal远程代码执行漏洞-Render API

    今天学习一下Drupal的另一个漏洞,由于渲染数组不当造成的漏洞 poc: url:http://localhost/drupal-8.5.0/user/register?element_parent ...

  3. 12、python文件的操作

    前言:本文主要介绍python中文件的操作,包括打开文件.读取文件.写入文件.关闭文件以及上下文管理器. 一.打开文件 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处 ...

  4. tf.keras遇见的坑:Output tensors to a Model must be the output of a TensorFlow `Layer`

    经过网上查找,找到了问题所在:在使用keras编程模式是,中间插入了tf.reshape()方法便遇到此问题. 解决办法:对于遇到相同问题的任何人,可以使用keras的Lambda层来包装张量流操作, ...

  5. phpstudy切换的php7.1版本php-cgi报错

    phpstudy切换的php7.1版本php-cgi报错 是因为没有装对应vc的插件. 下载地址:https://www.microsoft.com/zh-CN/download/details.as ...

  6. 当vps服务器被墙,如果用xshell连接

    当然你的被墙了,肯定是访问不了,你得去找一个新的可用的节点去访问,在xshell里面设置代理就能连接上.上图. 然后是两个不同的结点 鼠标放在小火箭上面就能显示

  7. A novel multi-swarm particle swarm optimization with dynamic learning strategy(一种新颖的具有动态学习策略的多种群粒子群优化算法)

    1.核心 在每个子种群的粒子被划分为普通粒子(ordinary particles)和交流粒子(communication particles),在每次迭代过程中,不同的粒子执行不同的进化操作.普通粒 ...

  8. Git详解之其他系统结合

    前言 世界不是完美的.大多数时候,将所有接触到的项目全部转向 Git 是不可能的.有时我们不得不为某个项目使用其他的版本控制系统(VCS, Version Control System ),其中比较常 ...

  9. 如何在网页读取用户IP,操作系统版本等数据demo

    我们浏览网页的时候,会不经意间看到,有些地方(如个人的签名档)显示出了个人的IP,操作系统等数据.借助第三方API和请求报头useragent是很容易实现的. <html> <hea ...

  10. ubuntu 安装LAMP web 服务器, phpmyadmin 安装后无法打开解决

    安装方法: http://blog.chinaunix.net/uid-26495963-id-3173291.html 在上述文档中需要增加apache 支持mysql 功能. apt-get in ...