您的位置:首页 > 娱乐 > 八卦 > java生成pdf通过接口下载

java生成pdf通过接口下载

2024/12/23 15:24:48 来源:https://blog.csdn.net/qq_28011629/article/details/139452950  浏览:    关键词:java生成pdf通过接口下载

pom

<!--生成pdf-->
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.11</version>
</dependency>

demo

package com.example.controller;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itextpdf.text.pdf.BaseFont;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;@RestController
@RequestMapping("pdf")
public class PdfController {@GetMapping("/createPdfDownload")public String createPdfToDownload(HttpServletResponse response) throws Exception {// 生成 pdf 名称Date date = new Date();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String str = simpleDateFormat.format(date);String datetime = str.replace("-", "").replace(" ", "").replace(":","");String filename = "pdf_"+datetime+"_content.pdf";// 创建PDFDocument document = new Document(PageSize.A4,60,60,15,40);try {// 设置响应头,控制浏览器下载该文件response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));//常用的有paragraph段落、phrase语句块、chunk最小单位块OutputStream out = response.getOutputStream();PdfWriter writer = PdfWriter.getInstance(document, out);// 打开文档document.open();BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);for(int n = 0; n<2; n++){// 添加文字水印PdfContentByte cb = writer.getDirectContent();cb.beginText(); // 开始// 设置透明度PdfGState gs = new PdfGState();gs.setFillOpacity(0.2f);cb.setGState(gs);cb.setFontAndSize(bfChinese,100);cb.showTextAligned(Element.ALIGN_CENTER, "北京交通大学", 340, 410 , 60);cb.endText(); // 结束// 添加标题//通过Font去设置字体的基本属性:大小,加粗等等Font font  = new Font(bfChinese, 15, Font.NORMAL, BaseColor.BLACK);// 创建段落Paragraph title = new Paragraph("我是 title ", font);title.setAlignment(Element.ALIGN_CENTER);title.setSpacingBefore(40f);document.add(title);// 制作大文本数据StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i <= 10; i++){stringBuilder.append("如果说荷兰是橙色的,那阿姆斯特丹就是缤纷的彩色。");}Font font1  = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);Paragraph context = new Paragraph(stringBuilder.toString(), font1);context.setFirstLineIndent(20);context.setLeading(12);context.setSpacingBefore(10f);document.add(context);// 开启新的一页document.newPage();//显示空内容的页writer.setPageEmpty(false);}// 关闭流document.close();writer.close();} catch (IOException e) {throw new RuntimeException(e);} catch (DocumentException e) {throw new RuntimeException(e);}return "ok";}
}

版权声明:

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

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