springboot项目:生成pdf文件

1、生成一个简单的pdf

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

<dependency>
    <groupId>com.itextpdfgroupId>
    <artifactId>itextpdfartifactId>
    <version>5.5.13version>
dependency>

<dependency>
    <groupId>com.itextpdfgroupId>
    <artifactId>itext-asianartifactId>
    <version>5.2.0version>
dependency>
  • 生成一个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 {
    
    private static final String FILE_NAME = "F:\\pdf\\helloPdf.pdf";

    public static void main(String[] args) throws IOException {
        TestPdf pdf = new TestPdf();
        pdf.createPdf(FILE_NAME);
        log.info("打印完成");
    }

    /**
     * 生成一个hello world
     */
    public void createPdf(String filename)throws IOException {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            document.add(new Paragraph("Hello World!"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

2、导出表格形式的pdf

/**
     * 生成带表格的pdf
     */
    public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
        //设置中文字体
        BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
        Font fontChinese5 = new Font(bf,8);

        PdfPTable table = new PdfPTable(2);//生成一个两列的表格
        //表格垂直居中
        table.setHorizontalAlignment(Element.ALIGN_CENTER);

        PdfPCell cell;
        int size = 15;
        cell = new PdfPCell(new Paragraph("哈哈哈",fontChinese5));
        cell.setFixedHeight(size);//设置高度

        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("two",fontChinese5));
        cell.setFixedHeight(size);

        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("three",fontChinese5));
        cell.setFixedHeight(size);

        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("four",fontChinese5));
        cell.setFixedHeight(size);
        table.addCell(cell);
        return table;
    }

    /**
     * 创建表
     * @throws IOException
     */
    public void createPDF(String filename) throws IOException {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            PdfPTable table = createTable(writer);
            document.add(table);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }

    }

springboot项目:生成pdf文件_第1张图片

3、在看一个例子

springboot项目:生成pdf文件_第2张图片

public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
   //设置中文字体
    BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
    Font fontChinese5 = new Font(bf,8);

    PdfPTable table = new PdfPTable(2);//生成一个两列的表格
    //表格垂直居中
    table.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cell;
    int size = 15;
    cell = new PdfPCell(new Paragraph("one",fontChinese5));
    cell.setFixedHeight(size);//设置高度

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("two",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("three",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("four",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("five",fontChinese5));
    cell.setColspan(1);//设置所占列数
    cell.setRowspan(2);//合并行
    cell.setFixedHeight(size*2);//设置高度
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
    cell.setFixedHeight(size);

    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("six",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("seven",fontChinese5));
    cell.setFixedHeight(size);
    table.addCell(cell);
    return table;
}

你可能感兴趣的:(SpringBoot,itextpdf,生成pdf文件)