java
package com.stone.service;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
/**
* Created by shidonghua on 2017/12/29.
*/
@Service
public class Html2Pdf {
public byte[] convert(String htmlStr) {
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
try {
fontResolver.addFont("/Users/shidonghua/IdeaProjects/vm2pdf/src/main/resources/fonts/SimSun.ttf",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// fontResolver.addFont("STSong-Light",
// "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
renderer.setDocumentFromString(htmlStr);
renderer.layout();
try {
renderer.createPDF(os);
} catch (DocumentException e) {
e.printStackTrace();
}
return os.toByteArray();
}
public static void main(String[] args) throws IOException {
Html2Pdf html2Pdf = new Html2Pdf();
ClassLoader classLoader = html2Pdf.getClass().getClassLoader();
File htmlFile = new File(classLoader.getResource("html/htmlTest1.html").getFile());
String htmlStr = FileUtils.readFileToString(htmlFile);
// byte[] content = html2Pdf.convert("<html><body>test pdf</body></html>");
byte[] content = html2Pdf.convert(htmlStr);
FileOutputStream fstream = null;
BufferedOutputStream stream = null;
File file = null;
try {
file = new File("test.pdf");
fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
// IOUtils.closeQuietly(fstream);
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fstream != null) {
try {
fstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>合同</title>
<style type="text/css">
body {font-family: SimSun;}
@page{size:210mm,297mm}
</style>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td rowspan="2">项目单位基本信息</td>
<td colspan="2">项目单位名称</td>
<td colspan="2">地址</td>
<td colspan="2">Telephone</td>
</tr>
</table>
</body>
</html>
build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.stone.tool'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.xhtmlrenderer:flying-saucer-pdf:9.0.8')
compile('com.itextpdf:itext-asian:5.2.0')
compile('com.itextpdf:itextpdf:5.4.3')
compile 'org.apache.commons:commons-io:1.3.2'
testCompile('org.springframework.boot:spring-boot-starter-test')
}