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了

相关推荐
csdn2015_1 分钟前
IDEA配置Continue
java·ide·intellij-idea
2601_9491942620 分钟前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
c***892038 分钟前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
aq55356001 小时前
C语言、C++和C#:三大编程语言核心差异详解
java·开发语言·jvm
老马95271 小时前
opencode4-在已有项目中增加修改功能
java·后端·ai编程
Moe4881 小时前
Spring AI:结构化输出
java·后端·面试
并不喜欢吃鱼1 小时前
从零开始C++----七.继承及相关模型和底层(上篇)
开发语言·c++
沐知全栈开发1 小时前
XML CDATA
开发语言
APIshop1 小时前
Python 爬虫获取闲鱼商品详情 API 接口实战指南
开发语言·爬虫·python
代码羊羊2 小时前
rust-字符串(切片)、元组、结构体、枚举、数组
开发语言·后端·rust