HTML转成pdf

发布时间:2020-07-19 14:58:01 作者:mufeng07
来源:网络 阅读:411

HTML转成pdf:
<!-- 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, 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 = "<html><body style=\"font-size:12.0pt; font-family:宋体\">" + "<h2>Test</h2><p>测试中文Hello World</p></body></html>";
        //parseHtml2PdfByString(pdfFile,htmlContent,fontPath);
        parseHtml2PdfByFilePath(pdfFile,htmlFile,fontPath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

/**

推荐阅读:
  1. 基于tcpdf将html转成pdf
  2. HTML如何转成PDF

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java %d

上一篇:【分享】Spring全家桶思维笔记导图(Spring Boot+Cloud+IOC+AOP+MVC)

下一篇:GO学习笔记 - 数据校验

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》