简单介绍一下:如何将html文件转化为pdf文件。
仅供自己学习。
常见的几个方法总结:
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));
}
}
对于这部分需要引入几个特定的属性:
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 = "Test
Hello World
";
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);
}
}
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));
}
}
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);
}
}
利用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);
}
}
这一层和上一层不同之处在于,输出的时候用一层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,基本上都能实现目地,只是对于不同的输出方式采用不同的方式。