160603、使用pd4ml.jar和ss_css2.jar转pdf的工具类
注意:需要导入pd4ml.jar和ss_css2.jar
import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidParameterException;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
/***
* pd4ml.jar,ss_css2.jar
* @author admin
*/
public class ToPdfUtils {
protected int topValue = 10;
protected int leftValue = 20;
protected int rightValue = 10;
protected int bottomValue = 10;
protected int userSpaceWidth = 1300;
public static void main(String[] args) throws InvalidParameterException, MalformedURLException, IOException {
ToPdfUtils pdfUtils = new ToPdfUtils();
// pdfUtils.doConversion("http://pd4ml.com/sample.htm", "g:/test/pd4ml.pdf");//网络地址
String html = readFile("g:/test/confirm.html", "UTF-8"); //文件地址
pdfUtils.doConversion2(html, "g:/test/pd4ml2.pdf");
}
/**将文件转换成pdf,源文件为http://开头的网络地址*/
public void doConversion(String url, String outputPath)
throws InvalidParameterException, MalformedURLException,
IOException {
File output = new File(outputPath);
FileOutputStream fos = new FileOutputStream(output);
PD4ML pd4ml = new PD4ML();
pd4ml.setHtmlWidth(userSpaceWidth);
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue,rightValue));
pd4ml.addStyle("BODY {margin: 0}", true);
pd4ml.useTTF("java:fonts", true);//src下fonts文件夹中的字体设置
pd4ml.render(new URL(url), fos);
fos.close();
System.out.println(outputPath + "\ndone.");
}
public void doConversion2( String htmlDocument, String outputPath )
throws InvalidParameterException, MalformedURLException, IOException {
PD4ML pd4ml = new PD4ML();
pd4ml.setHtmlWidth(userSpaceWidth);
// 选择目标文件的格式
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
// 设置边距
pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
//原来的html文档也有边距,可以通过这个方式压缩
pd4ml.addStyle("BODY {margin: 0}", true);
// 如果内置的基本pdf字体不够用,可以设置成non-Latin,TTF能够做到这一点
pd4ml.useTTF("java:fonts", true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pd4ml.render(new StringReader(htmlDocument), baos);
baos.close();
File output = new File(outputPath);
FileOutputStream fos = new 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);
}
}
160603、使用pd4ml.jar和ss_css2.jar转pdf的工具类的更多相关文章
- velocity merge作为工具类从web上下文和jar加载模板的两种常见情形
很多时候,处于各种便利性或折衷或者通用性亦或是限制的原因,会借助于模板生成结果,在此介绍两种使用velocity merge的情形,第一种是和spring mvc一样,将模板放在velocityCon ...
- Android framework编译出来的jar包classes.jar的位置
在源码环境下编译 Android framework编译出来的jar包classes.jar的位置 out/target/common/obj/JAVA_LIBRARIES/framework_in ...
- 转:linux下jar命令与jar包
原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.htmlJAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是有 ...
- OJDBC版本区别 [ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别]
http://blog.163.com/jekyll_zhou@126/blog/static/1820473820123206189381/ OJDBC版本区别 [ojdbc14.jar,ojdbc ...
- 使用jar 命令生成.jar遇到的问题(绝对路径)
最近学java遇到一个问题:在使用命令行编译jar包的时候 出现了jar包里面的结构是一个电脑的绝对路径(把jar包变成zip格式后看到的) 之所以出现这个问题一个是以为 jar包会自己识别其相对路径 ...
- spring中各jar功能及jar包之间的依赖关系
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工 ...
- Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar ...
- Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError
SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackO ...
- JDK环境变量中dt.jar、tools.jar等变量值的作用
变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; tools.jar: 工具类 库,它跟我们程序中用到的 基础 ...
随机推荐
- unity, 不要change Default sharedMaterial
假设在场景中加一个sprite,其材质使用默认的Sprites-Default. 若调用: Color color=sprite.GetComponent<SpriteRenderer>( ...
- 安装wxWidgets遭遇的两大关卡
早就想体验wxWidgets.这学期的C++课,课时还算充裕.关键是弟子们的实践能跟得上,我希望能让他们也浅尝一把GUI开发. MFC能够选.但既然IDE都用CodeBlocks了.还是选wxWidg ...
- makefile之strip函数
#$(strip <string> ) #名称:去空格函数--strip. #功能:去掉<string>字串中开头和结尾的空字符,并将中间的多个连续空字符(如果有的话)合并为一 ...
- 工作流Activiti的学习总结(十二) activiti官方十分钟快速学习 (zhuan)
http://topmanopensource.iteye.com/blog/1315341 ***************************************************** ...
- 使用eclipse进行web开发的3个lib文件夹
1.右击project>Build Path>Configure Build Path(一般是在你的项目文件夹中手动创建一个lib文件夹,里面设置若干子文件夹存放不同的jar包,然后通过C ...
- Ununtu 15.04 安装MySql(Django连接Mysql)
本文介绍Ubuntu 15.04下安装MySQL ubuntu 15.04安装mysql django项目连接mysql 一.安装数据库 1.sudo apt-get install mysql-se ...
- PHP的数据类型转换
PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: •(int).(integer):转换成整形 •(float).(double).(real):转换成浮点型 •(string):转换成 ...
- 最新PHPcms9.6.0 任意文件上传漏洞
在用户注册处抓包: 然后发送到repeater POC: siteid=&modelid=&username=z1aaaac121&password=aasaewee311as ...
- 重置 ckeditor清空内容
转载自:http://blog.csdn.net/woshirongshaolin/article/details/8239407 <script type="text/javascr ...
- 应用DataAdapter对象填充DataSet数据集
private void Form1_Load(object sender, EventArgs e) { string strCon = "Server=localhost;User Id ...