美文网首页
Java程序生成PDF简单实例

Java程序生成PDF简单实例

作者: 我的马儿有些瘦 | 来源:发表于2017-05-10 19:50 被阅读0次

在实习开发中,生成PDF或Excel表格是很常见的功能,但是有时往往在短时间之内又不能很好的写出功能齐全的代码,所以简单的列一个模板。

首先,需要几个相关夹包的支持,将其加入到类路径地下方可使用:

iTextAsian

这个包在maven中央仓库是找不到,只有在Spring4.x企业应用开发实战这本书的网盘上有,版本有点旧,在依赖itext-jar时千万要注意,不要依赖最新的jar,不然版本不兼容,无法解析 。

<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

基本条件配置完成后,就可以实现代码的编写了。

具体代码实现

1、AttendancePdfView - 考勤报表的PDF视图
package com.qfedu.sail.controller.lgj;

import java.awt.Color;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.qfedu.sail.entity.Attendance;

@Component
public class AttendancePdfView extends AbstractPdfView {

    @Override
    protected void buildPdfDocument(Map<String, Object> model, 
            Document document, PdfWriter writer, HttpServletRequest req,
            HttpServletResponse resp) throws Exception {
        
        resp.setHeader("Content-Disposition", "inline;filename="+ 
        new String("考勤报表".getBytes(),"iso-8859-1"));
        List<Attendance> attendanceList = (List<Attendance>) model.get("aList");
        for (Attendance attendance : attendanceList) {
            System.out.println(attendance.getAid());
        }
        Table table = new Table(3);
        table.setWidth(80);
        table.setBorder(1);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        
        BaseFont cnBaseFont = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
        Font cnFont = new Font(cnBaseFont,10,Font.NORMAL,Color.BLUE);
        
        table.addCell(buildFontCell("编号",cnFont));
        table.addCell(buildFontCell("姓名",cnFont));
        table.addCell(buildFontCell("打卡时间",cnFont));
        
        for(Attendance attendance:attendanceList) {
            table.addCell(buildFontCell(attendance.getUser().getUid().toString(), cnFont));
            table.addCell(buildFontCell(attendance.getUser().getUserName(), cnFont));
            table.addCell(buildFontCell(attendance.getClockInTime().toString(),cnFont)); 
        }
        document.add(table);
    }
    
    private Cell buildFontCell(String content,Font font) throws RuntimeException{   
            try {
                Phrase phrase = new Phrase(content,font);
                return new Cell(phrase);
            } catch (BadElementException e) {
                throw new RuntimeException(e);
            }
    }

}
2、showAttendanceListByPdf-controller
@RequestMapping("/showAttendanceListByPdf") 
    public String showAttendanceListByPdf(ModelMap mm) {
        List<Attendance> aList = new ArrayList<>();
        aList = attendanceService.findAll();
        mm.addAttribute("aList",aList);
        return "attendanceListPdf";
    }
3、想要生成PDF还需要在springMVC的配置文件中将视图配置成bean,同时还需要添加一个视图解析器,具体如下:
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
        <property name="order" value="10" />
    </bean>
    <bean id="attendanceListPdf" class="com.qfedu.sail.controller.lgj.AttendancePdfView" />

相关文章

  • Java程序生成PDF简单实例

    在实习开发中,生成PDF或Excel表格是很常见的功能,但是有时往往在短时间之内又不能很好的写出功能齐全的代码,所...

  • Java生成PDF

    Java生成PDF有两种办法:1.利用PDF模板,读入模板,填充数据,生成带数据的PDF。 ...

  • pdf生成

    Java生成PDF有两种办法:1.利用PDF模板,读入模板,填充数据,生成带数据的PDF。 Jasperrepor...

  • java 教程-我的第一个JAVA程序

    Java视频教程 我的第一个JAVA程序 以下我们通过一个简单的实例来展示Java编程,本实例输出"编程字典,Ja...

  • Java程序优化

    Java程序优化 参考书籍: Java程序性能优化 让你的Java程序更快、更稳定.pdf 性能分析-程序性能指...

  • java生成pdf

    简述生成pdf思路主要是分两种情况,情况一:如果模板里面不需要数组参数,那么可以根据html模板直接生成pdf;情...

  • 类方法与实例方法的区别

    一、区别 1、静态方法在程序开始时生成内存,实例方法在程序运行中生成内存,所以静态方法可以直接调用。 2、实例方法...

  • java基本程序设计

    先来一段简单的java代码 注意大小写,这是一个Java程序正确的结构 java /** **/自动生成文档 un...

  • Java实例教程(上)

    第一个Java程序Java 枚举Java注释Java创建对象Java访问实例变量和方法Java局部变量实例 Jav...

  • java.lang.NoClassDefFoundError:

    spring+jasperReport生成PDF格式的报表时报 java.lang.NoClassDefFound...

网友评论

      本文标题:Java程序生成PDF简单实例

      本文链接:https://www.haomeiwen.com/subject/ofngtxtx.html