React-pdf: Uint8Array ๋ฐ์ดํ„ฐ๋กœ onLoadSuccess๊ฐ€ ์‹œ์ž‘๋˜์ง€ ์•Š์Œ

์— ๋งŒ๋“  2018๋…„ 02์›” 21์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: wojtekmaj/react-pdf

ํŠน์ • ํ™˜๊ฒฝ(electron)์—์„œ react-pdf๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ http/https๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ ๋กœ๋“œํ•  ์ˆ˜ ์—†์ง€๋งŒ fs์—์„œ ๋กœ๋“œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
onLoadSuccess๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์€๋ฐ ์ •์ƒ์ธ๊ฐ€์š”?
์ฒซ ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋งํ•˜๊ธฐ ์ „์— ํŽ˜์ด์ง€ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

question

๋ชจ๋“  4 ๋Œ“๊ธ€

ํ™•์‹คํžˆ ์ •์ƒ์ด ์•„๋‹™๋‹ˆ๋‹ค! ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํŒŒ์ผ์ด ์™„๋ฒฝํ•˜๊ฒŒ๋กœ๋“œ๋ฉ๋‹ˆ๊นŒ? ์—ฌ๊ธฐ์—์„œ 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 ์ฝœ๋ฐฑ์„ ๋ฐ›๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค...!
๊ทธ๊ฑด ๊ทธ๋ ‡๊ณ , ๋‚˜๋Š” ๋‹น์‹ ์ด ๊ทธ๋ ‡๊ฒŒ ๋งŽ์€ ์ผ์„ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

  1. fs.readFile ์–‘์‹์ด ๋ฌด์—‡์ด๋“  ๋ฐ›์•„ ๋“ค์—ฌ์งˆ ๊ฒƒ์ด๋ผ๊ณ  ๋ฏฟ์Šต๋‹ˆ๋‹ค.
  2. file={this.file} ๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ํžŒํŠธ, componentWillMount๋Š” ๊ณง ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. componentDidMount์— ๋ฌธ์„œ๋ฅผ ๋กœ๋“œํ•ด ๋ณด์„ธ์š”. :)

ํžŒํŠธ๋ฅผ ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. componentDidMount


    1. ์˜ˆ, Uint8Array ์—†์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.


    1. ์•„๋‹ˆ์š”, ๊ฐœ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ  ํŒŒ์ผ์„ ์ง์ ‘ ๋กœ๋“œํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

ํ•ดํ‚น์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•œ ํ›„ ํŽ˜์ด์ง€ ์ˆ˜๋ฅผ ์•Œ์•„์•ผ ํ•˜๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ํŽ˜์ด์ง€์˜ renderSuccess๊ฐ€ ๋ฌธ์„œ ์ƒํƒœ์˜ ํŽ˜์ด์ง€ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‚ด ํ”„๋กœ์ ํŠธ์— ์•ฝ๊ฐ„ ๋ฐ”๋น ์„œ ์ด๊ฒƒ์„ ๋””๋ฒ„๊น…ํ•  ์‹œ๊ฐ„์ด ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์ „์ž์—์„œ ์‹คํ–‰ ์ค‘์ด๊ฑฐ๋‚˜ ๋กœ์ปฌ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ธ์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์–ด์จŒ๋“ , ๋””๋ฒ„๊น… ํ•  ์‹œ๊ฐ„์ด ์ƒ๊ธฐ๋ฉด ๊ณ„์† ์—…๋ฐ์ดํŠธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