利用itext将html文件转化pdf文件

利用itext将html文件转化pdf文件

大黄奔跑 2018-04-04 10:01:49 8161 收藏 2
分类专栏: java iText springMVC
简单介绍一下:如何将html文件转化为pdf文件。

仅供自己学习。

常见的几个方法总结:

1:最简单的html用String方式表示的形式

package com.ctbri.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.HtmlConverter;

/**

  • Converts a simple Hello World HTML String to a PDF document.
    */
    public class C01E01_HelloWorld {

    /**

    • The HTML-html原文件路径
    • The target —— 结果的输出所在的文件夹
    • DesT —— pdf输出的具体路径
      */
      public static final String HTML = “

      Test

      Hello World

      ”;
      public static final String TARGET = “target/results/ch01/”;
      public static final String DEST = String.format("%stest-01.pdf", TARGET);

    public static void main(String[] args) throws IOException {
    File file = new File(TARGET);
    file.mkdirs();
    new C01E01_HelloWorld().createPdf(HTML, DEST);
    System.out.println(“ok”);
    }

    /**

    • Creates the PDF file.
      */
      public void createPdf(String html, String dest) throws IOException {
      HtmlConverter.convertToPdf(html, new FileOutputStream(dest));
      }
      }
      2、需要引入图片、css文件

对于这部分需要引入几个特定的属性:

BASEURI 用于表示装载有css、image、以及html等文件的文件夹
SRC html文件所在的路径
TARGET PDF文件所在的父路径
DEST 生成的pdf文件的路径
比如:

public static final String BASEURI = “src/main/resources/html/”;
public static final String SRC = String.format("%shello.html", BASEURI);
public static final String TARGET = “target/results/ch01/”;
public static final String DEST = String.format("%stest-03.pdf", TARGET);
同时引入ConverterProperties 属性,用于添加Baseuri属性

package com.ctbri.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;

public class C01E02_HelloWorld {

public static final String BASEURI = "src/main/resources/html/";
public static final String HTML = "<h1>Test</h1><p>Hello World</p><img src=\"img/logo.png\">";
public static final String TARGET = "target/results/ch01/";
public static final String DEST = String.format("%stest-02.pdf", TARGET);

public static void main(String[] args) throws IOException {
    File file = new File(TARGET);
    file.mkdirs();
    new C01E02_HelloWorld().createPdf(BASEURI, HTML, DEST);
}

/**
 * Creates the PDF file.
 */
public void createPdf(String baseUri, String html, String dest) throws IOException {
	ConverterProperties properties = new ConverterProperties();
	properties.setBaseUri(baseUri);
    HtmlConverter.convertToPdf(html, new FileOutputStream(dest), properties);
}

}
3、html和pdf都用File对象表示

package com.ctbri.test;

import java.io.File;
import java.io.IOException;

import com.itextpdf.html2pdf.HtmlConverter;

/**

  • Converts a simple HTML file to PDF using File objects
    */
    public class C01E03_HelloWorld {

    public static final String BASEURI = “src/main/resources/html/”;
    public static final String SRC = String.format("%shello.html", BASEURI);
    public static final String TARGET = “target/results/ch01/”;
    public static final String DEST = String.format("%stest-03.pdf", TARGET);

    public static void main(String[] args) throws IOException {
    File file = new File(TARGET);
    file.mkdirs();
    new C01E03_HelloWorld().createPdf(BASEURI, SRC, DEST);
    }

    /**

    • Creates the PDF file.
    • @param baseUri the base URI
    • @param src the path to the source HTML file
    • @param dest the path to the resulting PDF
      */
      public void createPdf(String baseUri, String src, String dest) throws IOException {
      HtmlConverter.convertToPdf(new File(src), new File(dest));
      }
      }
      4、html和pdf都用FileInputStream对象表示

package com.ctbri.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;

/**

  • Converts a simple HTML file to PDF using an InputStream and an OutputStream
    */
    public class C01E04_HelloWorld {

    public static final String BASEURI = “src/main/resources/html/”;
    public static final String SRC = String.format("%shello.html", BASEURI);
    public static final String TARGET = “target/results/ch01/”;
    public static final String DEST = String.format("%stest-04.pdf", TARGET);

    /**

    • The main method of this example.
      */
      public static void main(String[] args) throws IOException {
      File file = new File(TARGET);
      file.mkdirs();
      new C01E04_HelloWorld().createPdf(BASEURI, SRC, DEST);
      }

    /**

    • Creates the PDF file.
      */
      public void createPdf(String baseUri, String src, String dest) throws IOException {
      ConverterProperties properties = new ConverterProperties();
      properties.setBaseUri(baseUri);
      HtmlConverter.convertToPdf(new FileInputStream(src), new FileOutputStream(dest), properties);
      }
      }
      5、html用FileInputStream、pdf用PDfWriter

利用pdfWriter代替outputStream ,前者可以更好的配合我们设置输出文件的属性。

package com.ctbri.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;

/**

  • Converts a simple HTML file to PDF using an InputStream and a PdfWriter
    */
    public class C01E05_HelloWorld {

    public static final String BASEURI = “src/main/resources/html/”;
    public static final String SRC = String.format("%shello.html", BASEURI);
    public static final String TARGET = “target/results/ch01/”;
    public static final String DEST = String.format("%stest-05.pdf", TARGET);

    public static void main(String[] args) throws IOException {
    File file = new File(TARGET);
    file.mkdirs();
    new C01E05_HelloWorld().createPdf(BASEURI, SRC, DEST);
    }

    /**

    • Creates the PDF file. output using PdfWriter
      */
      public void createPdf(String baseUri, String src, String dest) throws IOException {
      ConverterProperties properties = new ConverterProperties();
      properties.setBaseUri(baseUri);
      PdfWriter writer = new PdfWriter(dest, new WriterProperties().setFullCompressionMode(true));
      HtmlConverter.convertToPdf(new FileInputStream(src), writer, properties);
      }
      }
      6、html用FileInputStream、pdf用PDfDocument

这一层和上一层不同之处在于,输出的时候用一层pdfDocument包裹在pdfWriter外面。

并且引入了pdf.setTagged() 方便我们加入侧边的语义结构。

package com.ctbri.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;

/**

  • Converts a simple HTML file to PDF using an InputStream and a PdfDocument
    */
    public class C01E06_HelloWorld {

    public static final String BASEURI = “src/main/resources/html/”;
    public static final String SRC = String.format("%shello.html", BASEURI);
    public static final String TARGET = “target/results/ch01/”;
    public static final String DEST = String.format("%stest-06.pdf", TARGET);

    public static void main(String[] args) throws IOException {
    File file = new File(TARGET);
    file.mkdirs();
    new C01E06_HelloWorld().createPdf(BASEURI, SRC, DEST);
    }

    /**

    • Creates the PDF file.
      */
      public void createPdf(String baseUri, String src, String dest) throws IOException {
      ConverterProperties properties = new ConverterProperties();
      properties.setBaseUri(baseUri);
      PdfWriter writer = new PdfWriter(dest);
      PdfDocument pdf = new PdfDocument(writer);
      pdf.setTagged(); //用于增加目录
      HtmlConverter.convertToPdf(new FileInputStream(src), pdf, properties);
      }
      }
      总结:上面介绍了6中基础的将html转为pdf文件的方式,总体没有什么区别,都是对不同源文件或者输出文件处理的几种方式。

包括:File、OutputStream、pdfWriter、pdfDocument,基本上都能实现目地,只是对于不同的输出方式采用不同的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值