本地html转换成pdf,HTML转成pdf

HTML转成pdf:

com.itextpdf

itextpdf

5.5.13

com.itextpdf.tool

xmlworker

5.5.13

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, 45, 50, 60);

document.setMarginMirroring(false);

document.open();

XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.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();

}

}

}

public static void main(String[] args) {

try {

// 本地

String htmlFile = "D:\\1.html";

String pdfFile = "D:\\test2.pdf";

String fontPath = "D:\\simsun.ttf";

String htmlContent = "

" + "Test

测试中文Hello World

";

//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;

}

@Override

public 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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值