<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.kingdee.shr.custom.sendfile.GetElectronFile"%>
<%@page import="org.springframework.http.ResponseEntity"%>
<%@ page import="org.springframework.core.io.Resource" %>
<%@ page import="java.util.Base64" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.IOException" %>

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <script src="./pdf.min.js"></script>
    <script src="./pdf.worker.min.js"></script>
    <title>证明文件预览</title>
    <style>
        #downloadBtn {
            position: absolute;
            top: 10px;
            right: 10px;
            z-index: 1000;
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }
        #pdfViewer {
            width: 100%;
            height: 100vh;
            overflow: auto;
        }
    </style>
</head>
<body>
<button id="downloadBtn">下载PDF</button>
<div id="pdfViewer"></div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        viewPdf();

        // 下载PDF按钮
        document.getElementById('downloadBtn').onclick = function() {
            window.location.href = 'downElecPdf.jsp?sole=<%= request.getParameter("sole") %>&fname=<%= request.getParameter("fname") %>';
        };

    });

    function viewPdf() {
        <%
            String sole1 = request.getParameter("sole");
            String fname1 = request.getParameter("fname");
            GetElectronFile abd = new GetElectronFile();
            ResponseEntity<Resource> resultData = abd.viewPdf(sole1, fname1);
            Resource body = resultData.getBody();

            String base64EncodedString = null;
            try (InputStream inputStream = body.getInputStream()) {
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                byte[] data = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
                    buffer.write(data, 0, bytesRead); // 将读取到的字节写入缓冲区
                }
                buffer.flush(); // 确保所有数据都被写入
                // 将缓冲区转换为字节数组并进行Base64编码
                base64EncodedString = Base64.getEncoder().encodeToString(buffer.toByteArray());
            } catch (IOException e) {
                e.printStackTrace(); // 处理异常，适当记录错误
            }


        %>
        var response2 = "<%= base64EncodedString %>";
        // 现在你可以使用response2作为Base64编码的PDF文件数据
        var byteCharacters = atob(response2);
        var byteNumbers = new Array(byteCharacters.length);
        for (var i = 0; i < byteCharacters.length; i++) {
            byteNumbers[i] = byteCharacters.charCodeAt(i);
        }
        var byteArray = new Uint8Array(byteNumbers);
        var blob = new Blob([byteArray], { type: 'application/pdf' });
        var loadingTask = pdfjsLib.getDocument(URL.createObjectURL(blob));
        loadingTask.promise.then(function(pdf) {
            var pdfViewer = document.getElementById('pdfViewer');
            pdfViewer.innerHTML = ''; // 清空之前的PDF内容
            for (var pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) {
                pdf.getPage(pageNumber).then(function(page) {
                    var scale = 1.5;
                    var viewport = page.getViewport({ scale: scale });
                    var canvas = document.createElement('canvas');
                    var context = canvas.getContext('2d');
                    canvas.height = viewport.height;
                    canvas.width = viewport.width;

                    pdfViewer.appendChild(canvas);
                    var renderContext = {
                        canvasContext: context,
                        viewport: viewport
                    };
                    page.render(renderContext);
                });
            }
        }, function (reason) {
            console.error(reason);
        });
    }
</script>
</body>
</html>