小静妈 發表於 2024-9-14 16:38:00

react-pdf预览在线PDF的使用

<h3>1、在react项目中安装react-pdf依赖包</h3>
<p>建议安装8.0.2版本的react-pdf,如果安装更高版本的可能出现一些浏览器的兼容性问题;</p>
<div class="cnblogs_code">
<pre>npm install react-pdf@8.0.2 -S</pre>
</div>
<p>&nbsp;</p>
<h3>1、PC端的使用</h3>
<h4>1.1、封装一个组件:PdfViewModal.tsx</h4>
<div class="cnblogs_code">
<pre>import React, { useState } from 'react'<span style="color: rgba(0, 0, 0, 1)">
import { Modal, Spin, Alert } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">
import { Document, Page, pdfjs } from </span>'react-pdf'<span style="color: rgba(0, 0, 0, 1)">
import </span>'react-pdf/dist/esm/Page/AnnotationLayer.css'<span style="color: rgba(0, 0, 0, 1)">
import </span>'react-pdf/dist/esm/Page/TextLayer.css'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置 PDF.js 的 worker 文件</span>
pdfjs.GlobalWorkerOptions.workerSrc = <span style="color: rgba(0, 0, 255, 1)">new</span> URL('pdfjs-dist/build/pdf.worker.min.js'<span style="color: rgba(0, 0, 0, 1)">, import.meta.url).toString()

interface PDFPreviewModalProps {
fileName: string </span>| <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
fileUrl: string </span>| <span style="color: rgba(0, 0, 255, 1)">null</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 传入的 PDF 文件地址</span>
onCancel: () =&gt; <span style="color: rgba(0, 0, 255, 1)">void</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 关闭弹框的回调</span>
<span style="color: rgba(0, 0, 0, 1)">}

