慕运维1137616
富文本?用iText或者PD4ML直接转换为pdf[code="java"]String outputFile = "D:/Test/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();// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误html.append(""-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");html.append("").append("").append("").append("body {font-family: SimSun;}").append("").append("");//html.append("支持中文!");String str = htmlStream();html.append(str);html.append("");System.out.println(html.toString());renderer.setDocumentFromString(html.toString());renderer.getSharedContext().setBaseURL("file:/E:/workspace/yjsas/WebRoot/images/");renderer.layout();renderer.createPDF(os);os.close();[/code][code="java"]PD4ML pd4ml = new PD4ML();pd4ml.setHtmlWidth(1024);pd4ml.setPageSize(PD4Constants.A4);pd4ml.setPageInsetsMM(new Insets(20, 10, 10, 10));pd4ml.addStyle("BODY {margin: 0; font-family:MSJH}", true);pd4ml.useTTF("java:fonts", true);pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");ByteArrayOutputStream baos = new ByteArrayOutputStream();pd4ml.render(new StringReader(inputHTML), baos);baos.close();File output = new File(outputPDFFile);FileOutputStream fos = new FileOutputStream(output);fos.write(baos.toByteArray());fos.close();