一、SpringBoot导出数据为PDF
1、添加所需依赖
< dependency> < groupId> com. itextpdf< / groupId> < artifactId> itextpdf< / artifactId> < version> 5.5 .11 < / version>
< / dependency>
< dependency> < groupId> com. itextpdf. tool< / groupId> < artifactId> xmlworker< / artifactId> < version> 5.5 .11 < / version>
< / dependency>
2、编写生成工具方法
public class PdfFontUtil { private PdfFontUtil ( ) { } public static Paragraph getParagraph ( String content, Font font, Integer alignment) { Paragraph paragraph = new Paragraph ( content, font) ; if ( alignment != null && alignment >= 0 ) { paragraph. setAlignment ( alignment) ; } return paragraph ; } public static Image getImage ( String imgPath, float width, float height) throws Exception { Image image = Image . getInstance ( imgPath) ; image. setAlignment ( Image . MIDDLE) ; if ( width > 0 && height > 0 ) { image. scaleAbsolute ( width, height) ; } return image ; } public static PdfPTable getPdfPTable01 ( int numColumns, float totalWidth) throws Exception { PdfPTable table = new PdfPTable ( numColumns) ; table. setWidthPercentage ( 100 ) ; table. setTotalWidth ( totalWidth) ; table. setLockedWidth ( true ) ; table. setSpacingBefore ( 10f ) ; table. setSpacingAfter ( 10f ) ; table. getDefaultCell ( ) . setBorder ( 0 ) ; table. setPaddingTop ( 50 ) ; table. setSplitLate ( false ) ; return table ; } public static PdfPCell getPdfPCell ( Phrase phrase) { return new PdfPCell ( phrase) ; } public static void addTableCell ( PdfPTable dataTable, Font font, List < String > cellList) { for ( String content: cellList) { dataTable. addCell ( getParagraph ( content, font, - 1 ) ) ; } }
}
3、调用方法生成PDF
public class PdfPage01 { private static String PDF_SITE = "G:\\file\\PDF页面.pdf" ; private static String FONT = "G:/file/Fonts/Simhei.ttf" ; private static String PAGE_TITLE = "PDF数据导出报告" ; private static Font TITLE_FONT = FontFactory . getFont ( FONT, BaseFont . IDENTITY_H, 20 , Font . BOLD) ; private static Font NODE_FONT = FontFactory . getFont ( FONT, BaseFont . IDENTITY_H, 15 , Font . BOLD) ; private static Font BLOCK_FONT = FontFactory . getFont ( FONT, BaseFont . IDENTITY_H, 13 , Font . BOLD, BaseColor . BLACK) ; private static Font INFO_FONT = FontFactory . getFont ( FONT, BaseFont . IDENTITY_H, 12 , Font . NORMAL, BaseColor . BLACK) ; private static Font CONTENT_FONT = FontFactory . getFont ( FONT, BaseFont . IDENTITY_H, BaseFont . NOT_EMBEDDED) ; private static void createPdfPage ( ) throws Exception { Document document = new Document ( ) ; PdfWriter writer = PdfWriter . getInstance ( document, new FileOutputStream ( PDF_SITE) ) ; document. open ( ) ; document. add ( PdfFontUtil . getParagraph ( PAGE_TITLE, TITLE_FONT, 1 ) ) ; document. add ( PdfFontUtil . getParagraph ( "\n商户名称:XXX科技有限公司" , INFO_FONT, - 1 ) ) ; document. add ( PdfFontUtil . getParagraph ( "\n生成时间:2021-09-02\n\n" , INFO_FONT, - 1 ) ) ; document. add ( PdfFontUtil . getParagraph ( "城市数据分布统计" , NODE_FONT, - 1 ) ) ; document. add ( PdfFontUtil . getParagraph ( "\n· 可视化图表\n\n" , BLOCK_FONT, - 1 ) ) ; float documentWidth = document. getPageSize ( ) . getWidth ( ) - document. leftMargin ( ) - document. rightMargin ( ) ; float documentHeight = documentWidth / 580 * 320 ; document. add ( PdfFontUtil . getImage ( "F:\\file-type\\myChart.jpg" , documentWidth- 80 , documentHeight- 80 ) ) ; document. add ( PdfFontUtil . getParagraph ( "\n· 数据详情\n\n" , BLOCK_FONT, - 1 ) ) ; PdfPTable dataTable = PdfFontUtil . getPdfPTable01 ( 6 , 500 ) ; List < String > tableHeadList = tableHead ( ) ; List < List < String > > tableDataList = getTableData ( ) ; PdfFontUtil . addTableCell ( dataTable, CONTENT_FONT, tableHeadList) ; for ( List < String > tableData : tableDataList) { PdfFontUtil . addTableCell ( dataTable, CONTENT_FONT, tableData) ; } document. add ( dataTable) ; document. add ( PdfFontUtil . getParagraph ( "\n· 报表描述\n\n" , BLOCK_FONT, - 1 ) ) ; document. add ( PdfFontUtil . getParagraph ( "数据报告可以监控每天的推广情况," + "可以针对不同的数据表现进行分析,以提升推广效果。" , CONTENT_FONT, - 1 ) ) ; document. newPage ( ) ; document. close ( ) ; writer. close ( ) ; } private static List < List < String > > getTableData ( ) { List < List < String > > tableDataList = new ArrayList < > ( ) ; for ( int i = 0 ; i < 3 ; i++ ) { List < String > tableData = new ArrayList < > ( ) ; tableData. add ( "浙江" + i) ; tableData. add ( "杭州" + i) ; tableData. add ( "276" + i) ; tableData. add ( "33.3%" ) ; tableData. add ( "33.3%" ) ; tableData. add ( "33.3%" ) ; tableData. add ( "33.3%" ) ; tableData. add ( "33.3%" ) ; tableDataList. add ( tableData) ; } return tableDataList ; } private static List < String > tableHead ( ) { List < String > tableHeadList = new ArrayList < > ( ) ; tableHeadList. add ( "省份" ) ; tableHeadList. add ( "城市" ) ; tableHeadList. add ( "数量" ) ; tableHeadList. add ( "百分比" ) ; tableHeadList. add ( "百分比" ) ; tableHeadList. add ( "百分比" ) ; tableHeadList. add ( "百分比2" ) ; tableHeadList. add ( "百分比1" ) ; return tableHeadList ; } public static void main ( String [ ] args) throws Exception { createPdfPage ( ) ; }
}
4、最终效果