itext html标签生成pdf,itext使用html标签生成pdf文件

使用itext5通过html文本生成pdf文件

需要jar包

com.itextpdf

itextpdf

5.5.10

com.itextpdf

itext-asian

5.2.0

com.itextpdf.tool

xmlworker

5.5.11

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("

你好世界!hello world !");

html.append("what are you 弄啥咧!");

html.append("

标题");

html.append("");

html.append("

");

html.append("序号");

html.append("用户名");

html.append("性别");

html.append("");

html.append("

");

html.append("1");

html.append("fengxing");

html.append("男");

html.append("");

html.append("

");

html.append("2");

html.append("admin");

html.append("女");

html.append("");

html.append("");

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值