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

最新文章

  1. C文件读写
  2. IOS开发基础知识--碎片30
  3. Asp.net WebApi Put模式调用,“HTTP 错误 405.0 - Method Not Allowed”解决方法
  4. ElasticSearch入门系列(七)搜索
  5. 兼容iOS 10:配置获取隐私数据权限声明
  6. Gruntfile.js
  7. Vim,一个开放源代码的文本编辑器(转)
  8. IIS连接数
  9. Cracking the coding interview-String
  10. 云服务和虚拟机的预留 IP 地址
  11. iis配置PHP
  12. HADOOP源码分析之RPC(1)
  13. linux-mv
  14. Java中的java.math.BigInteger
  15. Back To Top
  16. developer的996,需要谁来拯救
  17. 【Redis】3、Redis集群部署
  18. 介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码
  19. 5. MYSQL问题:Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password:YES)
  20. CodeForces 1073F Choosing Two Paths

热门文章

  1. 【Java】增强的for流程
  2. lcov和gcov的使用错误
  3. 【ZOJ 3929】Deque and Balls(普通dp)
  4. sqlserver 2005 数据误删恢复
  5. Lua在单片机中的移植
  6. linux 安装maven
  7. Java实现比较版本号
  8. WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日
  9. kvm虚拟机静态和动态迁移
  10. CentOS7 睡眠 休眠 关机 电源