java实现html页面转pdf解决方案
java实现html页面转pdf解决方案
篇一:Java实现HTML代码生成PDF文档 1、IText实现html2pdf,速度快,纠错能力差,支持中文(要求HTML使用unicode编码),但中支持一种中文字体,开源。 2、Flying Sauser实现html2pdf,纠错能力差,支持多种中文字体(部分样式不能识别),开源。 3、PD4ML实现html2pdf,速度快,纠错能力强,支持多种中文字体,商业。 (一)IText 官网:/ 测试案例: 依赖jar包:、(支持中文) 下面只是一个小的测试案例,如果项目中使用到了该组件可以参考API完成项目组中相应的功能! [c-sharp] view plaincopyprint? 1. import ; 2. import ; 3. import ; 4. import ; 5. import ; 6. import ; 7. import ; 8. import ; 9. import ; 10. import ; 11. import ; 12. import ; 13. public class TestIText{ 14. public static void main(String[] args) { 15. TestIText ih = new TestIText(); 16. ("D://Test//", "D://Test//iText_"); 17. ("Hello中文", "D://Test//iText_"); 18. } 19. 20. public void htmlCodeComeFromFile(String filePath, String pdfPath) { 21. Document document = new Document(); 22. try { 23. StyleSheet st = new StyleSheet(); 24. ("body", "leading", "16,0"); 25. (document, new FileOutputStream(pdfPath)); 26. (); 27. ArrayList p = (new FileReader(filePath), st); 28. for(int k = 0; k 29. ((Element)(k)); 30. } 31. (); 32. ("文档创建成功"); 33. }catch(Exception e) { 34. (); 35. } 36. } 37. 38. public void htmlCodeComeString(String htmlCode, String pdfPath) { 39. Document doc = new Document(); 40. try { 41. (doc, new FileOutputStream(pdfPath)); 42. (); 43. // 解决中文问题 44. BaseFont bfChinese = ("STSong-Light", "UniGB-UCS2-H", _EMBEDDED); 45. Font FontChinese = new Font(bfChinese, 12, ); 46. Paragraph t = new Paragraph(htmlCode, FontChinese); 47. (t); 48. 49. 50. 51. 52. 53. 54. (); ("文档创建成功"); }catch(Exception e) { (); } } } 1. import ; 2. import ; 3. import ; 4. 5. import ; 6. import tRenderer; 7. 8. import ; 9. 10. public class TestFlyingSauser { 11. public static void main(String[]