<!-- pdf 相关jar包 -->
<dependency>
<groupID>com.itextpdf</groupID>
<artifactID>itextpdf</artifactID>
<version>5.5.13</version>
</dependency>
<dependency>
<groupID>com.itextpdf.tool</groupID>
<artifactID>xmlworker</artifactID>
<version>5.5.13</version>
</dependency>
public class pdfUtil2 {
//根据HTML文件生成pdf
public static voID parseHTML2pdfByfilePath(String pdffilePath,String HTMLfilePath,String FontPath) {
document document = new document();
pdfWriter writer = null;
fileOutputStream fileOutputStream = null;
fileinputStream fileinputStream = null;
try {
fileOutputStream = new fileOutputStream(pdffilePath);
writer = pdfWriter.getInstance(document,fileOutputStream);
// 设置底部距离60,解决重叠问题
document.setPageSize(PageSize.A4);
document.setmargins(50,45,50,60);
document.setmarginMirroring(false);
document.open();
StringBuffer sb = new StringBuffer();
fileinputStream = new fileinputStream(HTMLfilePath);
BufferedReader br = new BufferedReader(new inputStreamReader(fileinputStream,"UTF-8"));
String readStr = "";
while ((readStr = br.readline()) != null) {
sb.append(readStr);
}
XMLWorkerHelper.getInstance().parsexhtml(writer,document,new ByteArrayinputStream(sb.toString().getBytes("Utf-8")),null,Charset.forname("UTF-8"),new MyFontProvIDer(FontPath));
} catch (Exception e) {
e.printstacktrace();
} finally {
if (null != document) {
document.close();
}
if (null != writer) {
writer.close();
}
if (null != fileinputStream) {
try {
fileinputStream.close();
} catch (IOException e) {
e.printstacktrace();
}
}
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printstacktrace();
}
}
}
}
/** * 根据HTML内容生成pdf * * @param pdffilePath pdf文件存储位置 * @param HTMLcontent HTML内容 * @param FontPath 字体路径 * @throws documentException * @throws IOException */public static voID parseHTML2pdfByString(String pdffilePath,String HTMLcontent,String FontPath) { document document = new document(); pdfWriter writer = null; try { writer = pdfWriter.getInstance(document,new fileOutputStream(pdffilePath)); // 设置底部距离60,解决重叠问题 document.setPageSize(PageSize.A4); document.setmargins(50,60); document.setmarginMirroring(false); document.open(); XMLWorkerHelper.getInstance().parsexhtml(writer,new ByteArrayinputStream(HTMLcontent.getBytes("Utf-8")),new MyFontProvIDer(FontPath)); } catch (Exception e) { e.printstacktrace(); } finally { if (null != document) { document.close(); } if (null != writer) { writer.close(); } }}public static voID main(String[] args) { try { // 本地 String HTMLfile = "D:\1.HTML"; String pdffile = "D:\test2.pdf"; String FontPath = "D:\simsun.ttf"; String HTMLContent = "<HTML><body style=\"Font-size:12.0pt; Font-family:宋体\">" + "<h1>Test</h1><p>测试中文Hello World</p></body></HTML>"; //parseHTML2pdfByString(pdffile,HTMLContent,FontPath); parseHTML2pdfByfilePath(pdffile,HTMLfile,FontPath); } catch (Exception e) { e.printstacktrace(); }}
}
/**
HTML中文字体设置类 @Classname MyFontProvIDer@Description
*/
public class MyFontProvIDer extends XMLWorkerFontProvIDer {
private String FontPath;
public MyFontProvIDer(String filePath) {
this.FontPath = filePath;
}
@OverrIDepublic Font getFont(final String Fontname,final String enCoding,final boolean embedded,final float size,final int style,final Basecolor color) {BaseFont bf = null;try {bf = BaseFont.createFont(FontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMbedDED);} catch (documentException | IOException e) {e.printstacktrace();}Font Font = new Font(bf,size,style,color);Font.setcolor(color);return Font;}}
总结以上是内存溢出为你收集整理的HTML转成pdf全部内容,希望文章能够帮你解决HTML转成pdf所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)