富文本html转化为pdf
来源:互联网 发布:java 二叉树翻转 编辑:程序博客网 时间:2024/06/08 14:48
ckeditor、fckeditor富文本编辑器生成pdf,用pd4ml实现
Java代码
package com.ais.test;
import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.security.InvalidParameterException;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
public class Test {
protected int topValue = 10;
protected int leftValue = 20;
protected int rightValue = 10;
protected int bottomValue = 10;
protected int userSpaceWidth = 1300;
/**
* @param args
*/
public static void main(String[] args) {
try {
Test jt = new Test();
String html = readFile("D:\\Test\\pdf_sample.html", "UTF-8");
jt.doConversion2(html, "D:\\Test\\pd4ml.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
public void doConversion2(String htmlDocument, String outputPath)
throws InvalidParameterException, MalformedURLException,
IOException {
PD4ML pd4ml = new PD4ML();
pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of
// "virtual web browser"
// choose target paper format
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
// define PDF page margins
pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue,
rightValue));
// source HTML document also may have margins, could be suppressed this
// way
// (PD4ML *Pro* feature):
pd4ml.addStyle("BODY {margin: 0; font-family:MSJH}", true);
// If built-in basic PDF fonts are not sufficient or
// if you need to output non-Latin texts, TTF embedding feature should
// help
// (PD4ML *Pro*)
// pd4ml.useTTF("C:\\workspace\\HtmlToPDF", true);
pd4ml.useTTF( "java:fonts", true );
pd4ml.setDefaultTTFs("MSJH", "Arial", "Courier New");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// actual document conversion from HTML string to byte array
pd4ml.render(new StringReader(htmlDocument), baos);
// if the HTML has relative references to images etc,
// use render() method with baseDirectory parameter instead
baos.close();
System.out.println("resulting PDF size: " + baos.size() + " bytes");
// in Web scenarios it is a good idea to send the size with
// "Content-length" HTTP header
File output = new File(outputPath);
java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
fos.write(baos.toByteArray());
fos.close();
System.out.println(outputPath + "\ndone.");
}
private final static String readFile(String path, String encoding)
throws IOException {
File f = new File(path);
FileInputStream is = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
byte buffer[] = new byte[2048];
int read;
do {
read = is.read(buffer, 0, buffer.length);
if (read > 0) {
fos.write(buffer, 0, read);
}
} while (read > -1);
fos.close();
bis.close();
is.close();
return fos.toString(encoding);
}
}
需要的jar包:fonts.jar\pd4ml_demo.jar\ss_css2.jar
附件中少了font.jar包,由于太大,请自行下载
- 富文本html转化为pdf
- HTML转化为PDF
- 富文本保存为Word时,打开Word为html
- html标签转化为纯文本格式
- 将excel表格转化为html文本
- 解析html富文本
- 中文分词分析之PDF批量转化为文本
- 将PDF 转化 为 Word,Html 等文件
- V5富文本和HTML
- UILabel展示html富文本
- UIWebView展示html富文本
- 富文本Html.fromHtml方法
- iOS 富文本加载 HTML
- Swift HTML富文本显示
- 富文本编辑器html过滤
- word转化为PDF
- Word转化为PDF
- 将 文本格式标记 转化为 html格式标记
- 题目1113:二叉树
- CSS 背景图片 设置居中
- 黑马程序员_Java语言_网络编程
- MySQL数据库Root密码重置
- silver trend
- 富文本html转化为pdf
- 安卓学习第二十三天:Spinner,TableLayout
- 习题3-20
- mac下安装tesseract-ocr
- 蓝桥杯——操作格子(线段树)
- 2-17
- linux 的动态库和静态库
- Unity3D Compute shader 新解-图片粒子矩阵【一】
- Linux命令之curl