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