注意:需要导入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的工具类的更多相关文章

  1. velocity merge作为工具类从web上下文和jar加载模板的两种常见情形

    很多时候,处于各种便利性或折衷或者通用性亦或是限制的原因,会借助于模板生成结果,在此介绍两种使用velocity merge的情形,第一种是和spring mvc一样,将模板放在velocityCon ...

  2. Android framework编译出来的jar包classes.jar的位置

    在源码环境下编译 Android framework编译出来的jar包classes.jar的位置  out/target/common/obj/JAVA_LIBRARIES/framework_in ...

  3. 转:linux下jar命令与jar包

    原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.htmlJAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是有 ...

  4. OJDBC版本区别 [ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别]

    http://blog.163.com/jekyll_zhou@126/blog/static/1820473820123206189381/ OJDBC版本区别 [ojdbc14.jar,ojdbc ...

  5. 使用jar 命令生成.jar遇到的问题(绝对路径)

    最近学java遇到一个问题:在使用命令行编译jar包的时候 出现了jar包里面的结构是一个电脑的绝对路径(把jar包变成zip格式后看到的) 之所以出现这个问题一个是以为 jar包会自己识别其相对路径 ...

  6. spring中各jar功能及jar包之间的依赖关系

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工 ...

  7. 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 ...

  8. 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 ...

  9. JDK环境变量中dt.jar、tools.jar等变量值的作用

    变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; tools.jar: 工具类 库,它跟我们程序中用到的 基础 ...

随机推荐

  1. U3D安卓下OnApplicationQuit不执行的解决方法

    安卓下当你按Home键,程序会进入暂停状态.所以只能改成调用OnApplicationPause. Unity论坛上说实现IDispose接口也可以,似乎IOS可以,但安卓测试了,无效.

  2. atitit.thumb生成高质量缩略图 php .net c++ java

    atitit.java thumb生成高质量缩略图 php .net c++ 1. 图像缩放(image scaling)---平滑度(smoothness)和清晰度(sharpness) 1 2.  ...

  3. UIScrollView 滚动视图—IOS开发

    转自:http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect  ...

  4. Nginx - rewrite 不区分大小写进行匹配

    Nginx - rewrite 不区分大小写进行匹配 分类: Nginx2014-05-28 19:25 1046人阅读 评论(0) 收藏 举报 Use (?i) to match case-inse ...

  5. CSS3多列布局

    通过 CSS3,您能够创建多个列来对文本进行布局 - 就像报纸那样! 在本章中,您将学习如下多列属性: column-count column-gap column-rule 浏览器支持 属性 浏览器 ...

  6. 深入 Spring 系列之静态资源处理

    http://blog.csdn.net/xichenguan/article/details/52794862

  7. 设置root密码,su与sudo的区别

    sudo passwd root 可以修改root密码,但首先会要求你输入当前用户的密码 sudo的意思是switch user do,默认切换到root,要求当前用户的密码,会自动调用exit返回到 ...

  8. Unity3D学习(十):使用VideoPlayer在UI上播放视频

    前言 每一款游戏往往启动的第一次都会播放CG动画之类的,Unity本身对于移动平台也提供了一个接口. Handheld.PlayFullScreenMovie("path") 过场 ...

  9. Shape Control for .NET

    Shape Control for .NET Yang Kok Wah, 23 Mar 2017 CPOL    4.83 (155 votes)   Rate this: vote 1vote 2v ...

  10. [内核]procfs和sysfs

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-sysfs/ 使用 sys 文件系统访问 Linux 内核 sysfs 的历史其与 proc 的 ...