相关文章推荐
刚分手的牙膏  ·  Linter rule - no ...·  2 年前    · 
善良的稀饭  ·  如何使用 Watchtower ...·  2 年前    · 

1、maven文件下载

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>

<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.0.3</version>
</dependency>

2、重写Breaker.java文件,用于中文换行

新建org.xhtmlrenderer.layout文件夹,新建一个java文件,将以下内容粘贴进去

package org.xhtmlrenderer.layout;

3、html转换pdf代码

/**
* 文件格式转换工具类
*
* @author lz
*
* 2020-07-01 上午10:52:22
*/
public class FileTypeConvertUtil {

/**
* 将HTML转成PD格式的文件。html文件的格式比较严格
* @param htmlFile
* @param pdfFile
* @throws Exception
*/
public static void html2pdf(String content, String pdfFile) throws Exception {
OutputStream os = new FileOutputStream(pdfFile);
ITextRenderer renderer = new ITextRenderer();
StringBuffer html = new StringBuffer();
content = content.replace("宋体", "SimSun");
content = content.replace("黑体", "SimHei");
content = content.replace("微软雅黑", "Microsoft YaHei");
content = content.replace("微软正黑体", "Microsoft JhengHei");
content = content.replace("新宋体", "NSimSun");
content = content.replace("仿宋", "FangSong");
content = content.replace("楷体", "KaiTi");
content = content.replace("仿宋_GB2312", "FangSong_GB2312");
content = content.replace("楷体_GB2312", "KaiTi_GB2312");
content = content.replace("font-family:&quot;", "font-family:SimSun;");
content = content.replace("font-family:&quot;&quot;", "font-family:SimSun;");
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
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("<meta name=\"viewport\" content=\"initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width\"/>")
.append("<link rel=\"stylesheet\" href=\"https://static.loyalvalleycapital.com/web/css/frame.css\"/>")
.append("<style type=\"text/css\" mce_bogus=\"1\">"
+ "body {font-family: 宋体;} "
+ "table { border-collapse: collapse; table-layout: fixed;word-break:break-strict;font-size: 10px; width: 100%;text-align: center;widths:\"50;10;40\"}"
+ "td {word-break:break-all;word-wrap : break-word; } "
+ "@page { size:210mm 297mm;margin: 0.25in; -fs-flow-bottom: \"footer\"; -fs-flow-left: \"left\"; -fs-flow-right: \"right\"; padding: 1em; } "
+ "#footer { font-size: 90%; font-style: italic; position: absolute; top: 0; left: 0; -fs-move-to-flow: \"footer\"; } "
+ "#pagenumber:before { content: counter(page); } #pagecount:before {content: counter(pages); }</style>")
.append("</head>")
.append("<body style = \"font-family: SimSun;\">"
+ "<div style=\"width: 700px;\"><div id=\"footer\" style=\"\">  Page <span id=\"pagenumber\"/> of <span id=\"pagecount\"/> </div>")
.append("<div class='inner' style='width:698px;overflow-x:auto;font-size:14px;color:#333;font-family:微软雅黑;line-height:2;'>");
html.append("<div>"+content+"</div></div></div>");
html.append("</body></html>");

renderer.setDocumentFromString(html.toString());

ITextFontResolver fontResolver = renderer.getFontResolver();
if("linux".equals(getCurrentOperatingSystem())){
fontResolver.addFont("/usr/share/fonts/chiness/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}else{
fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/msyhbd.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/STKAITI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/STLITI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/仿宋_GB2312.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/楷体_GB2312.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}

renderer.layout();
renderer.createPDF(os);
os.close();

System.out.println("create pdf done!!");

public static String getCurrentOperatingSystem(){
String os = System.getProperty("os.name").toLowerCase();
System.out.println("---------当前操作系统是-----------" + os);
return os;
}


public static void main(String[] args) {
String content = "html内容";
String pdfFile = "d:/testoone.pdf";
try {
FileTypeConvertUtil.html2pdf(content, pdfFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

备注:html里面需要添加上一下代码

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\" />")

table的样式需要添加table-layout: fixed;word-break:break-strict;是为了中文代码换行。

1、maven文件下载 &lt;dependency&gt; &lt;groupId&gt;com.itextpdf&lt;/groupId&gt; &lt;artifactId&gt;itextpdf&lt;/artifactId&gt; &lt;version&gt;5.5.9&lt;/version&gt; &lt;/dependency&gt; &...
1、core-renderer.jar该jar包修改过 中文 换行 问题 public static String getConversion Html Code(String linkcss,String html Code,String title){ String css = ""; css +="<style>"; css +="table{"; css +=" border-collapse: collapse;"; css +=" font-size: 15px;"; css +=" width: 98%;"; css +="}"; css +=""; css +="td{"; css +=" border: 1px solid #ddd;"; css +=" text-align: left;"; css +=" white-space: nowrap"; css +="}"; css +="th{"; css +=" border: 1px solid #ddd;"; css +=" text-align: left;"; css +="}"; css +="</style>"; String html = "< html ><head>"+css+"</head><body margin:0;margin-top: 15px;margin-bottom: 15px; text-align: center; font-family:SimSun;\" >"+ title+""+ html Code + "</body></ html >"; System.out.println(" html :"+ html ); return html ;
使用 iText PDF HTML 换为 PDF 时出现不 换行 的问题,可能是因为 HTML 代码中缺少必要的 换行 符或CSS样式设置不正确。解决该问题的方法包括以下几个步骤: 1.调整 HTML 代码使其 包含 必要的 换行 符,这样在 PDF 时自然会 换行 。 2.检查CSS样式设置是否正确,特别是关于文本 换行 的设置,需要指定“white-space: normal”或“word-wrap: break-word”等属性。 3.在 PDF 时使用适当的参数和选项,例如设置“white-space-collapse”选项为false,这样可以确保文本标记之间的空格和 换行 符得到保留。 4.对于特殊情况,可以考虑使用 iText PDF 提供的其他功能,如表格和列表等来实现 换行 效果。 总体来说,解决 iText PDF HTML PDF 换行 的问题需要综合考虑 HTML 文件本身、CSS样式、 iText PDF 换参数和功能等多个方面。通过仔细分析和调试,可以找到最佳的解决方案,实现期望的效果。
Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central 36783 Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central Programmer2333: 解决了,用终结方法。牛逼大佬 Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central Rebas1996: 终于解决了,谢谢!!! Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central weixin_45875072: Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central joyner_zhang: Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central weixin_46323174: 还是不行,崩溃了