const PDFPreviewModal: React.FC</span>&lt;PDFPreviewModalProps&gt; = ({ fileName, fileUrl, onCancel }) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
const </span>= useState&lt;number | <span style="color: rgba(0, 0, 255, 1)">null</span>&gt;(<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
const </span>= useState&lt;number&gt;(600) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认宽度为 600px</span>
const = useState&lt;<span style="color: rgba(0, 0, 255, 1)">boolean</span>&gt;(<span style="color: rgba(0, 0, 255, 1)">true</span>) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 控制加载状态</span>
const = useState&lt;<span style="color: rgba(0, 0, 255, 1)">boolean</span>&gt;(<span style="color: rgba(0, 0, 255, 1)">false</span>) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 控制加载错误状态</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 PDF 加载成功时,设置页面数量</span>
const onDocumentLoadSuccess = ({ numPages }: { numPages: number }) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    setNumPages(numPages)
    setLoading(</span><span style="color: rgba(0, 0, 255, 1)">false</span>) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载成功后,隐藏 loading</span>
<span style="color: rgba(0, 0, 0, 1)">}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载失败时,设置错误状态</span>
const onDocumentLoadError = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    setLoading(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
    setError(</span><span style="color: rgba(0, 0, 255, 1)">true</span>) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 出错时显示错误提示</span>
<span style="color: rgba(0, 0, 0, 1)">}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取 PDF 页面加载后的宽度</span>
const onPageLoadSuccess = ({ width }: { width: number }) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    setPdfWidth(width)
}

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
    </span>&lt;<span style="color: rgba(0, 0, 0, 1)">Modal
      title</span>=<span style="color: rgba(0, 0, 0, 1)">{`【${fileName}】预览`}
      open
      onCancel</span>=<span style="color: rgba(0, 0, 0, 1)">{onCancel}
      footer</span>={<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">}
      width</span>={pdfWidth + 100<span style="color: rgba(0, 0, 0, 1)">}
      style</span>={{ top: 20<span style="color: rgba(0, 0, 0, 1)"> }}
    </span>&gt;<span style="color: rgba(0, 0, 0, 1)">
      {error </span>?<span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;Alert message="加载 PDF 文件失败" type="error" showIcon /&gt;
<span style="color: rgba(0, 0, 0, 1)">      ) : (
      </span>&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
          {loading </span>&amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> (
            </span>&lt;div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '80vh' }}&gt;
            &lt;Spin size="large" /&gt;
            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">          )}
          {fileUrl </span>&amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> (
            </span>&lt;&gt;
            &lt;div style={{ height: '88vh', overflowY: 'auto', padding: '24px' }}&gt;
            &lt;<span style="color: rgba(0, 0, 0, 1)">Document
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">file={new URL('/public/temp/DXF文件要求.pdf',import.meta.url).toString()}</span>
                file=<span style="color: rgba(0, 0, 0, 1)">{fileUrl}
                onLoadSuccess</span>=<span style="color: rgba(0, 0, 0, 1)">{onDocumentLoadSuccess}
                onLoadError</span>=<span style="color: rgba(0, 0, 0, 1)">{onDocumentLoadError}
            </span>&gt;<span style="color: rgba(0, 0, 0, 1)">
                {Array.from(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Array(numPages), (el, index) =&gt;<span style="color: rgba(0, 0, 0, 1)"> (
                  </span>&lt;Page key={`page_${index + 1}`} pageNumber={index + 1} onLoadSuccess={onPageLoadSuccess} /&gt;
<span style="color: rgba(0, 0, 0, 1)">                ))}
            </span>&lt;/Document&gt;
            &lt;/div&gt;
            &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">          )}
      </span>&lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">      )}
    </span>&lt;/Modal&gt;
<span style="color: rgba(0, 0, 0, 1)">)
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> PDFPreviewModal</pre>
</div>
<h4>1.2、业务代码中引入该组件</h4>
<div class="cnblogs_code">
<pre>import React, { useState, useEffect, useCallback } from 'react'<span style="color: rgba(0, 0, 0, 1)">
import { Form } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">
import { List } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">
import PDFPreviewModal from </span>'@/components/PdfViewModal.tsx'

const PdfTest = (props: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {</span><span style="color: rgba(0, 0, 0, 1)">
const </span>= useState&lt;any&gt;<span style="color: rgba(0, 0, 0, 1)">()<br><br> const onTestPdf = () =&gt; {<br>  <span style="color: rgba(0, 0, 0, 1)">setPreviewFile</span>({<br>    fileName: 'abc.pdf',<br>    fileUrl: 'http://****/abc.pdf'<br>  })<br> }</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
    </span>&lt;div className="mrUp mrLink"&gt;   <br>   &lt;div onClick={onTestPdf}&gt;测试预览PDF&lt;/div&gt;<br>
      {!!previewFile?.publicFileUrl &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;<span style="color: rgba(0, 0, 0, 1)">PDFPreviewModal
          fileName</span>={previewFile?<span style="color: rgba(0, 0, 0, 1)">.fileName}
          fileUrl</span>={previewFile?<span style="color: rgba(0, 0, 0, 1)">.publicFileUrl}
          onCancel</span>={() =&gt; setPreviewFile(''<span style="color: rgba(0, 0, 0, 1)">)}
      </span>/&gt;
<span style="color: rgba(0, 0, 0, 1)">      )}
    </span>&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">)
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> PdfTest</pre>
</div>
<h3>2、H5移动端的使用</h3>
<p>移动端加入放大、缩小、上一页、下一页的功能;</p>
<h4>2.1、封装一个组件:PDFViwer.tsx</h4>
<div class="cnblogs_code">
<pre>import React, { useState } from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import { Button, Modal, Space, Toast, Divider } from </span>'antd-mobile'<span style="color: rgba(0, 0, 0, 1)">
import { UpOutline, DownOutline, AddCircleOutline, MinusCircleOutline } from </span>'antd-mobile-icons'<span style="color: rgba(0, 0, 0, 1)">
import { Document, Page, pdfjs } from </span>'react-pdf'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'react-pdf/dist/esm/Page/AnnotationLayer.css'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 样式导入</span>
import 'react-pdf/dist/esm/Page/TextLayer.css'

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置 PDF.js 的 worker 文件</span>
pdfjs.GlobalWorkerOptions.workerSrc = <span style="color: rgba(0, 0, 255, 1)">new</span> URL('pdfjs-dist/build/pdf.worker.min.js'<span style="color: rgba(0, 0, 0, 1)">, import.meta.url).toString()

interface PDFPreviewModalProps {
fileUrl: string </span>| <span style="color: rgba(0, 0, 255, 1)">null</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 传入的 PDF 文件地址</span>
<span style="color: rgba(0, 0, 0, 1)">}

const styleBtnDv </span>=<span style="color: rgba(0, 0, 0, 1)"> {
display: </span>'flex'<span style="color: rgba(0, 0, 0, 1)">,
justifyContent: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
height: </span>'1rem'<span style="color: rgba(0, 0, 0, 1)">,
alignItems: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
gap: </span>'0.4rem'<span style="color: rgba(0, 0, 0, 1)">,
margin: </span>'0.3rem 1rem'<span style="color: rgba(0, 0, 0, 1)">,
padding: </span>'0 0.6rem'<span style="color: rgba(0, 0, 0, 1)">,
background: </span>'#444'<span style="color: rgba(0, 0, 0, 1)">,
borderRadius: </span>'0.5rem'<span style="color: rgba(0, 0, 0, 1)">
}

const styleBtn </span>=<span style="color: rgba(0, 0, 0, 1)"> {
flex: </span>1<span style="color: rgba(0, 0, 0, 1)">,
display: </span>'flex'<span style="color: rgba(0, 0, 0, 1)">,
justifyContent: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
height: </span>'0.6rem'<span style="color: rgba(0, 0, 0, 1)">,
alignItems: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> PDF预览功能</span>
const PDFViwer: React.FC&lt;PDFPreviewModalProps&gt; = ({ fileUrl }) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
const </span>= useState(1<span style="color: rgba(0, 0, 0, 1)">);
const </span>= useState(1<span style="color: rgba(0, 0, 0, 1)">);
const </span>= useState(0.65<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 PDF 加载成功时,设置页面数量</span>
const onDocumentLoadSuccess = ({ numPages }: { numPages: number }) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    setNumPages(numPages);
};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">上一页</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> lastPage() {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (pageNumber == 1<span style="color: rgba(0, 0, 0, 1)">) {
      Toast.show({
      content: </span>'已是第一页'<span style="color: rgba(0, 0, 0, 1)">
      })
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    const page </span>= pageNumber - 1<span style="color: rgba(0, 0, 0, 1)">;
    setPageNumber(page);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">下一页</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> nextPage() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (pageNumber ==<span style="color: rgba(0, 0, 0, 1)"> numPages) {
      Toast.show(</span>"已是最后一页"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    const page </span>= pageNumber + 1<span style="color: rgba(0, 0, 0, 1)">;
    setPageNumber(page);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">缩小</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> pageZoomOut() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (scale &lt;= 0.3<span style="color: rgba(0, 0, 0, 1)">) {
      Toast.show(</span>"已缩放至最小"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    const newScale </span>= scale - 0.1<span style="color: rgba(0, 0, 0, 1)">;
    setScale(newScale);
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">放大</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> pageZoomIn() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (scale &gt;= 5<span style="color: rgba(0, 0, 0, 1)">) {
      Toast.show(</span>"已放大至最大"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    const newScale </span>= scale + 0.1<span style="color: rgba(0, 0, 0, 1)">;
    setScale(newScale);
}

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
    </span>&lt;div&gt;<span style="color: rgba(0, 0, 0, 1)">
      {</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 预览 PDF 文件 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
      {fileUrl </span>?<span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div style={{ height: 'calc(100vh - 4.5rem)', overflowY: 'auto', padding: '24px' }}&gt;
          &lt;<span style="color: rgba(0, 0, 0, 1)">Document
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 写死的pdf文件地址,用于本地测试使用,打包提交前需要注释掉</span>
            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> file={new URL("/public/temp/AI销售助手-宽带&amp;套餐&amp;战新.pdf", import.meta.url).toString()}</span>
            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 真实传入的pdf地址</span>
            file=<span style="color: rgba(0, 0, 0, 1)">{fileUrl}
            onLoadSuccess</span>=<span style="color: rgba(0, 0, 0, 1)">{onDocumentLoadSuccess}
          </span>&gt;
            &lt;Page pageNumber={pageNumber} scale={scale} /&gt;
          &lt;/Document&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">      ) : (
      </span>&lt;p&gt;没有选择文件&lt;/p&gt;
<span style="color: rgba(0, 0, 0, 1)">      )}
      </span>&lt;div style={styleBtnDv}&gt;
      &lt;div style={styleBtn} onClick={lastPage}&gt;&lt;UpOutline color='#fff' fontSize={'0.6rem'} /&gt;&lt;/div&gt;
      &lt;div style={{ color: '#fff', fontSize: '0.35rem', ...styleBtn }}&gt;{pageNumber}/{numPages}&lt;/div&gt;
      &lt;div style={styleBtn} onClick={nextPage}&gt;&lt;DownOutline color='#fff' fontSize={'0.6rem'} /&gt;&lt;/div&gt;
      &lt;div style={styleBtn} onClick={pageZoomIn}&gt;&lt;AddCircleOutline color='#fff' fontSize={'0.6rem'} /&gt;&lt;/div&gt;
      &lt;div style={styleBtn} onClick={pageZoomOut}&gt;&lt;MinusCircleOutline color='#fff' fontSize={'0.6rem'} /&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">);
};

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> PDFViwer;</pre>
</div>
<h4>2.2、业务代码中引入该组件</h4>
<div class="cnblogs_code">
<pre>import React, { useMemo, useRef, useState } from 'react'<span style="color: rgba(0, 0, 0, 1)">
import { ErrorBlock, Swiper, SwiperRef, Popup, } from </span>'antd-mobile'<span style="color: rgba(0, 0, 0, 1)">
import PDFViwer from </span>'@/components/PDFViwer'<span style="color: rgba(0, 0, 0, 1)">;

const ellipsis1 </span>=<span style="color: rgba(0, 0, 0, 1)"> {
</span>"white-space": "nowrap"<span style="color: rgba(0, 0, 0, 1)">,
</span>"overflow": "hidden"<span style="color: rgba(0, 0, 0, 1)">,
</span>"text-overflow": "ellipsis"<span style="color: rgba(0, 0, 0, 1)">,
}
</span>
const IntroduceDocList = (props: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
const { loading, introduceDocList } </span>=<span style="color: rgba(0, 0, 0, 1)"> props
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> const introduceDocList = [</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   {publicFileUrl: '/public/temp/DXF文件要求.pdf', fileName:'DXF文件要求.pdf'},</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   {publicFileUrl: '/public/temp/AI销售助手-宽带&amp;套餐&amp;战新.pdf', fileName:'AI销售助手-宽带&amp;套餐&amp;战新.pdf'},</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ]<br><br></span></pre>
<pre>const [<span style="color: rgba(0, 0, 0, 1)">introduceDocList</span>, setI<span style="color: rgba(0, 0, 0, 1)">ntroduceDocList</span>] = useState({<br>  {publicFileUrl: 'http://****/abc.pdf', fileName:'abc.pdf'},<br>    {publicFileUrl: 'http://****/def.pdf', fileName:'def.pdf'},<br><span style="color: rgba(0, 0, 0, 1)"> });</span><span style="color: rgba(0, 128, 0, 1)"><br></span>
const = useState({ id: 1<span style="color: rgba(0, 0, 0, 1)"> });
const </span>= useState(<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)

const onOpenPdfViewer </span>= (item) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    console.log(item);
    setPdf(item);
    setShowPdfViwer(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
}

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (</span>
      &lt;div&gt;<span style="color: rgba(0, 0, 0, 1)">
      {
          introduceDocList</span>?.map(item =&gt;<span style="color: rgba(0, 0, 0, 1)"> (
            </span>&lt;div data-url={item?.publicFileUrl} style={{ marginBottom: '0.3rem', fontSize: '0.4rem' }}&gt;
            &lt;span style={{color:'#0B75FF'}} onClick={() =&gt; onOpenPdfViewer(item)}&gt;{item.fileName}&lt;/span&gt;
            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">          ))
      }
      </span>&lt;<span style="color: rgba(0, 0, 0, 1)">Popup
          position</span>='right'<span style="color: rgba(0, 0, 0, 1)">
          visible</span>=<span style="color: rgba(0, 0, 0, 1)">{showPdfViwer}
          showCloseButton
          bodyStyle</span>={{ width: '100%'<span style="color: rgba(0, 0, 0, 1)"> }}
          destroyOnClose</span>={<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">}
          onClose</span>={() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            setShowPdfViwer(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
            setPdf({ id: </span>1<span style="color: rgba(0, 0, 0, 1)"> })
          }}
      </span>&gt;
          &lt;div style={{ padding: '0.3rem 1rem', fontSize: '0.35rem', fontWeight: 600, textAlign:'center', ...ellipsis1 }}&gt;{pdf?.fileName}&lt;/div&gt;
          &lt;div style={{ height: '100%' }} data-url={pdf?.publicFileUrl}&gt;
            &lt;PDFViwer fileUrl={pdf?.publicFileUrl} /&gt;
          &lt;/div&gt;
      &lt;/Popup&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">)
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> IntroduceDocList</pre>
</div>
<h3>效果图:</h3>
<p><img src="https://img2024.cnblogs.com/blog/1048036/202409/1048036-20240914163525510-1769916366.png"></p>
<p>&nbsp;</p>
<p><span style="color: rgba(255, 0, 0, 1)">注意:挡在本地开发时,如果预览的pdf文件地址是线上地址,则会报跨域的问题,需要服务端解决跨域问题。</span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/libo0125ok/p/18414292
頁: [1]
查看完整版本: react-pdf预览在线PDF的使用