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了

相关推荐
крон3 分钟前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan37 分钟前
c++ 单例模式
开发语言·c++·单例模式
coderSong256839 分钟前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
老胖闲聊1 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1181 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
Mr_Air_Boy2 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
曹勖之2 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?2 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头2 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
年老体衰按不动键盘2 小时前
快速部署和启动Vue3项目
java·javascript·vue