Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify an existing PDF in React.js?

Tags:

reactjs

pdf

I have few values in my react state array that I'm passing through props to the current component. But instead of displaying as they are currently displayed in the code below, I would like to use a PDF template that's in my file system.

So my expected outcome is to be able to draw the given data from the state (text or images) on a copy of the original PDF at a given (x,y) coordinates and be able to email the new PDF or save it to the Local storage devices. Given below is my existing code which only displays the data in a container.

  export class FormReview extends Component{

 constructor(props) {
   super(props);
   this.state = {value:0};
 }

 /*methods used to navigate between multistep forms*/
 continue = e => {
   e.preventDefault();
   this.props.nextStep();
 }

 back = e => {
   e.preventDefault();
   this.props.prevStep();
 }




 render() {
   const {values} = this.props;

   return(

     <MuiThemeProvider>
       <React.Fragment>

         <AppBar title="Review Submission"/>
         <div>
         <Grid container spacing={2} style={{padding:50}}>
         <Grid item xs={1}/>
         <Grid item xs={10}>


         <Paper>


         <Grid item xs={12} sm container>
           <Grid item xs container direction="column">
             <Grid item xs style={{textAlign:'left', padding:20}}>

             <Container style={{ height:'500', backgroundColor:'#A9A9A9', overflow: 'scroll'}}>

          <List>
          <ListItem
          primaryText='Student Name'
          secondaryText={values.stu_name}/>


          <ListItem
          primaryText='Flight Registration'
          secondaryText={values.rego}/>

          <ListItem
          primaryText='Date'
          secondaryText={values.fl_date}/>

          <ListItem
          primaryText='Hours'
          secondaryText={values.fl_clock}/>

          <ListItem
          primaryText='Completed?'
          secondaryText={values.pfk_bool}/>

          <ListItem
          primaryText='next?'
          secondaryText={values.nextLesson_bool}/>

          /*formRating contains performance ratings of students depending on the lesson undertaken*/

          {values.formRating.map(
            item =>
            <Grid container>
                  <Grid item xs={2} style={{marginTop:20, marginRight:0}}>{item.condId} </Grid>

                  <Grid item xs={6} style={{marginTop:20}}>{item.condition} </Grid>
                    <Grid item xs={4} style={{marginTop:10}}>{item.rate}</Grid>



                </Grid>)}</List>

          </Container>



      <Grid item xs={12}>
      <RaisedButton label="Email & Save" secondary={true} onClick={this.continue} fullWidth='true' style={{marginTop:20, height:60}}/>
      <ButtonGroup variant="contained"  style={{marginTop:20}} fullWidth >
      <Button>Save Only</Button>
      <Button>Cancel</Button>
    </ButtonGroup>
      </Grid>
        </Grid >

        </Grid>
      </Grid>
        </Paper>
      </Grid>

      <Grid item xs={1}/>



    </Grid>



      </div>
    </React.Fragment>
  </MuiThemeProvider>
   )
 }
 }


  export default FormReview

What's expected is a NEW PDF to be displayed in the container which is based on the template PDF (in local file system) with the data drawn on it at given coordinates. This new documents should be able to be emailed or saved to the local storage as per user preference.

I'm a total beginner to React.

like image 754
Dworo Avatar asked Jan 31 '26 06:01

Dworo


1 Answers

Use pdf-lib.js to modify the content of pdfs in react. You can modify existing pdfs, add new pages, remove pages , draw text & images etc.

Check out this link: https://github.com/Hopding/pdf-lib

like image 138
neelesh bisht Avatar answered Feb 01 '26 19:02

neelesh bisht