使用flying-saucer-pdf将html转pdf
2016-06-04 22:39
543 查看
直接上代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
/**
*
* @date 2016年6月4日下午6:01:57
* @author 赵
*
*/
public class ConvertHtmlToPdfWithFly {
public void convert() {
FileOutputStream os = null;
try {
ITextRenderer renderer = new ITextRenderer();
os = new FileOutputStream("D:\\4.pdf");
String path = "D:\\7l3ra1b9ho8np.html";
renderer.setDocument(new File(path).toURI().toURL().toString());
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/SimSun.ttc", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意事项:
html文件的顶层的宽度最好设置为80%,这样输出成A4时效果比较好
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
/**
*
* @date 2016年6月4日下午6:01:57
* @author 赵
*
*/
public class ConvertHtmlToPdfWithFly {
public void convert() {
FileOutputStream os = null;
try {
ITextRenderer renderer = new ITextRenderer();
os = new FileOutputStream("D:\\4.pdf");
String path = "D:\\7l3ra1b9ho8np.html";
renderer.setDocument(new File(path).toURI().toURL().toString());
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/SimSun.ttc", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意事项:
html文件的顶层的宽度最好设置为80%,这样输出成A4时效果比较好
相关文章推荐
- 在Windows 8.1的IE 11中屏蔽双击放大功能
- HTML5调用摄像头实例
- Apple官网研究之使用Justify布局导航
- 关于前端的思考与感悟
- 一款免费且安全的在线 PDF 转换软件
- 通过Mootools 1.2来操纵HTML DOM元素
- jQuery Html控件基本操作(日常收集整理)
- WEB标准网页布局中尽量不要使用的HTML标签
- Flash 与 html 的一些实用技巧
- html工作中表格<tbody>标签的使用技巧
- HTML 向 XHTML1.0 兼容性指导
- C#自写的一个HTML解析类(类似XElement语法)
- PDF里的文字显示模糊的解决方法
- PDF编辑工具 Foxit PDF Editor 1.5 Build 2911 下载
- 没有文件大小限制并免费的PDF到HTML转换工具
- MicroAdobe PDF Editor 6.2 英文正式版附破解补丁 下载
- JavaScript与HTML结合的基本使用方法整理
- C#使用iTextSharp设置PDF所有页面背景图功能实例