优化Pdf
parent
116ff08b5b
commit
d251ca2f62
|
|
@ -1,8 +1,12 @@
|
||||||
import { message, Modal, Spin } from "antd";
|
import { useFullscreen } from "ahooks";
|
||||||
import { useState } from "react";
|
import { Button, message, Modal, Spin } from "antd";
|
||||||
|
import { useRef, useState } from "react";
|
||||||
import { Document, Page, pdfjs } from "react-pdf";
|
import { Document, Page, pdfjs } from "react-pdf";
|
||||||
import useDownloadFile from "../../hooks/useDownloadFile";
|
import useDownloadFile from "../../hooks/useDownloadFile";
|
||||||
import { getFileUrl } from "../../utils/index";
|
import { getFileUrl } from "../../utils";
|
||||||
|
import "react-pdf/dist/Page/AnnotationLayer.css";
|
||||||
|
import "react-pdf/dist/Page/TextLayer.css";
|
||||||
|
import "./index.less";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PDF查看组件
|
* PDF查看组件
|
||||||
|
|
@ -22,6 +26,11 @@ function Pdf(props) {
|
||||||
const [pdfWidth, setPdfWidth] = useState(600);
|
const [pdfWidth, setPdfWidth] = useState(600);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
const fullscreenRef = useRef(null);
|
||||||
|
|
||||||
|
const [isFullscreen, { enterFullscreen, exitFullscreen }] = useFullscreen(fullscreenRef);
|
||||||
|
const { downloadFile } = useDownloadFile();
|
||||||
|
|
||||||
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
|
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
|
||||||
|
|
||||||
const onDocumentLoadSuccess = ({ numPages }) => {
|
const onDocumentLoadSuccess = ({ numPages }) => {
|
||||||
|
|
@ -48,14 +57,14 @@ function Pdf(props) {
|
||||||
<Spin size="large" />
|
<Spin size="large" />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div style={{ height: "88vh", overflowY: "auto", padding: "24px", ...style }}>
|
<div style={{ height: isFullscreen ? "calc(100vh - 40px - 24px - 8px - 32px - 12px)" : "72vh", overflowY: "auto", padding: "24px", ...style }}>
|
||||||
<Document
|
<Document
|
||||||
file={!file.includes(fileUrl) ? fileUrl + file : file}
|
file={!file.includes(fileUrl) ? fileUrl + file : file}
|
||||||
onLoadSuccess={onDocumentLoadSuccess}
|
onLoadSuccess={onDocumentLoadSuccess}
|
||||||
onLoadError={onDocumentLoadError}
|
onLoadError={onDocumentLoadError}
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
Array.from({ length: numPages }).map((el, index) => (
|
Array.from({ length: numPages }).map((_, index) => (
|
||||||
<Page key={`page_${index + 1}`} pageNumber={index + 1} onLoadSuccess={onPageLoadSuccess} />
|
<Page key={`page_${index + 1}`} pageNumber={index + 1} onLoadSuccess={onPageLoadSuccess} />
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
@ -69,36 +78,52 @@ function Pdf(props) {
|
||||||
return renderPdfContent();
|
return renderPdfContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { loading: downloadFileLoading, downloadFile } = useDownloadFile();
|
|
||||||
|
|
||||||
const onDownloadFile = () => {
|
const onDownloadFile = () => {
|
||||||
Modal.confirm({
|
isFullscreen && exitFullscreen();
|
||||||
title: "提示",
|
|
||||||
content: "确定要下载此文件吗?",
|
|
||||||
onOk: () => {
|
|
||||||
downloadFile({
|
downloadFile({
|
||||||
url: fileUrl,
|
url: file,
|
||||||
name,
|
name,
|
||||||
});
|
});
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 默认弹窗模式
|
// 默认弹窗模式
|
||||||
return (
|
return (
|
||||||
|
<div ref={fullscreenRef}>
|
||||||
<Modal
|
<Modal
|
||||||
|
style={{ top: isFullscreen ? 0 : 100, maxWidth: isFullscreen ? "100vw" : "calc(100vw - 32px)", paddingBottom: isFullscreen ? 0 : 24 }}
|
||||||
open={visible}
|
open={visible}
|
||||||
maskClosable={false}
|
maskClosable={false}
|
||||||
width={pdfWidth + 100}
|
width={isFullscreen ? "100vw" : pdfWidth + 100}
|
||||||
title="PDF预览"
|
title="PDF预览"
|
||||||
onCancel={onCancel}
|
onCancel={() => {
|
||||||
cancelText="关闭"
|
isFullscreen && exitFullscreen();
|
||||||
okText="下载"
|
onCancel();
|
||||||
onOk={onDownloadFile}
|
}}
|
||||||
loading={downloadFileLoading}
|
getContainer={false}
|
||||||
|
footer={[
|
||||||
|
<Button
|
||||||
|
key="cancel"
|
||||||
|
onClick={() => {
|
||||||
|
isFullscreen && exitFullscreen();
|
||||||
|
onCancel();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
关闭
|
||||||
|
</Button>,
|
||||||
|
<Button
|
||||||
|
key="fullScreen"
|
||||||
|
onClick={() => {
|
||||||
|
isFullscreen ? exitFullscreen() : enterFullscreen();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isFullscreen ? "退出全屏" : "全屏"}
|
||||||
|
</Button>,
|
||||||
|
<Button key="download" type="primary" onClick={onDownloadFile}>下载</Button>,
|
||||||
|
]}
|
||||||
>
|
>
|
||||||
{renderPdfContent()}
|
{renderPdfContent()}
|
||||||
</Modal>
|
</Modal>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
.react-pdf__Page__canvas{
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue