Lowagie 导出html的内容到 pdf

使用 com.Lowagie.itext 导出html的内容到 pdf

  学习了使用com.Lowagie.itext导出html的内容到word,导出pdf的话,会是什么情况?可以直接用吗? 有什么不同呢?

处理:

引入itext:

<dependency>
   <groupId>com.lowagie</groupId>
   <artifactId>itext</artifactId>
   <version>2.0.8</version>
</dependency>

直接引用:

public static void main(String[] args) throws Exception {
    OutputStream out = new FileOutputStream("d://exportFile//f"+System.currentTimeMillis()+".pdf");
    Document document = new Document(PageSize.A4);
    PdfWriter.getInstance(document, out);
    document.open();

    String content = "<p>图片导出</p>\n<p>&nbsp;</p>\n<p>啦啦</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p><img src=\"F://temp//back1.jpg\" alt=\"\" width=\"640\" height=\"640\" /></p>\n<p>&nbsp;</p>\n<p>发放</p>\n<p>&nbsp;</p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p>&nbsp;</p>";

    StyleSheet styleSheet = new StyleSheet();
    List htmlList = HTMLWorker.parseToList(new StringReader(content), styleSheet);
    Paragraph context = new Paragraph();
    for (Object aHtmlList : htmlList) {
        Element e = (Element) aHtmlList;
        context.add(e);
    }
    document.add(context);
    document.close();
    System.out.println("ok");
}

PS: 字符里面的图片,根据实际的进行替换。大小要合适,不然会显示不出来。

结果:

出现的问题:

 1,只出现了两张图片,没出现字符;

 2,中文字符没有展示

 3,展示的内容出现重叠

处理中文:

BaseFont bfChinese = BaseFont.createFont(ResourceUtils.getFile("classpath:static/Fonts/simfang.ttf").getAbsolutePath(), 
 BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

在resources下,添加对应的fonts,fonts文件下载

根据类型添加:

在输出其类型的时候,发现字符和图片的是一样的,表格的类型不同,根据类型不同,进行添加

代码:

private final static int FONT_NORMAL = Font.NORMAL;

public static void main(String[] args) {
    String value = "<p>图片导出</p>\n<p>&nbsp;</p>\n<p>啦啦</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p><img src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2F20170105%2Fb01b-fxzkfuk2266724.png&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623917122&t=0eef227b3c5f25073c174523fd5410c7\" alt=\"\" width=\"640\" height=\"640\" /></p>\n<p>&nbsp;</p>\n<p>发放</p>\n<p>&nbsp;</p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 16.6667%;\">大丰收</td>\n<td style=\"width: 16.6667%;\">阿凡达</td>\n<td style=\"width: 16.6667%;\">执行</td>\n<td style=\"width: 16.6667%;\">打算</td>\n<td style=\"width: 16.6667%;\">阿道夫</td>\n<td style=\"width: 16.6667%;\">展旭</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">安抚</td>\n<td style=\"width: 16.6667%;\">啊</td>\n<td style=\"width: 16.6667%;\">的</td>\n<td style=\"width: 16.6667%;\">大</td>\n<td style=\"width: 16.6667%;\">&nbsp;发</td>\n<td style=\"width: 16.6667%;\">发送到</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">阿道夫</td>\n<td style=\"width: 16.6667%;\">ad</td>\n<td style=\"width: 16.6667%;\">答复</td>\n<td style=\"width: 16.6667%;\">阿斯蒂芬</td>\n<td style=\"width: 16.6667%;\">&nbsp;发多少</td>\n<td style=\"width: 16.6667%;\">发的</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">啊</td>\n<td style=\"width: 16.6667%;\">的</td>\n<td style=\"width: 16.6667%;\">更舒服的</td>\n<td style=\"width: 16.6667%;\">国防生的</td>\n<td style=\"width: 16.6667%;\">&nbsp;萨芬的</td>\n<td style=\"width: 16.6667%;\">阿斯蒂芬</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">啊</td>\n<td style=\"width: 16.6667%;\">是大法官</td>\n<td style=\"width: 16.6667%;\">十多个</td>\n<td style=\"width: 16.6667%;\">杀伐果断</td>\n<td style=\"width: 16.6667%;\">杀伐果断</td>\n<td style=\"width: 16.6667%;\">用复合弓的</td>\n</tr>\n</tbody>\n</table>\n<p>&nbsp;</p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p>&nbsp;</p>";

    htmlToPdf(value, "d://exportFile//g"+System.currentTimeMillis()+".pdf");
}

public static void htmlToPdf(String content, String pdfPath) {
    Document document = new Document();
    try {
        BaseFont bfChinese = BaseFont.createFont(ResourceUtils.getFile("classpath:static/Fonts/simfang.ttf").getAbsolutePath(),
                BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        StyleSheet styleSheet = new StyleSheet();
        styleSheet.loadTagStyle("body", "leading", "16,0");

        PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
        document.open();

        List htmlList = HTMLWorker.parseToList(new StringReader(content), styleSheet);
        Paragraph context = new Paragraph();

        context.setKeepTogether(false);
        Font FontChinese = new Font(bfChinese, 10, FONT_NORMAL);
        for (Object aHtmlList : htmlList) {
            Element e = (Element) aHtmlList;
            System.out.println(e.type() + e.getChunks().toString() + ": "+ e.toString());
            int type = e.type();
            if(type == 12){ // 文本和图片
                List chunks = e.getChunks();
                if(CollectionUtils.isEmpty(chunks)){
                    continue;
                }

                for (Object object : chunks) {
                    Chunk chunk = (Chunk) object;
                    HashMap attributes = chunk.getAttributes();
                    if (MapUtils.isEmpty(attributes)) {
                        context = addParagraph(chunk.getContent());
                        context.setIndentationLeft(50);
                        context.setIndentationRight(50);
                    } else {
                        context = addParagraph("");
                        context.setIndentationLeft(50);
                        Image image = chunk.getImage();
                        context.add(image);
                    }
                    document.add(context);
                }

            }else if( type == 23){ // 表格
                PdfPTable table = (PdfPTable) aHtmlList;
                document.add(table);
            }

        }
        document.close();
        System.out.println("ok");
    }catch(Exception e) {
        e.printStackTrace();
    }
}

  这边表格导出来的内容,只有表格,没有实际的内容。这个是老问题了。对于表格的内容,要拎出来,然后放到PdfPTable里面,一个一个设置PdfCell。这个后面再添加。

总结:

   使用 com.Lowagie.itext 导出html内容到pdf上,就没word那么简单,要根据实际的类型一一处理, 要处理好中文。带表格也得额外处理了,需要手动拼接。后面试试其它的方式,看看有没有不用特殊处理表格的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值