HTML转PDF总结

这两天自己尝试了一下,HTML页面转化成 PDF ,历时2天,下面总结一下经验:

1.先把某大神的关键代码贴出来
import java.awt.Dimension;
import java.awt.Insets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;

/**
 * @author jcoder
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class htmlToPdfServices {

    protected Dimension format = PD4Constants.A4;

    protected boolean landscapeValue = false;

    protected int topValue = 10;

    protected int leftValue = 10;

    protected int rightValue = 10;

    protected int bottomValue = 10;

    protected String unitsValue = "mm";

    protected String proxyHost = "";

    protected int proxyPort = 0;

    protected int userSpaceWidth = 780;

    public void runConverter(String urlstring, String fileName,
            HttpServletResponse response) throws IOException {

        if (urlstring.length() > 0) {

            if (!urlstring.startsWith("http://")
                    && !urlstring.startsWith("file:")) {

                urlstring = "http://" + urlstring;

            }

            ByteArrayOutputStream ba = new ByteArrayOutputStream();

            if (proxyHost != null && proxyHost.length() != 0 && proxyPort != 0) {

                System.getProperties().setProperty("proxySet", "true");

                System.getProperties().setProperty("proxyHost", proxyHost);

                System.getProperties().setProperty("proxyPort", "" + proxyPort);

            }

            PD4ML pd4ml = new PD4ML();
            pd4ml.setPageSize(new java.awt.Dimension(450, 450));
            pd4ml.setPageInsets(new java.awt.Insets(20, 50, 10, 10));
            pd4ml.enableImgSplit(false);
            //pd4ml.useTTF("java:cn/com/pdf/fonts", true);
            pd4ml.useTTF("java:fonts", true);
            pd4ml.enableDebugInfo();

            try {

                pd4ml.setPageSize(landscapeValue ? pd4ml
                        .changePageOrientation(format) : format);

            } catch (Exception e) {

                e.printStackTrace();

            }

            if (unitsValue.equals("mm")) {

                pd4ml.setPageInsetsMM(new Insets(topValue, leftValue,

                bottomValue, rightValue));

            } else {

                pd4ml.setPageInsets(new Insets(topValue, leftValue,

                bottomValue, rightValue));

            }

            pd4ml.setHtmlWidth(userSpaceWidth);
            URL url = new URL(urlstring);
            pd4ml.render(urlstring, ba);
           
            if(fileName.lastIndexOf(".pdf")==-1)
                fileName = fileName + ".pdf";
            try {
                response.setHeader("Content-disposition",
                        "attachment; filename="
                                + new String(fileName.getBytes("gb2312"),
                                        "iso8859-1"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/pdf");
            response.setContentLength(ba.size());
            try {
                ServletOutputStream out = response.getOutputStream();
                ba.writeTo(out);
                out.flush();
                out.close();
                ba.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}

这个是转化的核心代码,没看懂具体怎么转,但是能用就行!


光有这个还用不起来,需要引入两个包 pd4ml_demo.jar, ss_css2.jar 图片-

然后在项目的src 文件夹下面新建一个文件夹,把pd4fonts.properties、songti.ttf 文件放在里面 ,代码中有用到
图片

做了这些 还是不够,页面上的中文会乱码,需要在页面中增加一个东西:
图片

要在样式中增加一个
 *{
            font-family: KaiTi_GB2312;
   }
到这里,基本就差不多啦! 接下来看调用的代码 :
        htmlToPdfServices jt = new htmlToPdfServices(); 
        jt.runConverter(url, name, response); 
url:  就是要生成的PDF的网络路径 
name: 就是要保存成PDF的名称,这个可以随便起

这个功能最最给力的还是,它可以弹出框让用户选择输出PDF的保存路径,这个很牛逼! 

接下来总结 JS 页面跳转到servlet 的一些知识:

function store_searchToHTML(){
  

    //这句话是获得这个页面的http地址
 
    var url=window.location.href;    
    //下面这句话是组装调用Servlet的地址并传递参数 多个参数 用 “&” 连接
    surl= $("base").attr("href")+"/servlet/HtmlToPDFServlet?url="+url+"&name="+"巡店报告单";
    //然后用以下方法去跳转到Servlet中去

    window.location.href=surl;

}


接下来还有一种跳转,就是在页面上直接跳转
 <a href="<%=basePath%>/servlet/TestServlet">导出PDF</a>

 

好了,今天就总结这些,今天是2015年的最后一天! 2016,继续加油!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值