在spring boot中生成PDF

首先在pom.xml中加入下列依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
			<version>1.5.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf.tool</groupId>
			<artifactId>xmlworker</artifactId>
			<version>5.5.11</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version> 5.5.13</version>
		</dependency>

下面开始生成PDF的代码

 public void createPDF() {
        //设置纸张大小
        Document document = new Document(PageSize.A4);
        try {
            //设置PDF存放路径
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\DJLFTPPDF\\PDFDEMO/testTable.pdf"));
            document.open();
            //生成一个表格
            PdfPTable table = this.createTable();
            //生成一张图片
            Image png = this.createImage();
            //将表格添加PDF进去
            document.add(table);
            //将图片添加到PDF
            document.add(png);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
    //生成表格方法
    public static PdfPTable createTable() throws DocumentException, IOException{
        PdfPTable table = new PdfPTable(2);//生成一个两列的表格
        //渎系统中的字体
        Font font = new Font(BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED));
        PdfPCell cell;
        //设置每列高度
        int size = 50;
        //按顺序向表格中添加值(输入汉字要加font)
        cell = new PdfPCell(new Phrase("董津林你真帅",font));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("two"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("three"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("four"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        //最后一行给合并起来
        cell = new PdfPCell(new Phrase("five"));
        cell.setColspan(2);//设置所占列数
        cell.setFixedHeight(size*2);//设置高度
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
        table.addCell(cell);

        return table;
    }

    public Image createImage() throws Exception{
        //寻找图片路径
        Image png = Image.getInstance("D:\\dongjinlin\\360wallpaper.jpg");
        //图片大小
        png.scaleToFit(500,500);
        //图片位置
        png.setAbsolutePosition(50,300);
        return png;
    }

生成效果
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,下面是一个使用Spring Boot和iText库生成PDF文件的简单示例。 1. 引入依赖 在您的Spring Boot项目的pom.xml文件,添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> ``` 2. 编写生成PDF文件的代码 在您的Java,编写生成PDF文件的逻辑。以下是一个简单的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import org.springframework.stereotype.Service; import java.io.FileOutputStream; @Service public class PdfService { public void generatePdf() throws Exception { // 创建一个文档对象 Document document = new Document(PageSize.A4, 50, 50, 50, 50); // 创建一个PdfWriter对象 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("example.pdf")); // 打开文档对象 document.open(); // 添加内容到文档 document.add(new Paragraph("Hello, World!")); // 关闭文档对象 document.close(); // 关闭PdfWriter对象 writer.close(); } } ``` 在上面的代码,我们使用iText库创建了一个文档对象和一个PdfWriter对象,然后打开文档对象、添加内容、关闭文档对象和PdfWriter对象。最后,我们将生成PDF文件保存到本地磁盘上。 3. 调用生成PDF文件的代码 在您的Spring Boot应用程序,您可以通过调用PdfService的generatePdf()方法来生成PDF文件。例如,您可以在某个控制器方法调用该方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class PdfController { @Autowired private PdfService pdfService; @GetMapping("/pdf") public String generatePdf() throws Exception { pdfService.generatePdf(); return "PDF generated successfully!"; } } ``` 当您访问“/pdf”端点时,将调用PdfController的generatePdf()方法生成PDF文件,并返回“PDF generated successfully!”的消息。 以上是一个简单的使用Spring Boot和iText库生成PDF文件的示例。当然,您可以根据自己的需求来编写更加复杂的代码逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值