import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.net.MalformedURLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
public class ToPdf {
public static void main(String[] args) throws DocumentException, IOException, ParserConfigurationException, SAXException {
htmlToPdf();
htmlCodeToPdf();
//有个bug就是如果html展示内容的宽度太宽的话,生成PDF会展示不全,不知道怎么解决呢??如果解决了请留个言啊
//有个问题要说明根据html代码生成pdf的时候经常会报一个utf-8的错误,这个时候要把html文件的编码改成GB2312,另外中文乱码问题请
//参考文章http://blog.csdn.net/jimmy609/article/details/12748053
}
//直接根据html文件生成pdf
public static void htmlToPdf() throws DocumentException, IOException{
String inputFile = "e://index.html";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "e://firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
//直接根据html代码文件生成pdf
public static void htmlCodeToPdf() throws DocumentException, IOException, ParserConfigurationException, SAXException{
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("<div class='strt-wrap' id='strtWrap'>"
+"<div class='strt-block'>"
+" <div class='strt-part'>"
+" <span class='strt-name'>阿斯达斯大</span>"
+" <div class='line-v'><span></span></div>"
+" <div class='strt-block'>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-r'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>阿斯达斯大</span>"
+" <div class='line-v'><span></span></div>"
+" <div class='strt-block'>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-l'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>阿斯达斯大</span>"
+" </div>"
+" </div>"
+" </div>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-c'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>bbb</span>"
+" <div class='line-v'><span></span></div>"
+" <div class='strt-block'>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-r'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>HTC</span>"
+" <div class='line-v'><span></span></div>"
+" <div class='strt-block'>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-r'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>One X</span>"
+" </div>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-c'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>G8</span>"
+" </div>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-l'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>Butterfly</span>"
+" </div>"
+" </div>"
+" </div>"
+" <div class='strt-part'>"
+" <span class='line-h line-h-c'></span>"
+" <div class='line-v'><span></span></div>"
+" <span class='strt-name'>cc</span>"
+" </div>"
+" </div>"
+" </div>"
+" </div>"
+" </div>"
+" </div>"
+" </div>");
// 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 = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
String outputFile = "e://100bottles.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页