maven,Springboot项目生成pdf文件

这里需要使用依赖

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

比较简单,直接贴代码

package com.ykq.pdf_test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestPdf {
	  public static void main(String[] args) throws Exception {
	    
	    TestPdf pdf = new TestPdf();
	    String filename = "/Users/yuankaiqiang/Desktop/测试.pdf";
	    pdf.createPDF(filename);
	    System.out.println("打印完成");
	    
	}
	  public void createPDF(String filename) throws IOException, DocumentException {
		    Document document = new Document(PageSize.A4);
		    try {
		    	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
		        document.addTitle("example of PDF");
		        document.open();
		        PdfPTable table = createTable(writer);
		        document.add(table);
		    } catch (FileNotFoundException e) {
		        e.printStackTrace();
		    } catch (DocumentException e) {
		        e.printStackTrace();
		    } finally {
		        document.close();
		    }
	  }
		public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException{
			BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font font = new Font(bfChinese, 10, Font.NORMAL);
			PdfPTable table = new PdfPTable(2);//生成一个两列的表格
		    PdfPCell cell;
		    int size = 20;
		    cell = new PdfPCell(new Paragraph("姓名",font));
		    cell.setFixedHeight(size);
		    table.addCell(cell);
		    cell = new PdfPCell(new Paragraph("专业",font));
		    cell.setFixedHeight(size);
		    table.addCell(cell);
		    cell = new PdfPCell(new Paragraph("张三",font));
		    cell.setFixedHeight(size);
		    table.addCell(cell);
		    cell = new PdfPCell(new Paragraph("计算机科学与技术",font));
		    cell.setFixedHeight(size);
		    table.addCell(cell);
		    cell = new PdfPCell(new Paragraph("评分",font));
		    cell.setColspan(1);//设置所占列数
		    cell.setRowspan(2);//合并行
		    cell.setFixedHeight(size*2);//设置高度
		    cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
		    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
		    table.addCell(cell);
		    cell = new PdfPCell(new Paragraph("合格",font));
		    cell.setFixedHeight(size);
		    table.addCell(cell);
		    cell = new PdfPCell(new Paragraph("优秀",font));
		    cell.setFixedHeight(size);
		    table.addCell(cell);
		    return table;
		  }
}

运行以后会在对应的目录生成PDF文件
在这里插入图片描述
有问题可以私聊我或者评论我会尽快回复,如果文章对你有帮助,请点个赞,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小凯77

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

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

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

打赏作者

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

抵扣说明:

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

余额充值