itextpdf html转pdf


itextpdf html转pdf

                            

                                   

**************

相关类与接口

                 

HtmlConverter

public class HtmlConverter {
    private HtmlConverter() {
    }


***********
转换为pdf

    public static void convertToPdf(String html, OutputStream pdfStream) {
    public static void convertToPdf(String html, OutputStream pdfStream, ConverterProperties converterProperties) {
    public static void convertToPdf(String html, PdfWriter pdfWriter) {
    public static void convertToPdf(String html, PdfWriter pdfWriter, ConverterProperties converterProperties) {
    public static void convertToPdf(String html, PdfDocument pdfDocument, ConverterProperties converterProperties) {

    public static void convertToPdf(File htmlFile, File pdfFile) throws IOException {
    public static void convertToPdf(File htmlFile, File pdfFile, ConverterProperties converterProperties) throws IOException {

    public static void convertToPdf(InputStream htmlStream, OutputStream pdfStream) throws IOException {
    public static void convertToPdf(InputStream htmlStream, OutputStream pdfStream, ConverterProperties converterProperties) throws IOException {
    public static void convertToPdf(InputStream htmlStream, PdfDocument pdfDocument) throws IOException {
    public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter) throws IOException {
    public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter, ConverterProperties converterProperties) throws IOException {
    public static void convertToPdf(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties) throws IOException {


***********
转换为document

    public static Document convertToDocument(String html, PdfWriter pdfWriter) {
    public static Document convertToDocument(InputStream htmlStream, PdfWriter pdfWriter) throws IOException {
    public static Document convertToDocument(String html, PdfWriter pdfWriter, ConverterProperties converterProperties) {
    public static Document convertToDocument(InputStream htmlStream, PdfWriter pdfWriter, ConverterProperties converterProperties) throws IOException {
    public static Document convertToDocument(String html, PdfDocument pdfDocument, ConverterProperties converterProperties) {
    public static Document convertToDocument(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties) throws IOException {


***********
转换为element

    public static List<IElement> convertToElements(String html) {
    public static List<IElement> convertToElements(InputStream htmlStream) throws IOException {
    public static List<IElement> convertToElements(String html, ConverterProperties converterProperties) {
    public static List<IElement> convertToElements(InputStream htmlStream, ConverterProperties converterProperties) throws IOException {


    static IMetaInfo createPdf2HtmlMetaInfo() {

    private static IMetaInfo resolveMetaInfo(ConverterProperties converterProperties) {
    private static class HtmlMetaInfo implements IMetaInfo {

                      

Converterproperties

public class ConverterProperties {
    private static final int DEFAULT_LIMIT_OF_LAYOUTS = 10;
    private MediaDeviceDescription mediaDeviceDescription;
    private FontProvider fontProvider;
    private ITagWorkerFactory tagWorkerFactory;
    private ICssApplierFactory cssApplierFactory;
    private OutlineHandler outlineHandler;
    private String baseUri;
    private IResourceRetriever resourceRetriever;
    private boolean createAcroForm = false;
    private String charset;
    private boolean immediateFlush = true;
    private int limitOfLayouts = 10;
    private IMetaInfo metaInfo;

    public ConverterProperties() {
    public ConverterProperties(ConverterProperties other) {


    public ConverterProperties setCharset(String charset) {
    public ConverterProperties setBaseUri(String baseUri) {
    public ConverterProperties setEventMetaInfo(IMetaInfo metaInfo) {
    public ConverterProperties setLimitOfLayouts(int limitOfLayouts) {
    public ConverterProperties setImmediateFlush(boolean immediateFlush) {
    public ConverterProperties setCreateAcroForm(boolean createAcroForm) {
    public ConverterProperties setFontProvider(FontProvider fontProvider) {
    public ConverterProperties setOutlineHandler(OutlineHandler outlineHandler) {
    public ConverterProperties setTagWorkerFactory(ITagWorkerFactory tagWorkerFactory) {
    public ConverterProperties setCssApplierFactory(ICssApplierFactory cssApplierFactory) {
    public ConverterProperties setResourceRetriever(IResourceRetriever resourceRetriever) {
    public ConverterProperties setMediaDeviceDescription(MediaDeviceDescription mediaDeviceDescription) {

    public boolean isCreateAcroForm() {
    public boolean isImmediateFlush() {

    public String getCharset() {
    public String getBaseUri() {
    IMetaInfo getEventMetaInfo() {
    public int getLimitOfLayouts() {
    public FontProvider getFontProvider() {
    public OutlineHandler getOutlineHandler() {
    public ITagWorkerFactory getTagWorkerFactory() {
    public IResourceRetriever getResourceRetriever() {
    public ICssApplierFactory getCssApplierFactory() {
    public MediaDeviceDescription getMediaDeviceDescription() {

                     

                           

**************

示例

                

public class Test8 {

    private static final String source = "./html/1.html";
    private static final String source2 = "./html/2.html";

    private static final String dest = "./dest/html.pdf";
    private static final String dest2 = "./dest/html2.pdf";

    public static void fun() throws Exception{
        ConverterProperties properties = new ConverterProperties();
        FontProvider fontProvider = new FontProvider();
        fontProvider.addFont("./fonts/simkai.ttf");
        properties.setFontProvider(fontProvider);     //添加中文支持

        HtmlConverter.convertToPdf(new FileInputStream(source),new PdfWriter(dest),properties);
    }

    public static void fun2() throws Exception{    //转换包含图片的html
        ConverterProperties properties = new ConverterProperties();
        FontProvider fontProvider = new FontProvider();
        fontProvider.addFont("./fonts/simkai.ttf");
        properties.setFontProvider(fontProvider);
        //properties.setBaseUri(".");      //包含html、css、image的目录,默认为当前项目路径

        HtmlConverter.convertToPdf(new FileInputStream(source2),new PdfWriter(dest2),properties);
    }

    public static void main(String[] args) throws Exception{
        fun();
        fun2();
    }
}

                      

源文件:1.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div align="center">
  <h1>瓜田李下</h1>
  <span style="color: coral;font-size: 20px">海贼王</span>
</div>
</body>
</html>

                  

源文件:2.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
  .cla{
    color: coral;
  }
</style>
<body>
<div align="center">
  <h2>瓜田李下</h2><br>
  <img src="./image/ymhd.jpg"><br>
  <span class="cla">灌篮高手</span>
</div>
</body>
</html>

                     

转换后的pdf文件:html.pdf

                 

                 

转换后的pdf文件:html2.pdf

                 

                       

                           

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值