springboot项目:生成pdf文件

骑猪看日落 2023-06-14 13:53 138阅读 0赞

1、生成一个简单的pdf

  • 创建springboot项目,这里不多说
  • 导入依赖


    com.itextpdf
    itextpdf
    5.5.13



    com.itextpdf
    itext-asian
    5.2.0
  • 生成一个helloword的pdf

    package com.example.demo;

    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.pdf.PdfWriter;
    import jdk.nashorn.internal.runtime.logging.Logger;
    import lombok.extern.slf4j.Slf4j;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    / @ClassName TestPdf @Description 生成hello world @Author 戴某某 @Date 2019/11/19 20:29 * @Version 1.0 /
    @Slf4j
    public class TestPdf {

    1. private static final String FILE_NAME = "F:\\pdf\\helloPdf.pdf";
    2. public static void main(String[] args) throws IOException {
    3. TestPdf pdf = new TestPdf();
    4. pdf.createPdf(FILE_NAME);
    5. log.info("打印完成");
    6. }
    7. /** * 生成一个hello world */
    8. public void createPdf(String filename)throws IOException {
    9. Document document = new Document(PageSize.A4);
    10. try {
    11. PdfWriter.getInstance(document, new FileOutputStream(filename));
    12. document.addTitle("example of PDF");
    13. document.open();
    14. document.add(new Paragraph("Hello World!"));
    15. } catch (FileNotFoundException e) {
    16. e.printStackTrace();
    17. } catch (DocumentException e) {
    18. e.printStackTrace();
    19. } finally {
    20. document.close();
    21. }
    22. }

    }

2、导出表格形式的pdf

  1. /** * 生成带表格的pdf */
  2. public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
  3. //设置中文字体
  4. BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
  5. Font fontChinese5 = new Font(bf,8);
  6. PdfPTable table = new PdfPTable(2);//生成一个两列的表格
  7. //表格垂直居中
  8. table.setHorizontalAlignment(Element.ALIGN_CENTER);
  9. PdfPCell cell;
  10. int size = 15;
  11. cell = new PdfPCell(new Paragraph("哈哈哈",fontChinese5));
  12. cell.setFixedHeight(size);//设置高度
  13. table.addCell(cell);
  14. cell = new PdfPCell(new Paragraph("two",fontChinese5));
  15. cell.setFixedHeight(size);
  16. table.addCell(cell);
  17. cell = new PdfPCell(new Paragraph("three",fontChinese5));
  18. cell.setFixedHeight(size);
  19. table.addCell(cell);
  20. cell = new PdfPCell(new Paragraph("four",fontChinese5));
  21. cell.setFixedHeight(size);
  22. table.addCell(cell);
  23. return table;
  24. }
  25. /** * 创建表 * @throws IOException */
  26. public void createPDF(String filename) throws IOException {
  27. Document document = new Document(PageSize.A4);
  28. try {
  29. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  30. document.addTitle("example of PDF");
  31. document.open();
  32. PdfPTable table = createTable(writer);
  33. document.add(table);
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (DocumentException e) {
  37. e.printStackTrace();
  38. } finally {
  39. document.close();
  40. }
  41. }

在这里插入图片描述

3、在看一个例子

在这里插入图片描述

  1. public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
  2. //设置中文字体
  3. BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
  4. Font fontChinese5 = new Font(bf,8);
  5. PdfPTable table = new PdfPTable(2);//生成一个两列的表格
  6. //表格垂直居中
  7. table.setHorizontalAlignment(Element.ALIGN_CENTER);
  8. PdfPCell cell;
  9. int size = 15;
  10. cell = new PdfPCell(new Paragraph("one",fontChinese5));
  11. cell.setFixedHeight(size);//设置高度
  12. table.addCell(cell);
  13. cell = new PdfPCell(new Paragraph("two",fontChinese5));
  14. cell.setFixedHeight(size);
  15. table.addCell(cell);
  16. cell = new PdfPCell(new Paragraph("three",fontChinese5));
  17. cell.setFixedHeight(size);
  18. table.addCell(cell);
  19. cell = new PdfPCell(new Paragraph("four",fontChinese5));
  20. cell.setFixedHeight(size);
  21. table.addCell(cell);
  22. cell = new PdfPCell(new Paragraph("five",fontChinese5));
  23. cell.setColspan(1);//设置所占列数
  24. cell.setRowspan(2);//合并行
  25. cell.setFixedHeight(size*2);//设置高度
  26. cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
  27. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
  28. cell.setFixedHeight(size);
  29. table.addCell(cell);
  30. cell = new PdfPCell(new Paragraph("six",fontChinese5));
  31. cell.setFixedHeight(size);
  32. table.addCell(cell);
  33. cell = new PdfPCell(new Paragraph("seven",fontChinese5));
  34. cell.setFixedHeight(size);
  35. table.addCell(cell);
  36. return table;
  37. }

发表评论

表情:
评论列表 (有 0 条评论,138人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Itext生成PDF文件

    最近做一个项目的报表,设计导出问题,要求pdf、excel、word。说实话一个字,烦。写个备忘录吧。。。虽然还是很烦 所需依赖:itext-asian-5.2.0.jar