java天生pdf通过接口下载

河曲智叟  金牌会员 | 2024-6-24 06:05:14 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 528|帖子 528|积分 1584

pom
  1. <!--生成pdf-->
  2. <dependency>
  3.     <groupId>com.itextpdf</groupId>
  4.     <artifactId>itextpdf</artifactId>
  5.     <version>5.5.11</version>
  6. </dependency>
复制代码
demo
  1. package com.example.controller;
  2. import com.itextpdf.text.*;
  3. import com.itextpdf.text.pdf.PdfContentByte;
  4. import com.itextpdf.text.pdf.PdfGState;
  5. import com.itextpdf.text.pdf.PdfWriter;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import com.itextpdf.text.pdf.BaseFont;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.*;
  12. import java.net.URLEncoder;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. @RestController
  16. @RequestMapping("pdf")
  17. public class PdfController {
  18.     @GetMapping("/createPdfDownload")
  19.     public String createPdfToDownload(HttpServletResponse response) throws Exception {
  20.         // 生成 pdf 名称
  21.         Date date = new Date();
  22.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  23.         String str = simpleDateFormat.format(date);
  24.         String datetime = str.replace("-", "").replace(" ", "").replace(":","");
  25.         String filename = "pdf_"+datetime+"_content.pdf";
  26.         // 创建PDF
  27.         Document document = new Document(PageSize.A4,60,60,15,40);
  28.         try {
  29.             // 设置响应头,控制浏览器下载该文件
  30.             response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
  31.             //常用的有paragraph段落、phrase语句块、chunk最小单位块
  32.             OutputStream out = response.getOutputStream();
  33.             PdfWriter writer = PdfWriter.getInstance(document, out);
  34.             // 打开文档
  35.             document.open();
  36.             BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  37.             for(int n = 0; n<2; n++){
  38.                 // 添加文字水印
  39.                 PdfContentByte cb = writer.getDirectContent();
  40.                 cb.beginText(); // 开始
  41.                 // 设置透明度
  42.                 PdfGState gs = new PdfGState();
  43.                 gs.setFillOpacity(0.2f);
  44.                 cb.setGState(gs);
  45.                 cb.setFontAndSize(bfChinese,100);
  46.                 cb.showTextAligned(Element.ALIGN_CENTER, "北京交通大学", 340, 410 , 60);
  47.                 cb.endText(); // 结束
  48.                 // 添加标题
  49.                 //通过Font去设置字体的基本属性:大小,加粗等等
  50.                 Font font  = new Font(bfChinese, 15, Font.NORMAL, BaseColor.BLACK);
  51.                 // 创建段落
  52.                 Paragraph title = new Paragraph("我是 title ", font);
  53.                 title.setAlignment(Element.ALIGN_CENTER);
  54.                 title.setSpacingBefore(40f);
  55.                 document.add(title);
  56.                 // 制作大文本数据
  57.                 StringBuilder stringBuilder = new StringBuilder();
  58.                 for (int i = 0; i <= 10; i++){
  59.                     stringBuilder.append("如果说荷兰是橙色的,那阿姆斯特丹就是缤纷的彩色。");
  60.                 }
  61.                 Font font1  = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);
  62.                 Paragraph context = new Paragraph(stringBuilder.toString(), font1);
  63.                 context.setFirstLineIndent(20);
  64.                 context.setLeading(12);
  65.                 context.setSpacingBefore(10f);
  66.                 document.add(context);
  67.                 // 开启新的一页
  68.                 document.newPage();
  69.                 //显示空内容的页
  70.                 writer.setPageEmpty(false);
  71.             }
  72.             // 关闭流
  73.             document.close();
  74.             writer.close();
  75.         } catch (IOException e) {
  76.             throw new RuntimeException(e);
  77.         } catch (DocumentException e) {
  78.             throw new RuntimeException(e);
  79.         }
  80.         return "ok";
  81.     }
  82. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表