HTML文件转PDF
- 利用IText将HTML文件转为pdf
pom文件
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>
public static void main(String[] args) {
StringBuilder strline = new StringBuilder("");
File fin = new File("C:\\Users\\about.html");
try (RandomAccessFile accessFile = new RandomAccessFile(fin, "r");
FileChannel fcin = accessFile.getChannel();
) {
Charset charset = Charset.forName("UTF-8");
int bufSize = 100000;
ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);
String enterStr = "\n";
byte[] bs = new byte[bufSize];
StringBuilder strBuf = new StringBuilder("");
while (fcin.read(rBuffer) != -1) {
int rSize = rBuffer.position();
rBuffer.rewind();
rBuffer.get(bs);
rBuffer.clear();
String tempString = new String(bs, 0, rSize, charset);
tempString = tempString.replaceAll("\r", "");
int fromIndex = 0;
int endIndex = 0;
while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
String line = tempString.substring(fromIndex, endIndex);
line = strBuf.toString() + line;
strline.append(line.trim());
strBuf.delete(0, strBuf.length());
fromIndex = endIndex + 1;
}
if (rSize > tempString.length()) {
strline.append(tempString.substring(fromIndex, tempString.length()));
strBuf.append(tempString.substring(fromIndex, tempString.length()));
} else {
strline.append(tempString.substring(fromIndex, rSize));
strBuf.append(tempString.substring(fromIndex, rSize));
}
}
String htmlString = "";
htmlString = strline.toString().replaceAll("\"", "'").replaceAll("<style>", "<style>body{font-family:SimSun;font-size:14px;}");
OutputStream os = new FileOutputStream("C:\\Users\\about.pdf"); //生成PDF文件的路径
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver font = renderer.getFontResolver();
font.addFont("C:/WINDOWS/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocumentFromString(htmlString.toString());
renderer.layout();
renderer.createPDF(os);
renderer.finishPDF();
} catch (Exception e) {
System.out.println(e);
}
}
- 使用html2pdf
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>4.0.2</version>
</dependency>
public static void main(String[] args) {
File file=new File("about.html");
StringBuffer buffer=new StringBuffer();
try{
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while(bufferedReader.ready()){
buffer.append(bufferedReader.readLine());
}
bufferedReader.close();
}catch (Exception e){
e.printStackTrace();
}
String html =buffer.toString();
try(FileOutputStream outputStream = new FileOutputStream("D:\\table.pdf")){
Long start = Calendar.getInstance().getTimeInMillis();
html = html.replace(" ", " ");
html = html.replace("–", "–");
html = html.replace("—", "—");
html = html.replace("‘", "‘");
html = html.replace("’", "’");
html = html.replace("‚", "‚");
html = html.replace("“", "“");
html = html.replace("”", "”");
html = html.replace("„", "„");
html = html.replace("′", "′");
html = html.replace("″", "″");
html = html.replace("‹", "‹");
html = html.replace("›", "›");
html = html.replace("‾", "‾");
ConverterProperties converterProperties = new ConverterProperties();
FontProvider provider = new FontProvider();
provider.addFont("C:/WINDOWS/Fonts/STXIHEI.TTF");
converterProperties.setFontProvider(provider);
HtmlConverter.convertToPdf(html,outputStream,converterProperties);
}catch (Exception e){
e.printStackTrace();
}
}