pdf.js 字体无法显示
pdfjs-dist加载不全的解决方案
Rollup 配置 rollup-plugin-copy插件,进行打包构建时的文件复制
参考了网上诸多解决方案,都是webpack的引入包方式,
照猫画虎,把vite解决方案奉献给大家
vite.config.js
            
            
              js
              
              
            
          
          import copy from 'rollup-plugin-copy' //引入插件
// https://vitejs.dev/config/
export default defineConfig({
    base: '/',
    plugins: [
        react(),
        viteCompression(),
        copy({
            targets: [
                { src: 'node_modules/pdfjs-dist/cmaps/', dest: 'public/' }, //执行拷贝
            ]
        })
    ],
})
        主文件
            
            
              js
              
              
            
          
          const cmaps = new URL('/cmaps', import.meta.url).href
{<Document
    file={url}// 文件地址
    className="pdf-viewer-show"
    onLoadSuccess={onDocumentLoadSuccess}
    onLoadError={onDocumentLoadError}
    options={{
        cMapUrl: `${cmaps}/`,
        cMapPacked: true,
        // disableWorker: true,
    }}
>
    {Array.from(new Array(numPages), (el, index) => (
        <Page
            key={`page_${index + 1}`}
            pageNumber={index + 1}
            renderAnnotationLayer={false}
            renderTextLayer={false}
        />
    ))}
</Document>}
        最后页面中cmps静态资源位置位于


更多详细代码请关注公众号索取(备注:公众号):
