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了

相关推荐
wowocpp4 分钟前
idea springboot 配置文件 中文显示
java·spring boot·intellij-idea
User_芊芊君子7 分钟前
【Java面试题】——this 和 super 的区别
java·开发语言
柴薪之王、睥睨众生8 分钟前
(自用)Java学习-5.8(总结,springboot)
java·开发语言·spring boot·学习·mybatis
牛马baby8 分钟前
Java高频面试之并发编程-17
java·开发语言·面试
让我们一起加油好吗9 分钟前
【C++】模板(初阶)
开发语言·c++·visualstudio·模板·泛型编程
不辉放弃1 小时前
java连数据库
java·mysql
灏瀚星空1 小时前
基于Python的量化交易实盘部署与风险管理指南
开发语言·python
Run_Teenage1 小时前
C++类和对象:运行符重载、取地址运算符重载、const 修饰的类如何作为参数
开发语言·c++
一只小bit1 小时前
More Effective C++:改善编程与设计(上)
开发语言·c++·方法·技巧
钢铁男儿2 小时前
C# 方法(栈帧)
开发语言·c#