Spring、iText - 从许多实体创建 PDF

Spring, iText - Create PDF from many entities

我有 9 个具有不同字段数的不同实体 我正在尝试从所有这些文件创建 PDF 文件。我找到了这个用于 PDF 创建的解决方案:

public class PDFGenerator {

  private static Logger logger = LoggerFactory.getLogger(PDFGenerator.class);

  public static ByteArrayInputStream customerPDFReport(List<Customer> customers) {
    Document document = new Document();
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {

          PdfWriter.getInstance(document, out);
            document.open();

            // Add Text to PDF file ->
          Font font = FontFactory.getFont(FontFactory.COURIER, 14, BaseColor.BLACK);
          Paragraph para = new Paragraph( "Customer Table", font);
          para.setAlignment(Element.ALIGN_CENTER);
          document.add(para);
          document.add(Chunk.NEWLINE);

          PdfPTable table = new PdfPTable(3);
          // Add PDF Table Header ->
            Stream.of("ID", "First Name", "Last Name")
              .forEach(headerTitle -> {
                  PdfPCell header = new PdfPCell();
                  Font headFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
                  header.setBackgroundColor(BaseColor.LIGHT_GRAY);
                  header.setHorizontalAlignment(Element.ALIGN_CENTER);
                  header.setBorderWidth(2);
                  header.setPhrase(new Phrase(headerTitle, headFont));
                  table.addCell(header);
              });

            for (Customer customer : customers) {
              PdfPCell idCell = new PdfPCell(new Phrase(customer.getId().toString()));
              idCell.setPaddingLeft(4);
              idCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
              idCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(idCell);

                PdfPCell firstNameCell = new PdfPCell(new Phrase(customer.getFirstName()));
                firstNameCell.setPaddingLeft(4);
                firstNameCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                firstNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(firstNameCell);

                PdfPCell lastNameCell = new PdfPCell(new Phrase(String.valueOf(customer.getLastName())));
                lastNameCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                lastNameCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                lastNameCell.setPaddingRight(4);
                table.addCell(lastNameCell);
            }
            document.add(table);

            document.close();
        }catch(DocumentException e) {
          logger.error(e.toString());
        }

    return new ByteArrayInputStream(out.toByteArray());
  }
}

此解决方案使用 Stream.of("ID", "First Name", "Last Name")customer.getId().toString() 但我不知道用户要为哪个实体创建 PDF,所以我无法使用 get 方法。

我需要像 AbstractPDFGenerator 这样的东西,它将为任意数量的实体字段创建 PDF 并在没有 getter 的情况下填充它们。

为多个实体生成 PDF 的最佳方式是什么?

做这样的事情....列数取决于列表中有多少个字符串。输入是由许多字符串列表组成的列表。外部列表包含每一行,内部列表包含行中的每个值

public static void main( String[] args ) throws IOException {
    List<List<String>> rows = new ArrayList<>();

    List<String> headerRow = Arrays.asList( "ID", "First Name", "Last Name" );
    List<String> firstRow = Arrays.asList( "1", "Jon", "Snow" );
    List<String> secondRow = Arrays.asList( "2", "Mr", "Person" );

    rows.add( headerRow );
    rows.add( firstRow );
    rows.add( secondRow );

    File file = new File( "pathhhh" );
    file.getParentFile().mkdirs();

    ByteArrayInputStream bais = new ByteArrayInputStream( customerPDFReport( rows ) );
    StreamUtils.copy( bais, new FileOutputStream( file ) );
}

public static byte[] customerPDFReport( List<List<String>> rows ) {
    Document document = new Document();
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {

        PdfWriter.getInstance( document, out );
        document.open();

        // Add Text to PDF file ->
        Font font = FontFactory.getFont( FontFactory.COURIER, 14, BaseColor.BLACK );
        Paragraph para = new Paragraph( "Customer Table", font );
        para.setAlignment( Element.ALIGN_CENTER );
        document.add( para );
        document.add( Chunk.NEWLINE );

        PdfPTable table = new PdfPTable( rows.get( 0 ).size() );
        List<String> headerRow = rows.remove( 0 ); // remove header

        for ( String value : headerRow ) {

            PdfPCell header = new PdfPCell();
            Font headFont = FontFactory.getFont( FontFactory.HELVETICA_BOLD );
            header.setBackgroundColor( BaseColor.LIGHT_GRAY );
            header.setHorizontalAlignment( Element.ALIGN_CENTER );
            header.setBorderWidth( 2 );
            header.setPhrase( new Phrase( value, headFont ) );
            table.addCell( header );

        }

        for ( List<String> wholeRow : rows ) {
            for ( String value : wholeRow ) {
                PdfPCell idCell = new PdfPCell( new Phrase( value ) );
                idCell.setPaddingLeft( 4 );
                idCell.setVerticalAlignment( Element.ALIGN_MIDDLE );
                idCell.setHorizontalAlignment( Element.ALIGN_CENTER );
                table.addCell( idCell );
            }
        }

        document.add( table );

        document.close();
    } catch ( DocumentException e ) {

    }

    return out.toByteArray();
}