html 转换成pdf
程序员文章站
2022-04-09 09:22:54
...
[size=medium][b][color=blue]解决需求:[/color][/b][/size]
[b]1.字符串html代码转换成pdf文件[/b]
[b]2.根据FreeMarker模板动态生成静态html文件[/b]
[b]3.把静态html文件装换成pdf文件
4.解决html编写不够严谨规范的问题,自动转换为符合规范的格式
5.支持中文及换行
6.pdf文件打水印 - (html文件设置背景图片即可)
7.使用用户根据自己的需求改写jar包中的代码添加 页眉页脚,及当前第几页[/b]
[b][color=red][size=medium]注意:[/size][/color][/b]
[b]使用jar包的版本,特定的jar包是经过改写而实现支持中文及换行问题,附件中给出的就是一组特定版本的jar包,能够解决以上需求[/b]
[color=blue]还是有点毛病:[/color]
加粗不管用,css样式和B标签都不管用这。。
[color=blue]
参考了很多网上的文章:[/color]
[url]http://swordshadow.iteye.com/blog/1983935#comments[/url]等等。。。
[color=blue]先来个效果图:[/color]
[img]http://dl.iteye.com/upload/picture/pic/132197/00de7503-80c3-30b6-8ffc-c2809a634b74.jpg[/img]
[b][color=red]1.得到html的文本内容[/color][/b]
[b][color=red]2.String类型的html转换成pdf[/color][/b]
连接数据库类文件
[b][color=red]3.html静态文件生成pdf[/color][/b]
[b][color=red]4.利用freemark生成静态的html文件[/color][/b]
core-renderer3.jar
htmlcleaner.jar
iText-2.0.8.jar
iTextAsian.jar
jtidy-r8-21122004.jar
mysql-connector-java-5.1.6-bin.jar
下面有测试项目
http://www.micmiu.com/opensource/expdoc/itext-xml-worker-cn/
[b]1.字符串html代码转换成pdf文件[/b]
[b]2.根据FreeMarker模板动态生成静态html文件[/b]
[b]3.把静态html文件装换成pdf文件
4.解决html编写不够严谨规范的问题,自动转换为符合规范的格式
5.支持中文及换行
6.pdf文件打水印 - (html文件设置背景图片即可)
7.使用用户根据自己的需求改写jar包中的代码添加 页眉页脚,及当前第几页[/b]
[b][color=red][size=medium]注意:[/size][/color][/b]
[b]使用jar包的版本,特定的jar包是经过改写而实现支持中文及换行问题,附件中给出的就是一组特定版本的jar包,能够解决以上需求[/b]
[color=blue]还是有点毛病:[/color]
加粗不管用,css样式和B标签都不管用这。。
[color=blue]
参考了很多网上的文章:[/color]
[url]http://swordshadow.iteye.com/blog/1983935#comments[/url]等等。。。
[color=blue]先来个效果图:[/color]
[img]http://dl.iteye.com/upload/picture/pic/132197/00de7503-80c3-30b6-8ffc-c2809a634b74.jpg[/img]
[b][color=red]1.得到html的文本内容[/color][/b]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* <ul>
* <li>功能描述: 工具类 得到html内容</li>
* </ul>
* @author gjw
*/
public class GetHtmlContent{
/**
* <ul>
* <li>功能描述: 得到html内容</li>
* </ul>
* @param ******
* @param ******
* @return ******
* @author gjw
*/
public static String getHtmlContent(String tender_id,String invest_id)
{
try
{
String path = "./upload/44/445/"+tender_id+"/"+invest_id;
File f = new File(path+"/ceshi.html");
if (f.exists())
{
FileInputStream ism = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(ism,"utf-8");
BufferedReader bs = new BufferedReader(isr);
String info;
info = bs.readLine();
String content = "";
while(info!=null){
content+=info;
info = bs.readLine();
}
return content;
}
return null;
} catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}
[b][color=red]2.String类型的html转换成pdf[/color][/b]
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.xml.parsers.ParserConfigurationException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;
import LuceneDAO.BaseDAO;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
public class PdfRenderer {
public static void main(String[] args) throws IOException, DocumentException, ParserConfigurationException, SAXException {
// Create a buffer to hold the cleaned up HTML 将不严格的html编写方式转为严谨的html编码方式
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Clean up the HTML to be well formed
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
TagNode node = cleaner.clean(getcontext());
// Instead of writing to System.out we now write to the ByteArray buffer
new PrettyXmlSerializer(props).writeXmlToStream(node, out);
// Create the PDF
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(new String(out.toByteArray()));
//BaseFont fontResolver = renderer.getFontResolver();
ITextFontResolver fontResolver = renderer.getFontResolver();
//解决中文不显示问题
fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//设置资源路径 如图片样式文件
//renderer.getSharedContext().setBaseURL("C:\\htmltopdf\\");
renderer.layout();
OutputStream outputStream = new FileOutputStream(".\\towork\\HTMLasPDF.pdf");
renderer.createPDF(outputStream);
// Finishing up
renderer.finishPDF();
out.flush();
out.close();
}
public static String getcontext()
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from post where fileid = 1";
BaseDAO baseDao = null;
try{
baseDao = new BaseDAO();
con = baseDao.getConnection();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
return rs.getString("fdesc").toString();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try
{
ps.close();
} catch (SQLException e)
{
e.printStackTrace();
}
try
{
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}
try
{
con.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
return"";
}
}
连接数据库类文件
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BaseDAO {
// 必须参数
public static final String DRIVER = "com.mysql.jdbc.Driver"; // 驱动名称
public static final String URL = "jdbc:mysql://localhost:3306/lucene"; // 数据库地址
public static final String DBNAME = "root"; // 用户名
public static final String PASSWORD = "tiger"; // 密码
/**
* 获取数据库连接
*
* @return con
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL, DBNAME, PASSWORD);
return con;
}
}
[b][color=red]3.html静态文件生成pdf[/color][/b]
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
public class PdfRenderer {
public static void main(String[] args) throws IOException, DocumentException, ParserConfigurationException, SAXException {
// Create a buffer to hold the cleaned up HTML将不严格的html编写方式转为严谨的html编码方式
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Clean up the HTML to be well formed
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
InputStream inputStream = new FileInputStream(".\\towork\\user.html");
TagNode node = cleaner.clean(inputStream, "utf-8");
// Instead of writing to System.out we now write to the ByteArray buffer
new PrettyXmlSerializer(props).writeXmlToStream(node, out);
// Create the PDF
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(new String(out.toByteArray()));
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
OutputStream outputStream = new FileOutputStream(".\\towork\\HTMLasPDF.pdf");
renderer.createPDF(outputStream);
// Finishing up
renderer.finishPDF();
out.flush();
out.close();
}
}
[b][color=red]4.利用freemark生成静态的html文件[/color][/b]
//工具类
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* 生成 静态html文件的方法
* */
public class FreeMarkerUtil{
//templatePath模板文件存放路径
//templateName 模板文件名称
//filename 生成的文件名称
public static void analysisTemplate(String templatePath,String templateName,String fileName,Map<?,?>root){
try {
Configuration config=new Configuration();
//设置要解析的模板所在的目录,并加载模板文件
config.setDirectoryForTemplateLoading(new File(templatePath));
//设置包装器,并将对象包装为数据模型
config.setObjectWrapper(new DefaultObjectWrapper());
//获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
//否则会出现乱码
Template template=config.getTemplate(templateName,"UTF-8");
//合并数据模型与模板
FileOutputStream fos = new FileOutputStream(fileName);
Writer out = new OutputStreamWriter(fos,"UTF-8");
template.process(root, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}catch (TemplateException e) {
e.printStackTrace();
}
}
}
//测试类
import java.util.HashMap;
import java.util.Map;
import com.seening.utils.SWResultList;
import com.sinocec.model.xrwd.SWAreaFactory;
/**
* 测试ftl生成html文件
* */
public class ClientTest{
public static void main(String[] args){
User user=new User();
user.setUserName("李四");
user.setUserPassword("123");
Map<String,Object> root=new HashMap<String, Object>();
root.put("user", user);;
String templatesPath="./WEB-INF/templet";
String templateFile="templet.ftl";
String htmlFile="./upload/templet/commontemplet.html";
FreeMarkerUtil.analysisTemplate(templatesPath,templateFile,htmlFile,root);
}
}
core-renderer3.jar
htmlcleaner.jar
iText-2.0.8.jar
iTextAsian.jar
jtidy-r8-21122004.jar
mysql-connector-java-5.1.6-bin.jar
下面有测试项目
http://www.micmiu.com/opensource/expdoc/itext-xml-worker-cn/
上一篇: 笔记-迎难而上之Java基础进阶1
下一篇: EF的3种开发模式