zjh
2023-12-06 41f99a843fb2c0424426e006f29c1e78192eae01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.ltkj.web.jmreport;
 
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import javax.activation.DataHandler;
import javax.annotation.Resource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ltkj.common.core.domain.entity.SysDept;
import com.ltkj.common.utils.DateUtils;
import com.ltkj.common.utils.SecurityUtils;
import com.ltkj.hosp.domain.*;
import com.ltkj.hosp.service.*;
import com.ltkj.hosp.vodomain.DaoJianVO;
import com.ltkj.system.service.ISysConfigService;
import com.ltkj.system.service.ISysDeptService;
import com.ltkj.web.config.pdfutils.MyHeaderFooter;
import com.ltkj.web.config.pdfutils.PDFBinaryUtil;
import com.ltkj.web.config.pdfutils.PdfUtils;
import com.ltkj.web.controller.email.MailConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ltkj.common.annotation.Log;
import com.ltkj.common.core.controller.BaseController;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.enums.BusinessType;
import com.ltkj.common.utils.poi.ExcelUtil;
import com.ltkj.common.core.page.TableDataInfo;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
 
import static com.ltkj.common.utils.pdfutils.PdfUtils.createCell;
import static com.ltkj.common.utils.pdfutils.PdfUtils.createTable;
import static com.ltkj.framework.datasource.DynamicDataSourceContextHolder.log;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: lige
 * @Date: 2023/3/17 10:24
 */
@RestController
@RequestMapping("/test")
@Api(tags = "打印pdf测试接口111111111111")
public class TestController {
 
    @GetMapping("/exportPdf")
    @ApiOperation(value = "exportPdf")
    public void exportPdf(HttpServletResponse response, HttpServletRequest request){
        RestTemplate restTemplate = new RestTemplate();
        //创建url路径
        String url = "http://192.168.0.100:5011/jmreport/exportPdfStream";
        Map<String,Object> map = new HashMap<>();
        JSONObject object = new JSONObject();
        //此处为示例,需要传递api和sql的正确参数值
        object.put("tjNumber","10001230309183624");
        //queryParam中有个特殊属性 dpi每英寸点数,windows是96,可不用传
        object.put("dpi","96");
        map.put("excelConfigId","792997692059705344");
        map.put("queryParam",object);
        HttpHeaders headers = new HttpHeaders();
        //如果有字典需要传token
        headers.add("token",request.getHeader("Authorization"));
        HttpEntity<Map<String,Object>> httpEntity = new HttpEntity<>(map,headers);
        try {
            String apiResult = restTemplate.postForObject(url, httpEntity, String.class);
            System.out.println(apiResult);
            String encode = new BASE64Encoder().encode(apiResult.getBytes());
            System.out.println("==================================="+encode);
            //JSONObject jsonObject = JSONObject.parseObject(apiResult);
            //Object code = jsonObject.get("code");
            //Object message = jsonObject.get("message");
            //if(null != code && !"".equals(String.valueOf(code)) && "200".equals(String.valueOf(code))){
                //JSONObject result = jsonObject.getJSONObject("result");
                //文件byte64字符串
                //String file = result.getString("file");
                //文件名称
                //String name = result.getString("name");
                //转换成byte
                byte[] buffer = apiResult.getBytes();
                //name = URLEncoder.encode(name, "UTF-8");
                response.setContentType("application/vnd.ms-pdf;charset=GBK");
                response.setHeader("Content-Disposition", "attachment;filename=" + "体检报告");
                response.getOutputStream().write(buffer);
                response.getOutputStream().flush();
           // }else{
                //log.info("调用失败:"+String.valueOf(message));
            //}
        } catch (RestClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}