Custom Table - rocketbase-io/email-template-builder GitHub Wiki

You can configure your custom tables since version 2.x. You need to provide a class that implements

configuration

@Getter
public static class CustomTable implements TableLine {

    List<List<Object>> headerRows = new ArrayList<>();
    List<List<Object>> itemRows = new ArrayList<>();
    List<List<Object>> footerRows = new ArrayList<>();
    @Getter(AccessLevel.PRIVATE)
    EmailTemplateBuilder.EmailTemplateConfigBuilder builder;
    private AtomicInteger posCounter = new AtomicInteger(1);

    public CustomTable(EmailTemplateBuilder.EmailTemplateConfigBuilder builder) {
        this.builder = builder;

        headerRows.add(Arrays.asList("Pos", "Description", "Tax", "Amount"));
    }

    @Override
    public EmailTemplateBuilder.EmailTemplateConfigBuilder and() {
        return builder;
    }

    @Override
    public HtmlTextEmail build() {
        return builder.build();
    }

    public CustomTable itemRow(TableCellImage image, TableCellLink description, BigDecimal tax, BigDecimal amount) {
        itemRows.add(Arrays.asList(posCounter.getAndIncrement(), image, description, tax, amount));
        return this;
    }

    public CustomTable footerRow(TableCellHtml label, TableCellHtml amount) {
        footerRows.add(Arrays.asList(label, amount));
        return this;
    }

    @Override
    public List<ColumnConfig> getHeader() {
        return Arrays.asList(new ColumnConfig()
                        .center(),
                new ColumnConfig()
                        .colspan(2)
                        .width("60%"),
                new ColumnConfig()
                        .alignment(Alignment.RIGHT),
                new ColumnConfig()
                        .width("20%")
                        .alignment(Alignment.RIGHT));
    }

    @Override
    public List<ColumnConfig> getItem() {
        return Arrays.asList(new ColumnConfig().center(),
                new ColumnConfig()
                        .width(90),
                new ColumnConfig()
                        .lighter(),
                new ColumnConfig()
                        .numberFormat("# '%'")
                        .italic()
                        .right(),
                new ColumnConfig()
                        .numberFormat("#.## '€'")
                        .right());
    }

    @Override
    public List<ColumnConfig> getFooter() {
        return Arrays.asList(new ColumnConfig()
                        .colspan(4)
                        .alignment(Alignment.RIGHT),
                new ColumnConfig()
                        .alignment(Alignment.RIGHT));
    }
}

usage

EmailTemplateBuilder.EmailTemplateConfigBuilder builder = EmailTemplateBuilder.builder();

TbConfiguration config = TbConfiguration.newInstance();
config.getContent().setWidth(800);

builder
        .configuration(config)
        .header().text("Invoice {{invoice_id}}").and()
        .text("Hi {{name}},").and()
        .text("Thanks for using [Product Name]. This is an invoice for your recent purchase");

CustomTable customTable = new CustomTable(builder);
customTable.itemRow(new TableCellImageSimple("https://cdn.shopify.com/s/files/1/0255/1211/6260/products/TCW1142-07052_small.jpg?v=1589200198").width(80),
        new TableCellLinkSimple("Damen Harbour Tanktop × 1\nQUARTZ PINK / S", "http://localhost/?item=1234"),
        BigDecimal.valueOf(19),
        BigDecimal.valueOf(4995, 2));
customTable.itemRow(new TableCellImageSimple("https://cdn.shopify.com/s/files/1/0255/1211/6260/products/TCM1886-0718_201_fdf0be52-639f-4ea8-9143-6bd75e0821b1_small.jpg?v=1583509609").width(80),
        new TableCellLinkSimple("Herren ten Classic T-Shirt\nFOREST GREEN HEATHER / XL", "http://localhost/?item=4567"),
        BigDecimal.valueOf(16),
        BigDecimal.valueOf(3995, 2));
customTable.itemRow(new TableCellImageSimple("https://cdn.shopify.com/s/files/1/0255/1211/6260/products/TCM1939-0439_1332_da6f3e7c-e18d-4778-be97-c6c0b482b643_small.jpg?v=1583509671").width(80),
        new TableCellLinkSimple("Herren Joshua Hanfshorts\nDARK OCEAN BLUE / XL", "http://localhost/?item=890"),
        BigDecimal.valueOf(19),
        BigDecimal.valueOf(6995, 2));
customTable.footerRow(new TableCellHtmlSimple("<b>Net</b>", "Net"), new TableCellHtmlSimple("<b>159,85 €</b>", "159,85 €"));
customTable.footerRow(new TableCellHtmlSimple("Tax 19%<br>Tax 16%<br>Shipping", "Tax 19%\nTax 16%\nShipping"), new TableCellHtmlSimple("22,78 €<br>6,39 €<br>6,90 €", "22,78 €\n6,39 €\n6,90 €"));
customTable.footerRow(new TableCellHtmlSimple("<b>Total</b>", "Tax\n19%\n16%"), new TableCellHtmlSimple("<b>195,92 €</b>", "195,92 €"));


builder.table(customTable)
        .button("Download PDF", "http://localhost").gray().right().and()
        .text("If you have any questions about this receipt, simply reply to this email or reach out to our support team for help.").and()
        .copyright("rocketbase").url("https://www.rocketbase.io").suffix(". All rights reserved.").and()
        .footerText("[Company Name, LLC]\n" +
                "1234 Street Rd.\n" +
                "Suite 1234");

HtmlTextEmail htmlTextEmail = builder.build();
⚠️ **GitHub.com Fallback** ⚠️