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了

相关推荐
jinanwuhuaguo8 分钟前
(第三十三篇)五月的文明奠基:OpenClaw 2026.5.2版本的文明级解读
android·java·开发语言·人工智能·github·拓扑学·openclaw
xmjd msup41 分钟前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
952361 小时前
SpringBoot统一功能处理
java·spring boot·后端
有一个好名字1 小时前
工具即双手 —— 从 Bash 到 Tool Dispatch Map
开发语言·chrome·bash
Lyyaoo.1 小时前
优惠券秒杀业务分析
java·开发语言
消失的旧时光-19431 小时前
统一并发模型:线程、Reactor、协程本质是一件事(从线程到协程 · 第6篇·终章)
java·python·算法
勿忘初心12211 小时前
Java 国密 SM4 加密工具类实战(Hutool + BouncyCastle)|企业级数据加密 + 兼容 JDK8
java·数据安全·数据加密·后端开发·企业级开发·国密 sm4
庞轩px1 小时前
第8篇:原子类与CAS底层原理——无锁并发的实现
java·cas·乐观锁·aba·无锁编程·自旋
rleS IONS1 小时前
SpringBoot中自定义Starter
java·spring boot·后端
DevilSeagull2 小时前
MySQL(2) 客户端工具和建库
开发语言·数据库·后端·mysql·服务