XDocReport / FreeMarker(FTL):在模板中的模板中插入值

时间:2019-07-03 13:02:42

标签: java spring-boot freemarker docx4j xdocreport

我需要使用XDocReport从docx协议文件生成PDF。

在docx文件中,我有几个模板,这些模板是通过我的API将收到的请求填写的 我的用例是,我需要根据我将收到的请求类型生成的pdf文件中插入以下两个文本之一

1。)(如果有)独资/合伙制,在$ {address}设有办事处,由先生/女士代表。 [$ {authoriged_signatory_partner},为授权签署人 2。)(或如果有的话)根据[1956/2013年《公司法》的规定成立并注册的公共/私人有限公司,其注册办事处为$ {address},由先生/女士代表。 $ {authoriged_signatory_partner},授权签署者(“ API合作伙伴”)。

我想到了$ {partner_or_company_content}模板标记,根据收到的请求是合伙企业还是公司,我将在其中插入两个选择之一(该请求将包含一个键值对,例如: “ isPartner”:“是”)

如何在$ {authoriged_signatory_partner}和$ {address}中插入值,然后它将成为模板中的模板?

这是将数据填充到Map中的方法,我从其中填充模板

public void fillData(Map<String, Object> nonImageVariableMap, FileConvertRequest request) {
    // TODO Auto-generated method stub
    Parameters parameter = request.getParameters();

    if (parameter.getName() != null && parameter.getName().trim().length() > 0) {
        nonImageVariableMap.put("name", parameter.getName());
    }

    if (parameter.getCellnumber() != null && parameter.getCellnumber().trim().length() > 0) {
        nonImageVariableMap.put("cell_number", parameter.getCellnumber());
    }

    if (parameter.getAadhaarNumber() != null && parameter.getAadhaarNumber().trim().length() > 0) {
        nonImageVariableMap.put("aadhaar_number", parameter.getAadhaarNumber());
    }
    if (parameter.getAddress() != null && parameter.getAddress().trim().length() > 0) {
        nonImageVariableMap.put("address", parameter.getAddress());
    }
    if (parameter.getAuthorigedSignatoryPartner() != null
            && parameter.getAuthorigedSignatoryPartner().trim().length() > 0) {
        nonImageVariableMap.put("authoriged_signatory_partner", parameter.getAuthorigedSignatoryPartner());
    }
    if (parameter.getCellnumber() != null && parameter.getCellnumber().trim().length() > 0) {
        nonImageVariableMap.put("name", parameter.getName());
    }
    if (parameter.getEffectiveDate() != null && parameter.getEffectiveDate().trim().length() > 0) {
        nonImageVariableMap.put("effective_date", parameter.getEffectiveDate());
    }
    if (parameter.getEmail() != null && parameter.getEmail().trim().length() > 0) {
        nonImageVariableMap.put("email", parameter.getEmail());
    }
    if (parameter.getMerchantCode() != null && parameter.getMerchantCode().trim().length() > 0) {
        nonImageVariableMap.put("merchant_code", parameter.getMerchantCode());
    }
    if (parameter.getPanNumber() != null && parameter.getPanNumber().trim().length() > 0) {
        nonImageVariableMap.put("pan_number", parameter.getPanNumber());
    }
    if (parameter.getPlace() != null && parameter.getPlace().trim().length() > 0) {
        nonImageVariableMap.put("place", parameter.getPlace());
    }
    if (parameter.getMerchantName() != null && parameter.getMerchantName().trim().length() > 0) {
        nonImageVariableMap.put("merchant_name", parameter.getMerchantName());
    }
    if(parameter.getSignatory() !=null && parameter.getSignatory().trim().length() > 0) {
        nonImageVariableMap.put("signatory", parameter.getSignatory());
    }
    if(parameter.getOrignalAgreementSignedDate() !=null && parameter.getOrignalAgreementSignedDate().trim().length() > 0) {
        nonImageVariableMap.put("orignal_agreement_signed_date", parameter.getOrignalAgreementSignedDate());
    }

}

这是请求类

package com.eko.dconverter.requestobj;

import javax.xml.bind.annotation.XmlRootElement;

import com.fasterxml.jackson.annotation.JsonProperty;

@XmlRootElement
public class FileConvertRequest {

private String fileName;
private String fileLocation;
private String outputLocation;
private String convertInto;
private boolean isGoogleDrive;
private String merchantCode;
private Parameters parameters;

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getFileLocation() {
    return fileLocation;
}

public void setFileLocation(String fileLocation) {
    this.fileLocation = fileLocation;
}

public String getOutputLocation() {
    return outputLocation;
}

public void setOutputLocation(String outputLocation) {
    this.outputLocation = outputLocation;
}

public String getConvertInto() {
    return convertInto;
}

public void setConvertInto(String convertInto) {
    this.convertInto = convertInto;
}

public boolean isGoogleDrive() {
    return isGoogleDrive;
}

public void setGoogleDrive(boolean isGoogleDrive) {
    this.isGoogleDrive = isGoogleDrive;
}


public Parameters getParameters() {
    return parameters;
}

public String getMerchantCode() {
    return merchantCode;
}

public void setMerchantCode(String merchantCode) {
    this.merchantCode = merchantCode;
}

@JsonProperty("parameters")
public void setParameters(Parameters parameters) {
    this.parameters = parameters;
}
}

这是参数类

package com.eko.dconverter.requestobj;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Parameters {

private String name;
private String cellnumber;
private String address;
private String panNumber;
private String aadhaarNumber;
private String merchantName;
private String authorigedSignatoryPartner;
private String effectiveDate;
private String merchantCode;
private String place;
private String email;
private String signatory;
private String orignalAgreementSignedDate;

public String getOrignalAgreementSignedDate() {
    return orignalAgreementSignedDate;
}

public void setOrignalAgreementSignedDate(String orignalAgreementSignedDate) {
    this.orignalAgreementSignedDate = orignalAgreementSignedDate;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCellnumber() {
    return cellnumber;
}

public void setCellnumber(String cellnumber) {
    this.cellnumber = cellnumber;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getPanNumber() {
    return panNumber;
}

public void setPanNumber(String panNumber) {
    this.panNumber = panNumber;
}

public String getAadhaarNumber() {
    return aadhaarNumber;
}

public void setAadhaarNumber(String aadhaarNumber) {
    this.aadhaarNumber = aadhaarNumber;
}

public String getMerchantName() {
    return merchantName;
}

public void setMerchantName(String merchantName) {
    this.merchantName = merchantName;
}

public String getAuthorigedSignatoryPartner() {
    return authorigedSignatoryPartner;
}

public void setAuthorigedSignatoryPartner(String authorigedSignatoryPartner) {
    this.authorigedSignatoryPartner = authorigedSignatoryPartner;
}

public String getEffectiveDate() {
    return effectiveDate;
}

public void setEffectiveDate(String effectiveDate) {
    this.effectiveDate = effectiveDate;
}

public String getMerchantCode() {
    return merchantCode;
}

public void setMerchantCode(String merchantCode) {
    this.merchantCode = merchantCode;
}

public String getPlace() {
    return place;
}

public void setPlace(String place) {
    this.place = place;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getSignatory() {
    return signatory;
}

public void setSignatory(String signatory) {
    this.signatory = signatory;
}

}

此处是生成PDF的地方(不包含可以完成工作的辅助函数,但是可以给出想法)

public String mergeAndGeneratePDFOutput(String templatePath, TemplateEngineKind templateEngineKind, Map<String, Object> nonImageVariableMap,Map<String, String> imageVariablesWithPathMap, String fileToBeGenerated, String outputFileLocation ) throws IOException, XDocReportException, Docx4JException{

    System.out.println("File path :"+templatePath);
    InputStream inputStream = loadDocumentAsStream(templatePath);
    IXDocReport xdocReport = loadDocumentAsIDocxReport(inputStream,templateEngineKind);
    IContext context = replaceVariabalesInTemplateOtherThanImages(xdocReport,nonImageVariableMap);
    replaceImagesVariabalesInTemplate(xdocReport, imageVariablesWithPathMap, context);
    byte[] mergedOutput = generateMergedOutput(xdocReport, context);

    WordprocessingMLPackage wordprocessingMLPackage=null;

    FileOutputStream os = new FileOutputStream(outputFileLocation+""+fileToBeGenerated);

    wordprocessingMLPackage = WordprocessingMLPackage.load(new ByteArrayInputStream(mergedOutput));
    Docx4J.toPDF(wordprocessingMLPackage, os);

    return fileToBeGenerated;

}

0 个答案:

没有答案