java代码
StringBuffer buf = new StringBuffer();
buf.append("<?xml version='1.0' encoding='gb2312'?> <html>");
// put in some style
buf.append("<head><style language='text/css'>");
buf.append("body{font:12px/1.8 '宋体';position:relative;}"
+" html,body{height:100%;overflow-y:hidden;margin:0;padding:0;}"
+" .strt-wrap{width:100000px;margin:10px;cursor:move;}"
+" .strt-part{text-align:center;float:left;position:relative;}"
+" .strt-part .line-v{position:relative;height:40px;width:100%;}"
+" .strt-part .line-v span{display:block;background:#ccc;position:absolute;top:0;font-size:0;line-height:1px;width:1px;height:40px;left:50%;}"
+" .strt-name{display:inline-block;padding:0 5px;height:24px;line-height:24px;border:1px solid #ccc;margin:0 10px;border-radius:3px;background:#f8f8f8;}"
+" .strt-part .line-h{height:1px;display:block;background:#ccc;position:absolute;top:0;font-size:0;}"
+" .strt-part .line-h-l{width:50%;left:0;}"
+" .strt-part .line-h-c{width:100%;left:0;}"
+" .strt-part .line-h-r{width:50%;right:0;}"
+" .strt-block{float:left;}</style></head>");
// generate the body
buf.append("<body>");
buf.append(str);
// for(int i=99; i>0; i--) {
// buf.append("<h3>"+i+" 哈哈bottles of beer on the wall, "
// + i + " bottles of beer!</h3>");
// buf.append("<p>Take one down and pass it around, "
// + (i-1) + " bottles of beer on the wall</p>\n");
// }
// buf.append("<h2>No asdesa阿斯顿 of beer on the wall, no more bottles of beer. ");
// buf.append("Go to the store and buy some more, 99 bottles of beer on the wall.</h2>");
buf.append("</body>");
buf.append("</html>");
System.out.println("htmlString"+buf.toString());
// parse the markup into an xml Document
DocumentBuilder builder = null;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(new StringBufferInputStream(buf.toString()));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
String outputFile = "e://100bottles.pdf";
OutputStream os = null;
try {
os = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
renderer.layout();
try {
renderer.createPDF(os);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
html,body{height:100%;overflow-y:hidden;
看到没?超过100%被隐藏了。 去掉那些css就可以了。