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

ps手机网站页面设计wordpress 商品插件

ps手机网站页面设计,wordpress 商品插件,浙江建设招生网站,dw做的网站如何上传图片实现要点说明: 通过PathVariable接收文件路径参数,动态读取指定文件 使用PDFBox创建PDF文档对象并添加内容流 设置响应头为application/pdf和inline模式实现浏览器直接预览 通过ByteArrayOutputStream将PDF转换为字节数组返回 添加中文字体支持依赖确保中…

实现要点说明:
通过@PathVariable接收文件路径参数,动态读取指定文件
使用PDFBox创建PDF文档对象并添加内容流
设置响应头为application/pdf和inline模式实现浏览器直接预览
通过ByteArrayOutputStream将PDF转换为字节数组返回
添加中文字体支持依赖确保中文内容正常显示
使用方式:
启动应用后访问:http://localhost:8080/api/pdf/preview/文件路径
浏览器将自动加载PDF预览界面
支持任意文本文件转换为PDF预览
pom.xml 添加配置

<dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- PDFBox核心库 --><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.29</version></dependency><!-- 中文字体支持 --><dependency><groupId>com.github.librepdf</groupId><artifactId>openpdf</artifactId><version>1.3.30</version></dependency>
</dependencies>

编写controller

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.*;
import java.nio.file.Files;@RestController
@RequestMapping("/api/pdf")
public class PdfController {@GetMapping("/preview/{filePath:.+}")public ResponseEntity<byte[]> previewPdf(@PathVariable String filePath) throws IOException {File file = new File(filePath);if (!file.exists()) {throw new FileNotFoundException("文件不存在");}try (PDDocument document = new PDDocument()) {PDPage page = new PDPage();document.addPage(page);// 读取文件内容并写入PDFtry (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);contentStream.beginText();contentStream.newLineAtOffset(100, 700);contentStream.showText("文件内容预览: " + file.getName());contentStream.newLineAtOffset(0, -20);// 读取文件内容String content = new String(Files.readAllBytes(file.toPath()));contentStream.showText(content.substring(0, Math.min(content.length(), 1000)));contentStream.endText();}ByteArrayOutputStream outputStream = new ByteArrayOutputStream();document.save(outputStream);HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_PDF);headers.setContentDispositionFormData("inline", "preview.pdf");return ResponseEntity.ok().headers(headers).body(outputStream.toByteArray());}}
}

配置文件里面添加:
application.properties

# 允许访问静态资源
spring.mvc.static-path-pattern=/**
spring.web.resources.static-locations=classpath:/static/# 文件上传大小限制
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
http://www.dtcms.com/a/496768.html

相关文章:

  • 好一点网站建设公司网站开发时遇不到算法
  • 阿里云如何添加新网站做数学题网站
  • flash网站cms合肥网站建设哪家专业
  • JEP、exp4j和Expression4j解析公式对比
  • 756ka网站建设博客发布 网站模版
  • Python| intake | 无法将读取的nc数据转为xarray格式 | to_dask()
  • 电商导购网站开发四川企业seo
  • VLLM-大模型部署Qwen3-8b
  • @FeignClient 添加请求头header的4种方法
  • 网站建设人员考核设计师联盟网
  • python做个人网站前端后端
  • git一个分支最后一条记录合并到另一条分支上
  • 阿里云可以做电影网站吗淘宝网站建设概要
  • Coze 与 n8n 深度对比:AI智能体平台与工作流自动化的核心博弈
  • 西安正规网站建设报价做个网站app吗
  • 做网站主要学什么软件室内设计网站公司
  • 【Leetcode hot 100】45.跳跃游戏Ⅱ
  • 北京建设局网站百度seo快排软件
  • 免费优化网站建设中建一局
  • 流氓软件AlibabaProtect无需第三方软件无需重启电脑的清理方法
  • 自助建站网站手机网站设计规范
  • 平邑县建设局网站wordpress安装在哪
  • 东莞网站seo价格电脑系统重装wordpress
  • 做网站建设销售途径四川省住建设厅网站
  • 简单大气的企业网站广东网站建设建站模板
  • 2017自己做网站的趋势网络营销定价的特点有
  • BT BlueZ软硬件环境介绍
  • 智能防雷产品应用解决方案
  • 怎么样申请网站域名优秀的包装设计案例
  • 网站维护要求wordpress最好的编辑器下载地址