
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
react-pdf-simple-viewer
Advanced tools
   => {
const [pages, setPages] = useState<number[]>([]);
return (
<div>
<Document
URL="http://localhost:1234/123.pdf"
onSuccess={async PDF => {
const { numPages } = PDF;
setPages(
Array.from({ length: numPages })
.fill(0)
.map((val, index) => index + 1)
);
}}
>
{pages.map(value => {
return <Page index={value} key={value} width={857} scale={1}></Page>;
})}
</Document>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
配合 react-window(虚拟滚动提升性能) + react-virtualized-auto-sizer 实现全屏滚动预览。
import React, { useState } from 'react';
import { VariableSizeList as List } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';
import { Document, getPageHeight, Page } from 'react-pdf-simple-viewer';
import { createGlobalStyle } from 'styled-components';
// 给body设置高度,AutoSizer才可以获取到实际高度
const GlobalStyle = createGlobalStyle`
body {
margin: 0;
}
html,
body,
#root {
height: 100%;
overflow-x: hidden;
background: #f2f5f8;
}
`;
const Row: React.FC<{
index: number;
style: React.CSSProperties;
data: number;
}> = ({ style, index, data }) => {
return (
<div style={style}>
{/* //注意,这里我们给每页增加了7px的边框,高度计算那边也需要计算上 */}
<Page
index={index + 1}
width={data}
style={{ margin: '0px auto', borderBottom: 'solid 7px #f2f5f8' }}
></Page>
</div>
);
};
const Example2: React.FC = () => {
const WIDTH = 864;
const [pagesHeight, setPagesHeight] = useState<number[]>([]);
async function successHandler(pdf: any) {
const { numPages } = pdf;
const array = Array.from({ length: numPages }).fill(0);
// 计算每一页的高度
const pagesHeight = await Promise.all(
array.map((i, index) => {
return getPageHeight(pdf, { index: index + 1, width: WIDTH });
})
);
setPagesHeight(pagesHeight);
}
function getItemSize(index: number): number {
// 为什么加7看Row组件注释
return pagesHeight[index] + 7;
}
return (
<>
<GlobalStyle></GlobalStyle>
<AutoSizer>
{({ height, width }) => (
<Document URL="/0410100.pdf" onSuccess={successHandler}>
{pagesHeight.length !== 0 && (
<List
height={height}
width={width}
itemCount={pagesHeight.length}
itemSize={getItemSize}
itemData={WIDTH}
>
{Row}
</List>
)}
</Document>
)}
</AutoSizer>
</>
);
};
export default function App() {
return <Example2 />;
}
这里是Code Sanbox demo.
URL
:
onSuccess
:
onError
:
index
:
width
:
scale
:
hideTextLayer
:
style
:
FAQs
  ![typescript](https://img.shields.io/badge/%E6%94%AF%E6%8C%81-typescript-blue?s
The npm package react-pdf-simple-viewer receives a total of 43 weekly downloads. As such, react-pdf-simple-viewer popularity was classified as not popular.
We found that react-pdf-simple-viewer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.