itext使用html标签生成pdf文件
2017-08-10 14:03
537 查看
使用itext5通过html文本生成pdf文件
需要jar包
<!-- itext5 start -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<!-- itext5 end -->
package com.gehouse.smartdevice.controller;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class Itext5HtmlToPDF {
public static void main(String[] args) {
new Itext5HtmlToPDF().createPdf();
}
public void createPdf() {
try {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font font = new Font(bfChinese, 12, Font.NORMAL);
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter mPdfWriter = PdfWriter.getInstance(document, new FileOutputStream("C://Users/miju/Desktop/htmltest.pdf"));
document.open();
document.add(new Paragraph("创建pdf文件.支持中文......", font));
String s = getHtml();
ByteArrayInputStream bin = new ByteArrayInputStream(s.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(mPdfWriter, document, bin, Charset.forName("UTF-8"), new ChinaFontProvide());
document.close();
mPdfWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getHtml() {
StringBuffer html = new StringBuffer();
html.append("<div style='color:green;font-size:20px;'>你好世界!hello world !</div>");
html.append("<span style='color:red'>what are you 弄啥咧!</span>");
html.append("<h1>标题</h1>");
html.append("<table>");
html.append("<tr>");
html.append("<th>序号</th>");
html.append("<th>用户名</th>");
html.append("<th>性别</th>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>1</td>");
html.append("<td>fengxing</td>");
html.append("<td>男</td>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>2</td>");
html.append("<td>admin</td>");
html.append("<td>女</td>");
html.append("</tr>");
html.append("</table>");
return html.toString();
}
/**
*
* 提供中文
*
*/
public static final class ChinaFontProvide implements FontProvider {
@Override
public Font getFont(String arg0, String arg1, boolean arg2, float arg3,
int arg4, BaseColor arg5) {
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
return FontChinese;
}
@Override
public boolean isRegistered(String arg0) {
return false;
}
}
}
参考:http://blog.csdn.net/a1215656324/article/details/40346887
需要jar包
<!-- itext5 start -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<!-- itext5 end -->
package com.gehouse.smartdevice.controller;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class Itext5HtmlToPDF {
public static void main(String[] args) {
new Itext5HtmlToPDF().createPdf();
}
public void createPdf() {
try {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font font = new Font(bfChinese, 12, Font.NORMAL);
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter mPdfWriter = PdfWriter.getInstance(document, new FileOutputStream("C://Users/miju/Desktop/htmltest.pdf"));
document.open();
document.add(new Paragraph("创建pdf文件.支持中文......", font));
String s = getHtml();
ByteArrayInputStream bin = new ByteArrayInputStream(s.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(mPdfWriter, document, bin, Charset.forName("UTF-8"), new ChinaFontProvide());
document.close();
mPdfWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getHtml() {
StringBuffer html = new StringBuffer();
html.append("<div style='color:green;font-size:20px;'>你好世界!hello world !</div>");
html.append("<span style='color:red'>what are you 弄啥咧!</span>");
html.append("<h1>标题</h1>");
html.append("<table>");
html.append("<tr>");
html.append("<th>序号</th>");
html.append("<th>用户名</th>");
html.append("<th>性别</th>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>1</td>");
html.append("<td>fengxing</td>");
html.append("<td>男</td>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>2</td>");
html.append("<td>admin</td>");
html.append("<td>女</td>");
html.append("</tr>");
html.append("</table>");
return html.toString();
}
/**
*
* 提供中文
*
*/
public static final class ChinaFontProvide implements FontProvider {
@Override
public Font getFont(String arg0, String arg1, boolean arg2, float arg3,
int arg4, BaseColor arg5) {
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
return FontChinese;
}
@Override
public boolean isRegistered(String arg0) {
return false;
}
}
}
参考:http://blog.csdn.net/a1215656324/article/details/40346887
相关文章推荐
- 使用freemaker和itext从html模板生成pdf文件
- java使用IText生成PDF格式文件
- itext使用模板生成pdf文件
- 使用iText 将html页面转PDF文件(itext+freemarker)
- 使用flying-saucer,利用HTML来生成PDF文件(裴东辉)
- 滴水穿石--Java 生成PDF文件--iText使用之插入图片和中文乱码
- iTEXT生成PDF文件使用方法 实例 示例 Servlet输出PDF文档方法
- 【Java】使用iText生成PDF文件
- itext xmlworker转换html生成pdf文件
- 【Java】maven工程使用iText生成PDF文件
- 使用itextpdf 将HTML转成PDF文件,并做电子签章
- 滴水穿石--Java 生成PDF文件--iText使用之入门
- 使用iText生成pdf文件
- java 使用itextpdf.jar和itext-asian.jar根据pdf模板填写数据后生成一个新的pdf文件
- Android使用iText生成pdf文件并读取pdf内容
- 使用ITEXT生成pdf文件,使用POI、jxl对excel文件进行读取
- 从零开始学习使用iText PDF(1):生成第一个PDF文件
- 使用itext组件生成PDF文件
- 使用iText生成PDF文件
- Java使用Freemarker和iText生成PDF文件