ํน์ ํ๊ฒฝ(electron)์์ react-pdf๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฏ๋ก http/https๋ฅผ ํตํด ํ์ผ์ ๋ก๋ํ ์ ์์ง๋ง fs์์ ๋ก๋ํ ์ ์์ต๋๋ค.
onLoadSuccess๊ฐ ์คํ๋์ง ์์ ๊ฒ ๊ฐ์๋ฐ ์ ์์ธ๊ฐ์?
์ฒซ ํ์ด์ง๋ฅผ ๋ ๋๋งํ๊ธฐ ์ ์ ํ์ด์ง ์๋ฅผ ๊ฐ์ ธ์์ผ ํฉ๋๋ค.
ํ์คํ ์ ์์ด ์๋๋๋ค! ๊ทธ๋ ์ง ์์ผ๋ฉด ํ์ผ์ด ์๋ฒฝํ๊ฒ๋ก๋๋ฉ๋๊น? ์ฌ๊ธฐ์์ React-PDF ๊ตฌํ์ ๊ณต์ ํ ์ ์์ต๋๊น?
์, ํ์ผ์ด ์๋ฒฝํ๊ฒ ๋ก๋๋์์ต๋๋ค.
์์ ์ ์ธ ๋ฆด๋ฆฌ์ค์ 3 ์ํ์์ ์๋ํ์ต๋๋ค.
๋ด๊ฐ ๋ญ๊ฐ๋ฅผ ๋์ณค๋์?
export default class PDFPlayer extends PureComponent {
state = {
fileLoaded: 0,
numPages: null,
pageNumber: 1,
}
componentWillMount() {
const { filePath } = this.props;
if (fs.existsSync(filePath)) {
fs.readFile(filePath, (err, file) => {
if (err) { console.log(err); }
this.file = new Uint8Array(file);
this.setState({ fileLoaded: 1 });
});
}
}
onLoadSuccess = (pdf) => {
console.log('onDocumentLoad', pdf);
this.setState({ numPages: pdf.numPages });
}
onRenderSuccess = (page) => {
console.log(page.originalHeight);
}
render() {
const { fileLoaded } = this.state;
if (!fileLoaded) return null;
return (
<div style={{ ...styles.container, height: document.body.offsetHeight }}>
<Document
ref={(doc) => { this.document = doc; }}
file={{ data: this.file }}
onLoadSuccess={this.onLoadSuccess}
>
<Page onRenderSuccess={this.onRenderSuccess} pageNumber={1} />
</Document>
</div>
);
}
}
๊ทธ๊ฒ์ ์๋ฒฝํ๊ฒ ๊ด์ฐฎ์ ๋ณด์ธ๋ค! ์ต์ํ onSourceSuccess ์ฝ๋ฐฑ์ ๋ฐ๋์ง ๊ถ๊ธํฉ๋๋ค...!
๊ทธ๊ฑด ๊ทธ๋ ๊ณ , ๋๋ ๋น์ ์ด ๊ทธ๋ ๊ฒ ๋ง์ ์ผ์ ํ ํ์๊ฐ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
fs.readFile
์์์ด ๋ฌด์์ด๋ ๋ฐ์ ๋ค์ฌ์ง ๊ฒ์ด๋ผ๊ณ ๋ฏฟ์ต๋๋ค.file={this.file}
๋ ๊ด์ฐฎ์ต๋๋ค.๋ํ ํํธ, componentWillMount๋ ๊ณง ๋ ์ด์ ์ฌ์ฉ๋์ง ์์ต๋๋ค. componentDidMount์ ๋ฌธ์๋ฅผ ๋ก๋ํด ๋ณด์ธ์. :)
ํํธ๋ฅผ ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. componentDidMount
Uint8Array
์์ด ์๋ํฉ๋๋ค.ํดํน์ ์ฐพ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ํ์ด์ง๋ฅผ ๋ก๋ํ ํ ํ์ด์ง ์๋ฅผ ์์์ผ ํ๋ฏ๋ก ์ฒซ ๋ฒ์งธ ํ์ด์ง์ renderSuccess๊ฐ ๋ฌธ์ ์ํ์ ํ์ด์ง ์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ณ ์์ต๋๋ค. ๋๋ ๋ด ํ๋ก์ ํธ์ ์ฝ๊ฐ ๋ฐ๋น ์ ์ด๊ฒ์ ๋๋ฒ๊น ํ ์๊ฐ์ด ์์์ต๋๋ค. ์ ์์์ ์คํ ์ค์ด๊ฑฐ๋ ๋ก์ปฌ ํ์ผ์ ๋ก๋ํ๊ณ ์๊ธฐ ๋๋ฌธ์ธ์ง ํ์คํ์ง ์์ต๋๋ค. ์ด์จ๋ , ๋๋ฒ๊น ํ ์๊ฐ์ด ์๊ธฐ๋ฉด ๊ณ์ ์ ๋ฐ์ดํธํ๊ฒ ์ต๋๋ค.