添加了字體,沒有換行問題,沒有<strong>標籤不解析問題。html
jar字體
<dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.0.8</version> </dependency>
html 文件 轉換 PDFurl
String inputFile = "D:/testHtml.html"; String outFile = "F:/outPdf.pdf"; OutputStream os = new FileOutputStream(outFile); ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); String url = new File(inputFile).toURI().toURL().toString(); renderer.setDocument(url); renderer.layout(); renderer.createPDF(os); os.close();
ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); os = new ByteArrayOutputStream(); String htmlstr = "你的HTML字符串"; renderer.setDocumentFromString(htmlstr); renderer.layout(); renderer.createPDF(os);
若是你須要動態替換HTML裏面的數據,推薦使用freemarkerspa
StringTemplateLoader templateLoader = new StringTemplateLoader(); templateLoader.putTemplate("模板,也就是你的HTML", sb.toString()); Configuration config = new Configuration(); config.setTemplateLoader(templateLoader); Template template = config.getTemplate("模板,也就是你的HTML"); StringWriter writer = new StringWriter(); template.process("須要替換數據所對應的參數", writer);
給本身的建議 百度時要用時間排序下 儘可能看時間較近的文章。.net