package com.donglan.ahxf.ahxfmeetregisterwindow.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.deepoove.poi.XWPFTemplate;
import com.donglan.ahxf.ahxfmeetregisterwindow.common.Result;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shj
 * @date 2021/3/22 1:23 下午
 */
@Controller
@RequestMapping("/notify")
public class NotifyExportController {
    @RequestMapping(value = "/pdf",method = RequestMethod.POST)
    @CrossOrigin
    public void getNotifyPdf(@RequestBody HashMap map, HttpServletResponse response) throws IOException {
        InputStream is = ResourceUtil.getResourceObj("static/doc/notify.docx").getStream();
        XWPFTemplate template = XWPFTemplate.compile(is).render(map);
        is.close();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        template.writeAndClose(bos);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        bos.close();
        Document document = new Document();
        document.loadFromStream(bis, FileFormat.Docx);
        bis.close();
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition","inline;filename="+ URLEncoder.encode("告知单","utf-8") +".pdf");
        document.saveToFile(os,FileFormat.PDF);
        os.close();
    }
}
