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(一)的更多相关文章

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

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

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

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

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

    java高级架构师全套vip教学视频,需要的加我qq1324981084 在项目中遇见了将jsp页面转化为pdf的问题,试过itext,但是itext需要标准的html代码,我的页面中的一些属性是it ...

  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. 记录分享公司Spring data相关配置

    起因 公司Spring Data相关的配置还是有点意思的,不过蛮复杂的...XML很多...我不太喜欢强行记住....所以我打算记录一下配置的大致思路和结构,以后可能会有用.... 总体结构 总体结构 ...

  2. 更换域名后的数据库sql的执行命令

    原来域名为trz.lqzcw.com 更改成 www.trzbearing.com UPDATE wp_options SET option_value = replace(option_value, ...

  3. 360浏览器遇到文档模式是IE7的解决办法

    这段时间遇到了360浏览器在加载java项目时,默认的文档模式是IE7,使得网页加载下拉框出现问题. 解决的方法是: 在显示的jsp页面加上 <meta http-equiv="X-U ...

  4. php-fpm优化方法详解

    php-fpm优化方法 php-fpm存在两种方式,一种是直接开启指定数量的php-fpm进程,不再增加或者减少:另一种则是开始时开启一定数量的php-fpm进程,当请求量变大时,动态的增加php-f ...

  5. javascript语言理解

    1.使用jquery remove,无法remove自身标签; 使用标签

  6. BZOJ3522——[Poi2014]Hotel

    1.题意:http://www.lydsy.com/JudgeOnline/problem.php?id=3522 2.分析:这道题有两种情况,第一个是三个点在同一个链上,这显然不可能了,因为树上的路 ...

  7. linux命令crontab

    1.需求 服务端计划任务执行 2.例子 使用crontab命令 参考:http://justjavac.com/other/2013/09/22/linux-scheduled-task-cronta ...

  8. js基础总结

    DOM 节点 document节点  nodeType:9 文本节点  nodeType:3 元素节点  nodeType:1 注释节点  nodeType:8 属性节点  nodeType:2 at ...

  9. C#夯实基础之多线程一:初识多线程

    一. 烧水沏茶问题       在小学四年级有一个烧水沏茶问题,可以作为我们今天讨论话题的引子: 客人来了,要烧一壶茶,但是烧水需要5分钟,洗水壶需要1分钟,洗茶杯需要2分钟,接水需要1分钟,找茶叶需 ...

  10. C#夯实基础之接口(《CLR via C#》读书笔记)

    一. 接口的类型 接口是引用类型.因此从值类型赋值给接口是需要装箱的.如下所示: class Program { static void Main(string[] args) { ISay catS ...