当前位置: 首页 > news >正文

Spring Boot 简单预览PDF例子

目录

前言

一、引入依赖

二、使用步骤

1.创建 Controller 处理 PDF 生成和预览

2.创建预览页面

总结



前言

使用 Spring Boot 创建一个生成 PDF 并进行预览的项目,你可以按以下步骤进行。我们将使用 Spring BootThymeleafiText 等技术来完成这个任务。


一、引入依赖

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>pdf-preview</artifactId><version>0.0.1-SNAPSHOT</version><name>pdf-preview</name><description>Spring Boot PDF Preview</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.4</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Starter Thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- iText PDF Library for PDF generation --><dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.2.6</version><type>pom</type></dependency><!-- Spring Boot Starter Test (for unit tests) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

二、使用步骤

1.创建 Controller 处理 PDF 生成和预览

        

package com.example.pdfpreview;import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;import java.io.ByteArrayOutputStream;
import java.io.IOException;@Controller
public class PdfController {@GetMapping("/generate-pdf/{text}")public ResponseEntity<InputStreamResource> generatePdf(@PathVariable String text) throws IOException {// Step 1: Create a PDF documentByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();PdfWriter writer = new PdfWriter(byteArrayOutputStream);PdfDocument pdfDocument = new PdfDocument(writer);Document document = new Document(pdfDocument);document.add(new Paragraph(text));// Close document to finish writingdocument.close();// Step 2: Prepare the response as a downloadable PDFInputStreamResource resource = new InputStreamResource(byteArrayOutputStream.toInputStream());return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=generated.pdf").contentType(org.springframework.http.MediaType.APPLICATION_PDF).contentLength(byteArrayOutputStream.size()).body(resource);}@GetMapping("/preview-pdf")public String previewPdf(Model model) {// This page can be used for simple preview before generating the PDFmodel.addAttribute("previewText", "Hello, this is a sample preview text for your PDF!");return "preview";}
}

2.创建预览页面

src/main/resources/templates 目录下创建一个名为 preview.html 的 Thymeleaf 页面

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>PDF Preview</title>
</head>
<body><h1>PDF Preview</h1><p>Preview Text: <strong th:text="${previewText}"></strong></p><form action="/generate-pdf/{text}" method="get"><input type="text" name="text" value="Hello, World!" /><button type="submit">Generate PDF</button></form>
</body>
</html>

 


总结

http://www.lryc.cn/news/486517.html

相关文章:

  • 【魔珐有言-注册/登录安全分析报告-无验证方式导致安全隐患】
  • LabVIEW 使用 Snippet
  • 单片机_day3_GPIO
  • Python小游戏24——小恐龙躲避游戏
  • Python 的多态笔记
  • go module使用
  • c ++零基础可视化——数组
  • CVE-2024-2961漏洞的简单学习
  • 计算机组成原理笔记----基础篇
  • TheadLocal出现的内存泄漏具体泄漏的是什么?弱引用在里面有什么作用?什么情景什么问题?
  • AI在电商平台中的创新应用:提升销售效率与用户体验的数字化转型
  • CTF-RE 从0到N:RC4
  • HbuilderX 插件开发-模板创建
  • 打造专业问答社区:Windows部署Apache Answer结合cpolar实现公网访问
  • YOLO-SLD: An Attention Mechanism-ImprovedYOLO for License Plate Detection
  • ArcGIS的汉字(亚洲文本)垂直标注
  • 【面试题】
  • Leetcode 寻找峰值
  • 探索大规模语言模型(LLM)在心理健康护理领域中的应用与潜力
  • Infisical开源密钥管理平台实战指南
  • AI大模型:重塑软件开发流程与模式
  • AMD(Xilinx) FPGA配置Flash大小选择
  • 基于Java Springboot图书借阅系统
  • DDRPHY数字IC后端设计实现系列专题之数字后端floorplanpowerplan设计
  • 【Mysql】Mysql函数(上)
  • Java连接MySQL(测试build path功能)
  • 卡尔曼滤波器
  • 基于BERT的情感分析
  • 推荐15个2024最新精选wordpress模板
  • AWTK-WIDGET-WEB-VIEW 实现笔记 (2) - Windows