Answers for "add pdf in react app"

0

add pdf in react app

import React, { PureComponent } from "react"
import { Document, Page } from "react-pdf/build/entry.webpack"
import throttle from "lodash.throttle"
import pdf from "./pdf.pdf"

class App extends PureComponent {
  constructor(props) {
    super(props)
    this.state = {width: null}
    this.throttledSetDivSize = throttle(this.setDivSize, 500)
  }

  componentDidMount () {
    this.setDivSize()
    window.addEventListener("resize", this.throttledSetDivSize)
  }

  componentWillUnmount () {
    window.removeEventListener("resize", this.throttledSetDivSize)
  }

  setDivSize = () => {
    this.setState({width: this.pdfWrapper.getBoundingClientRect().width})
  }

  render() {
    return (
      <div id="row" style={{height: "100vh", width: "100vw", display: "flex", overflow: "hidden"}}>
        <div id="placeholderWrapper" style={{width: "10vw", height: "100vh"}}/>
        <div id="pdfWrapper" style={{width: "90vw"}} ref={(ref) => this.pdfWrapper = ref}>
          <PdfComponent wrapperDivSize={this.state.width} />
        </div>
      </div>
    )
  }
}

class PdfComponent extends PureComponent {
  render() {
    return (
      <div>
        <Document
          file={pdf}
        >
          <Page pageIndex={1} width={this.props.wrapperDivSize} />
        </Document>
      </div>
    )
  }
}

export default App
Posted by: Guest on May-14-2021
2

how to create a pdf in react

/*
Make sure to install the library
with yarn
yarn add @react-pdf/renderer
with npm
npm install @react-pdf/renderer --save
*/

import React from 'react';
import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';

// Create styles
const styles = StyleSheet.create({
  page: {
    flexDirection: 'row',
    backgroundColor: '#E4E4E4'
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1
  }
});

// Create Document Component
const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>Section #1</Text>
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
  </Document>
);
Posted by: Guest on December-14-2020
0

react pdf add to existing pdf

<Grid centered columns={2}>
      <Grid.Column textAlign="center" onClick={this.nextPage}>
      
        <Document file='/Sample.pdf' onLoadSuccess={this.onDocumentLoadSuccess} noData={<h4>Please select a file</h4>}>
          <Page pageNumber={pageNumber} >

          </Page>
        </Document>
        {this.state.file ? <p>Page {pageNumber} of {numPages}</p> : null}
      </Grid.Column>
    </Grid>
  </Container>
);
Posted by: Guest on March-24-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language