- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
-
- import org.xhtmlrenderer.pdf.ITextFontResolver;
- import org.xhtmlrenderer.pdf.ITextRenderer;
-
- import com.lowagie.text.pdf.BaseFont;
-
- public class TestFlyingSauser
- {
-
- public static void main( String[] args ) throws Exception
- {
-
- demo_2();
- }
-
-
- public static void demo_1() throws Exception
- {
- String inputFile = "D:/Test/flying.html";
- String url = new File( inputFile ).toURI().toURL().toString();
- String outputFile = "D:/Test/flying.pdf";
- OutputStream os = new FileOutputStream( outputFile );
- ITextRenderer renderer = new ITextRenderer();
- renderer.setDocument( url );
- renderer.layout();
- renderer.createPDF( os );
- os.close();
- }
-
-
- public static void demo_2() throws Exception {
- String outputFile = "G:/demo_3.pdf";
- OutputStream os = new FileOutputStream(outputFile);
- ITextRenderer renderer = new ITextRenderer();
- ITextFontResolver fontResolver = renderer.getFontResolver();
- fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- StringBuffer html = new StringBuffer();
-
- html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
- html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").
- append("<head>")
- .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
- .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")
- .append("</head>")
- .append("<body>");
- html.append("<div>支持中文!</div>");
- html.append("</body></html>");
- renderer.setDocumentFromString(html.toString());
-
-
- renderer.layout();
- renderer.createPDF(os);
- os.close();
- }}
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
-
- import org.w3c.dom.Document;
- import org.xhtmlrenderer.pdf.ITextFontResolver;
- import org.xhtmlrenderer.pdf.ITextRenderer;
-
- public class HTML2PDF {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void htmlToPDF(OutputStream out, String htmlcode, String fontpath,
- String
- fontName, String encoding, String htmlDTDURL) throws Exception {
- DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- Transformer transformer = TransformerFactory.newInstance()
- .newTransformer();
- ITextRenderer renderer = new ITextRenderer();
- ITextFontResolver resolver = renderer.getFontResolver();
- InputStream intream = null;
- Document doc = null;
- Document doc2 = null;
- try {
- htmlcode = this.filterHeader(htmlcode,
-
- encoding, fontName, htmlDTDURL);
-
- intream = new ByteArrayInputStream(htmlcode.getBytes(encoding));
- doc = (Document) builder.parse(intream);
- transformer.setOutputProperty("encoding", encoding);
- new DOMSource(doc);
- transformer.transform(new DOMSource(doc), new StreamResult(bos));
- intream = new ByteArrayInputStream(bos.toString().getBytes());
- doc2 = (Document) builder.parse(intream);
-
- resolver.addFont(fontpath, BaseFont.IDENTITY_H,
- BaseFont.NOT_EMBEDDED);
-
- renderer.setDocument(doc2, null);
- renderer.layout();
-
- renderer.createPDF(out, true);
-
- out.flush();
- out.close();
- } catch (Exception ex) {
-
- throw new Exception(ex.getMessage());
- }
- }
-
-
-
- private String filterHeader(String htmlcode, String encoding,String fontName,String htmlDTDURL) {
- htmlcode="<table style=\"font-family:FangSong_GB2312\" width=\"100%\" height=\"100%\" border=\"0\"><tr><td>"+htmlcode+"</td></tr></table>";
- htmlcode="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \""+htmlDTDURL+"\" >\n"+htmlcode;
- htmlcode = "<?xml version=\"1.0\" encoding=\"" + encoding + "\" ?>\n"
- + htmlcode;
- return htmlcode;
- }
- }
相关文章: