showModalDialog("${cpath}/common/popinput.htm",ar,"resizable:yes;scroll:yes;dialogHeight:800px;dialogWidth:900px;center=yes");
//打开对话框显示pdf,这种方式是为了避免在打开pdf时提示保存或打开,直接在ie中打开pdf。
}
// popinput.htm的代码也没什么,代码如下:
<script>
document.write("<frameset rows='0,*' frameborder='NO' border='0' framespacing='0'>");
document.write("<frame src='' name='unvisible'>");
document.write("<frame id='xxx' src="+"'"+window.dialogArguments[0]+"'" + " name='opened'>");
document.write("</frameset>");
var inputObj = window.dialogArguments[1];
</script>
<noframes><body>
</body></noframes>
5、action中就是生成pdf的操作了,这里主要是用了jasperreports,对print_t.jrxml模版进行xml解析,将需要添加的东西添加进去
Action中的主要代码:
else if("print".equals(act)){
final String cpath = request.getParameter("cpath");
String title = request.getParameter("title");
String scale = request.getParameter("scale");
int width = Integer.valueOf(request.getParameter("width"));
int height = Integer.valueOf(request.getParameter("height"));
double mapWidth = Double.valueOf(request.getParameter("mapWidth"));
double mapHeight = Double.valueOf(request.getParameter("mapHeight"));
String imgUrl = request.getParameter("imgUrl").replace("||", "&");
String imgname = request.getParameter("imgname");
String realPath = request.getParameter("realPath");
System.out.println(imgUrl);
SAXBuilder builder=new SAXBuilder(false);
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
return new InputSource(cpath+"/report/jasperreport.dtd");
}
});
String dirPath = cpath+"/moduleFile/print_t.jrxml";
Document doc = builder.build(dirPath);
setPrint(doc, title, scale, width, height, mapWidth, mapHeight, imgUrl,cpath,imgname,dataSet,realPath);
request.setAttribute("document", doc);
request.setAttribute("dataSet", dataSet);
return mapping.findForward("printview");
}
解析xml的代码,BillXML是我们写的一个用来解析xml的类。这个方法的代码其实就是些重复的代码,因为pdf的页面大小和各元素的x,y坐标都不确定,所以这里要根据选的纸张大小来确定他们的长宽,x,y坐标。
private void setPrint(Document doc,String title,String scale,int width,int height,double mapWidth,double mapHeight,String imgUrl,String cpath,String imgname,String dataSet,String realPath) throws Exception{
//set page size
Element root = doc.getRootElement();
root.setAttribute(new Attribute("pageWidth",String.valueOf(width)));//设置整个页面宽
root.setAttribute(new Attribute("pageHeight",String.valueOf(height)));//设置整个页面高
BillXML billXml=new BillXML();
int leftMargin = Integer.valueOf(root.getAttributeValue("leftMargin"));//左边距
int rightMargin = Integer.valueOf(root.getAttributeValue("rightMargin"));//右边距
int topMargin = Integer.valueOf(root.getAttributeValue("topMargin"));//上边距
int bottomMargin = Integer.valueOf(root.getAttributeValue("bottomMargin"));//下边距
root.setAttribute(new Attribute("columnWidth",String.valueOf(Integer.valueOf(width - leftMargin - rightMargin).intValue())));//设置内容显示区域的宽
//p0
Element summary = billXml.selectElement(doc, "summary");
Element su_band = billXml.selectElement(summary, "band");
Element su_image = billXml.selectElement(su_band, "image");
Element su_reportElement = billXml.selectElement(su_image, "reportElement");
Element su_imageExpression = billXml.selectElement(su_image, "imageExpression");
//images
Element pageFooter = billXml.selectElement(doc, "pageFooter");
Element pf_band = billXml.selectElement(pageFooter, "band");
List images = billXml.selectListElement(pf_band, "image");
//north_img
Element north_img = (Element)images.get(1);
Element north_img_reportElement = billXml.selectElement(north_img, "reportElement");
Element north_img_imageExpression = billXml.selectElement(north_img, "imageExpression");
int north_img_w = Integer.valueOf(north_img_reportElement.getAttributeValue("width"));
int north_img_h = Integer.valueOf(north_img_reportElement.getAttributeValue("height"));
//logo_img
Element logo_img = (Element)images.get(0);
Element logo_img_reportElement = billXml.selectElement(logo_img, "reportElement");
Element logo_img_imageExpression = billXml.selectElement(logo_img, "imageExpression");
int logo_img_w = Integer.valueOf(logo_img_reportElement.getAttributeValue("width"));
int logo_img_h = Integer.valueOf(logo_img_reportElement.getAttributeValue("height"));
//staticTexts
List staticTexts = billXml.selectListElement(pf_band, "staticText");
//t1
Element t1 = (Element)staticTexts.get(0);
Element t1_reportElement = billXml.selectElement(t1, "reportElement");
Element t1_text = billXml.selectElement(t1, "text");
int t1_w = Integer.valueOf(t1_reportElement.getAttributeValue("width"));
int t1_h = Integer.valueOf(t1_reportElement.getAttributeValue("height"));
//t2
Element t2 = (Element)staticTexts.get(1);
Element t2_reportElement = billXml.selectElement(t2, "reportElement");
Element t2_text = billXml.selectElement(t2, "text");
int t2_w = Integer.valueOf(t2_reportElement.getAttributeValue("width"));
int t2_h = Integer.valueOf(t2_reportElement.getAttributeValue("height"));
//t3
Element t3 = (Element)staticTexts.get(2);
Element t3_reportElement = billXml.selectElement(t3, "reportElement");
Element t3_text = billXml.selectElement(t3, "text");
int t3_w = Integer.valueOf(t3_reportElement.getAttributeValue("width"));
int t3_h = Integer.valueOf(t3_reportElement.getAttributeValue("height"));
//t4
Element t4 = (Element)staticTexts.get(3);
Element t4_reportElement = billXml.selectElement(t4, "reportElement");
Element t4_text = billXml.selectElement(t4, "text");
int t4_w = Integer.valueOf(t4_reportElement.getAttributeValue("width"));
int t4_h = Integer.valueOf(t4_reportElement.getAttributeValue("height"));
//set summary height
int summary_w = width - leftMargin - rightMargin;
int summary_h = height-(topMargin + bottomMargin + t1_h + north_img_h + logo_img_h);