首 页 行业热点 新车 试驾评测 养车用车 车型库

怎样用Java把html转为pdf

发布网友

我来回答

4个回答

热心网友

java中利用第三方jar包iText 5.4.2就可以实现html转为pdf。

比如有如下index.html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>

        <title>HTML to PDF</title>

        <link href="style.css" rel="stylesheet" type="text/css" />

    </head>

    <body>

        <h1>HTML to PDF</h1>

        <p>

            <span class="itext">itext</span> 5.4.2 <span class="description"> converting HTML to PDF</span>

        </p>

        <table>

          <tr>

                <th class="label">Title</th>

                <td>iText - Java HTML to PDF</td>

            </tr>

            <tr>

                <th>URL</th>

                <td>http://hmkcode.com/itext-html-to-pdf-using-java</td>

            </tr>

        </table>

    </body>

</html>

样式文件:

h1 {

  color:#ccc;

}

table tr td{

    text-align:center;

    border:1px solid gray;

    padding:4px;

}

table tr th{

    background-color:#84C7FD;

    color:#fff;

    width: 100px;

}

.itext{

    color:#84C7FD;

    font-weight:bold;

}

.description{

    color:gray;

}

后台转换的接口:

package com.hmkcode;

  

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.pdf.PdfWriter;

import com.itextpdf.tool.xml.XMLWorkerHelper;

  

public class App

{

    public static void main( String[] args ) throws DocumentException, IOException

    {

      // step 1

        Document document = new Document();

        // step 2

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));

        // step 3

        document.open();

        // step 4

        XMLWorkerHelper.getInstance().parseXHtml(writer, document,

                new FileInputStream("index.html"));

        //step 5

         document.close();

        System.out.println( "PDF Created!" );

    }

}

转换结果:

热心网友

http://blog.csdn.net/chinahgf/archive/2008/04/03/2246265.aspx

热心网友

doPDF V5.3.233┊把自己安装为打印机驱动、再从任意一个打印输出┊英文免费版
一个免费的PDF转换器,可同时运用于商业和个人,它把自己安装为一个打印机驱动,允许从任意一个有打印输出的Windows程序中打印,还包含缩放,质量定义和页面大小定义。
http://www.xy-365.com/company/s_12437.htm

参考资料:http://shipin.zhan18.com/sql/sql_jd_shipin/

热心网友

你QQ多少 我的中文乱码还没解决啊 先发给你 一起研究下

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com