java生成离职证明,各种申请模板并上传pdf

java生成离职证明,各种申请模板并上传pdf
1.利用Thymeleaf,生成证明模板
javascript 复制代码
    public static void main(String[] args) {
        Info info = new Info();//模板对应的属性
        String tempHtml = this.generateCertificateHTML(info);
        ByteArrayInputStream byteArrayInputStream = PdfUtil.exportPdf(tempHtml);
        String fileName ="文件名称";
        String directory = "文件夹地址";
        Boolean isFile = FavFTPUtil.sftpUploadStream(byteArrayInputStream, directory, fileName,emailConfig);
        if (isFile) {
            String sftpPath = "sftp服务文件地址";
            sftpPathList.add(sftpPath);
        }
    }
2.generateCertificateHTML 生成html模板
java 复制代码
private String generateCertificateHTML(FlightChangeProofMsgInfo info) {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);

        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

        Context context = new Context();
        context.setVariable("Name", info.getName());
        context.setVariable("idNo", info.getIdNo());
        String filledTemplate = templateEngine.process("templates/test"//模板地址, context);
        return filledTemplate;
    }

3.将html模板,转为数据流

java 复制代码
    public static String getCurrentOperatingSystem(){
        String os = System.getProperty("os.name").toLowerCase();
        System.out.println("---------当前操作系统是-----------" + os);
        return os;
    }


    public static ByteArrayInputStream exportPdf(String template) throws Exception {

        ByteArrayInputStream byteArrayInputStream = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ITextRenderer renderer = new ITextRenderer();
            ITextFontResolver fontResolver = renderer.getFontResolver();
            if("linux".equals(getCurrentOperatingSystem())|| "mac os x".equals(getCurrentOperatingSystem())){
                fontResolver.addFont(PDF_FONT_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            }else{
                fontResolver.addFont("c:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            }
            renderer.setDocumentFromString(template);
            renderer.layout();
            renderer.createPDF(byteArrayOutputStream, false);
            renderer.finishPDF();
            byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        } catch (DocumentException e) {
            log.info(e.getMessage(), e);
        }

        return byteArrayInputStream;
    }

4.数据流上传到sftp服务器上就ok了

相关推荐
GodKeyNet4 分钟前
设计模式-桥接模式
java·设计模式·桥接模式
future14121 小时前
C#学习日记
开发语言·学习·c#
guojl1 小时前
Java多任务编排技术
java
丶意冷1 小时前
mybatisPlus分页方言设置错误问题 mybatisPlus对于Oceanbase的Oracle租户分页识别错误
java·数据库·oracle·oceanbase
king_harry1 小时前
Java程序-OceanBase Connector/J 示例
开发语言
要开心吖ZSH1 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring
桦说编程2 小时前
深入解析CompletableFuture源码实现
java·性能优化·源码
傻啦嘿哟2 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
翻滚吧键盘2 小时前
js代码09
开发语言·javascript·ecmascript
q567315232 小时前
R语言初学者爬虫简单模板
开发语言·爬虫·r语言·iphone