# print-html **Repository Path**: carpONE_admin/print-html ## Basic Information - **Project Name**: print-html - **Description**: 用于后端处理模板返回给前端进行js 打印 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-08-10 - **Last Updated**: 2023-03-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Getting Started >> [项目地址](https://gitee.com/carpONE/print-html.git) > 如果该项目出现问题可能有些配置没有写上,自行百度 ## 一、新建SpringBoot项目 * 在线创建 [官网](https://start.spring.io/) * 选择Maven * 选择java * 选择2.7.2版本(这里不建议选择高版本) * java版本选择8 * Packaging选择Jar * 然后默认即可 * 选择依赖(ADD DEPENDENCIES... 或者 CTRL + B) * Spring Web * Apache Freemarker(主要) * 点击GENERATE 或者 CTRL + ⏎ ## 二、创建目录 ### 1、在 demo/src/main/resources 目录下创建放ftl模板的目录(webFtl) ### 2、再创建ftl模板文件 >> 如:examples.ftl > ftl语法可自行百度 ### 3、在 demo/src/main/java/com/example/demo 新建目录 名称可随意 >> 如:printHtml 目录 ### 4、在 printHtml 目录下分别创建 web、entity、service >> web 目录主要是用于前端请求管理 (创建 PrintHtmlApiController.java) > entity 目录主要用于数据实体类 (创建 PrintEntity.java、TreeChildren.java、TreeVo.java) > service 目录主要用于服务类(创建 PrintHtmlService.java) > base 主要用于继承类 (创建 BasePrintWriter.java、IBasePrintWriter.java) ## 三、开发后端 ### 1、实体类 > PrintEntity.java (用于树形控件数据) ```java public class PrintEntity { private String title; private String key ="0"; private String classWriteKey; private String templatePath; private boolean disabled =false; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getClassWriteKey() { return classWriteKey; } public void setClassWriteKey(String classWriteKey) { this.classWriteKey = classWriteKey; } public String getTemplatePath() { return templatePath; } public void setTemplatePath(String templatePath) { this.templatePath = templatePath; } public boolean isDisabled() { return disabled; } public void setDisabled(boolean disabled) { this.disabled = disabled; } } ``` > TreeChildren.java ```java public class TreeChildren extends PrintEntity { } ``` > TreeVo.java ```java public class TreeVo extends PrintEntity{ private List children; public List getChildren() { return children; } public void setChildren(List children) { this.children = children; } } ``` ### 2、base 类 > IBasePrintWriter.java ```java public interface IBasePrintWriter { HashMap generate(String referId, String TemplatePath); } ``` > BasePrintWriter.java ```java import org.springframework.stereotype.Component; @Component public abstract class BasePrintWriter implements IBasePrintWriter{ } ``` ### 3、服务类也是输出数据类 > PrintHtmlService.java > ```java import cn.hutool.core.date.DateUtil; import com.example.demo.printHtml.TemplateUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class PrintHtml extends BasePrintWriter { @Override public HashMap generate(String referId, String TemplatePath) { HashMap hashMap =new HashMap(); TreeVo treeVo =new TreeVo(); Map params = new HashMap<>(); params.put("xmmc", "我的项目"); params.put("xmbh", "SpringBoot"); params.put("hasTwoLine",true); params.put("pbdd", "协商地点:"); params.put("pbsj", "协商时间:"); params.put("supplierList","supplierList"); params.put("xmzjList","xmzjListResult"); params.put("zgfhResultList","zgfhResultList1"); params.put("date",DateUtil.format(DateUtil.date(), "YYYY年MM月dd日")); params.put("i",new List[4]); String content = TemplateUtils.generateTemplate(TemplatePath, params); hashMap.put("content",content); treeVo.setTitle("初审结果汇总表"); treeVo.setTemplatePath("webFtl/templates.ftl"); treeVo.setClassWriteKey("printTest"); //子 TreeChildren treeChildren =new TreeChildren(); treeChildren.setTitle("子数据"); treeChildren.setClassWriteKey("printTest"); treeChildren.setTemplatePath("webFtl/templates.ftl"); treeChildren.setKey("1"); List treeChildrenList=new ArrayList<>(); treeChildrenList.add(treeChildren); treeVo.setChildren(treeChildrenList); List printEntityList =new ArrayList<>(); printEntityList.add(treeVo); hashMap.put("printData",printEntityList); return hashMap; } } ``` ### 最后、控制类 > PrintHtmlApiController.java ```java import com.bid.printHtml.base.IBasePrintWriter; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.HashMap; @RestController @RequestMapping({"/api/1/printHtml"}) public class PrintHtmlApiController{ /** * * @param referId 从数据库中需要查找数据所用到的字段 * @param classWriteKey 为s ervice 目录下的 全限定类 * @param templatePath 放ftl模板的路径 一般是 上一级目录+ftl文件 如:webFtl/examples.ftl * @return * @throws ClassNotFoundException */ @GetMapping("/attachment") public HashMap getAttachment(String referId, String classWriteKey, String templatePath) throws ClassNotFoundException { if (StringUtils.isBlank(clerkTemplate)) { throw new ActionException("文书配置的全限定类不允许为空"); } Class cls = Class.forName(classPath); IBasePrintWriter iw = (IBasePrintWriter) applicationContext.getBean(cls); if (iw == null) { throw new ActionException("spring找不到对应的类"); } HashMap htmlHasMap = iw.generate(referId, templatePath); if (StringUtils.isBlank((String) htmlHasMap.get("content"))) { return FxResponse.failure("找不到对应ftl模板"); } return htmlHasMap;//返回的数据可自行封装 } } ``` ## 四、前端开发(略) 文件放在 demo/src/main/resources 目录下的 printHtml > 主要用到的 printJS 依赖进行打印 > [官网](https://printjs.crabbly.com/)