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: 工具类 库,它跟我们程序中用到的 基础 ...
随机推荐
- iframe自适应高度处理
一中方法: 在子页面加载完毕的时候执行 parent.document.getElementById("iframe").height=0; parent.document.get ...
- 从 "org.apache.hadoop.security.AccessControlException:Permission denied: user=..." 看Hadoop 的用户登陆认证
假设远程提交任务给Hadoop 可能会遇到 "org.apache.hadoop.security.AccessControlException:Permission denied: use ...
- shell脚本 批量转换目录下文件编码
发布:JB01 来源:脚本学堂 [大 中 小] 分享一例shell脚本,实现可以批量转换目录下的文件编码,很实用的一个小shell,有需要的朋友参考下.原文地址:http://www.jb ...
- C++加载dll失败或显示乱码
右键项目-属性-字符集-使用多字节字符集
- [elk]elasticsearch dashboard+保留10天内索引+导入导出备份
es dashboard 有两款 head 这款我一直在用 https://github.com/mobz/elasticsearch-head 先修改es的配置文件: elasticsearch.y ...
- 50篇经典珍藏 | Docker、Mesos、微服务、云原生技术干货
概念篇 全方位探(tian)索(keng)Mesos各种存储处理方式 老肖有话说@Mesos User Group第四次约会 技术实践 | Mesos 全方位“烹饪”指南 回顾 JAVA 发展轨迹,看 ...
- dp之多重背包hdu1059
题意:价值为1,2,3,4,5,6. 分别有n[1],n[2],n[3],n[4],n[5],n[6]个.求能否找到满足价值刚好是所有的一半的方案. 思路:简单的多重背包,我建议多重背包都用二进制拆分 ...
- linux内核对中断的处理方式
中断取代了轮询的通知方式,DMA取代了轮询的读写数据方式. 分类软件指令造成的中断(又叫异常,同步中断). svc, und, abt硬件通过中断请求信号造成的中断(异步中断). irq,fi ...
- SHLVL--shell终端深度
参考:How And Why You Would Use The $SHLVL Variable SHLVL代表shell打开的深度,进程第一次打开shell时$SHLVL=1,然后在此shell中再 ...
- 串口编程-termios编程
linux使用terminfo数据库来描述终端能力以及调用对应功能的方法.POSIX定义了完成终端I/O的标准方法:termios函数族 #include <termios.h>#incl ...