Freemarker+thymeleaf应用实现打印银行小票

背景:最近项目里有个需求,需要动态配置一个模板,来打印各种不同银行或者其他行业的小票,下面小小记录一下实现过程。

关键词:Springboot, thymeleaf, Freemarker,html2image

一,引入依赖


	    org.springframework.boot
	spring-boot-starter-thymeleaf



	org.xhtmlrenderer
	core-renderer
	R8


	com.github.xuwei-k
	html2image
	0.1.0



	org.springframework.boot
	spring-boot-starter-freemarker

二,准备数据

1,动态配置的字段
Freemarker+thymeleaf应用实现打印银行小票_第1张图片

2,包含所有配置信息的字段Dto
 

@Data
public class TemplateFieldDto {
    /**
     * 模板id
     */
    private long templateId;
    /**
     * 字段id
     */
    private long fieldId;
    /**
     * bankId
     */
    private long bankId;
    /**
     * 商户ID
     */
    private long merchantId;
    /**
     * 发票联类型, MERCHANT,CARDHOLDER,SETTLEMENT
     */
    private String ftlType;
    /**
     * 字段编码
     */
    private String fieldCode;
    /**
     * 字段名,多语言
     */
    private String fieldName;
    /**
     * 字段值
     */
    private String fieldValue;
    /**
     * 语言
     */
    private String languageType;
    /**
     * 顺序
     */
    private Integer fieldOrder;
    /**
     * 字体颜色,#333333
     */
    private String fontColor;
    /**
     * 标题字体大小,单位px
     */
    private String titleFontSize;
    /**
     * 字体大小,单位px
     */
    private String fontSize;
    /**
     * 是否粗体,bold
     */
    private String fontStyle;
    /**
     * 是否展示字段名标题
     */
    private boolean showTitle;
    /**
     * 是否换行, 0 否,1 是
     */
    private boolean newLine;
    /**
     * 是否合并到title
     */
    private boolean mergeToTitle;
    /**
     * 对齐方式,left,right,center
     */
    private String position;
}

3,组装成需要的数据结构

Freemarker+thymeleaf应用实现打印银行小票_第2张图片

三,配置动态模板 

这里是关键,需要遍历数据,并判断数据中属性




    
    
    FreeMarker



    
        <#if data??>
            <#list data as item>
                <#if item.showTitle>
                    <#if item.newLine>
                        
                    <#else>
                        <#if item.mergeToTitle>
                            
                        <#else>
                            
                <#else>
                    <#if item.fieldCode=='amountTitleLine'>
                        
                    <#elseif item.fieldCode=='creditCard'>
                        
                    <#elseif item.fieldCode=='debitCard'>
                        
                    <#elseif item.fieldCode=='allCards'>
                        
                    <#elseif item.fieldCode=='totalAmoutEnd'>
                        
                    <#else>
                        <#if item.fieldCode=='bankLogo'>
                            
                        <#elseif item.fieldCode='emptyLine'>
                            
                        <#elseif item.fieldCode='grayLine'>
                            
                        <#else>
                            
        <#else>
        
${item.fieldName}:
${item.fieldValue}
${item.fieldName}: ${item.fieldValue}
${item.fieldName}: ${item.fieldValue}
TYPE SUM AMOUNT
${item.fieldName}
SALE ${creditCard.creditSaleCount} ${creditCard.creditSaleAmount}
VOID ${creditCard.creditVoidCount} ${creditCard.creditVoidAmount}
${item.fieldName}
SALE ${debitCard.debitSaleCount} ${debitCard.debitSaleAmount}
REFUND ${debitCard.debitRefundCount} ${debitCard.debitRefundAmount}
CASHBACK ${debitCard.debitCashbackCount} ${debitCard.debitCashbackAmount}
${item.fieldName}
SALE ${allCards.totalSaleCount} ${allCards.totalSaleAmount}
REFUND ${allCards.totalRefundCount} ${allCards.totalRefundAmount}
${item.fieldName} ${totalAmoutEnd.totalCount} ${totalAmoutEnd.totalAmount}
BankLogo

${item.fieldValue}

四,结果展示

Freemarker+thymeleaf应用实现打印银行小票_第3张图片

码字不易,记得点赞关注哟

你可能感兴趣的:(微服务&Spring,spring,boot,spring,java,freemarker,thymeleaf)