欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > java导出pdf

java导出pdf

2025/9/18 0:21:50 来源:https://blog.csdn.net/evalsys/article/details/143735224  浏览:    关键词:java导出pdf

引入包

    <properties><itext.version>8.0.5</itext.version></properties><dependencies><dependency><groupId>com.itextpdf</groupId><artifactId>itext-core</artifactId><version>${itext.version}</version><type>pom</type></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>bouncy-castle-adapter</artifactId><version>${itext.version}</version></dependency></dependencies>

直接参考代码

import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;@ApiOperation("导出餐别统计")@PostMapping("/exportMealPdf")public void exportMealPdf(@RequestBody ChargeTypeReportPageDTO query, HttpServletResponse response) throws IOException {response.setContentType("application/pdf");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode("餐别统计表", "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".pdf");PdfDocument pdfDoc = new PdfDocument(new PdfWriter(response.getOutputStream()));Document doc = new Document(pdfDoc);// 设置中文字体PdfFont pdfFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);;doc.setFont(pdfFont);doc.add(new Paragraph("With 3 columns:"));Table table = new Table(UnitValue.createPercentArray(new float[] {10, 10, 80}));table.setMarginTop(5);table.addCell("Col a");table.addCell("Col b");table.addCell("Col c");table.addCell("Value a");table.addCell("Value b");table.addCell("This is a long description for column c. " +"It needs much more space hence we made sure that the third column is wider.");doc.add(table);doc.add(new Paragraph("With 2 columns:"));table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();table.setMarginTop(5);table.addCell("Col a");table.addCell("Col b");table.addCell("Value a");table.addCell("Value b");table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +"It needs much more space hence we made sure that the third column is wider.")));table.addCell("Col a");table.addCell("Col b");table.addCell("Value a");table.addCell("Value b");table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +"It needs much more space hence we made sure that the third column is wider.")));doc.add(table);doc.close();}

官网地址

itext-publications-examples-java/src/main/java/com/itextpdf/samples/sandbox/tables/SimpleTable9.java at master · itext/itext-publications-examples-java · GitHub

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词