Search code examples
reactjspdfreact-pdf

React-pdf submit multiple values


I am trying to create a form where you input some values in this case, name and surname and then you generate a pdf. I am using https://react-pdf.org/advanced#on-the-fly-rendering to aid with the task. However, I have only managed to submit one value. Code extract below:

<BlobProvider
          document={MyDoc({
            value: this.state.value,
          },{
            value1: this.state.value1,
          })}
        >
          {({ url }) => (
            <a href={url} target="_blank">
              Print
            </a>
          )}
        </BlobProvider>

I have tried multiple options in introducing the second value it appears as undefined when console.log, so it doesn't work let alone a much larger form. This is the full extent of the code https://codesandbox.io/s/strange-ramanujan-847ph?file=/src/App.js

Apologies, I didn't manage to make it work in codesandbox, but it does work in my code editor.


Solution

  • Managed to make it work and yes you can introdue as many values as possible

     <BlobProvider
              document={MyDoc({
                value: this.state.value,
                surname: this.state.surname, 
    **more values here**
              })}
            >
              {({ url }) => (
                <a href={url} target="_blank">
                  Print
                </a>
              )}
            </BlobProvider>
    

    The full extent of the code below:

    import React from "react";
    import { BlobProvider, Document, Page, Text, View } from "@react-pdf/renderer";
    
    const MyDoc = ({ value, surname }) => (
      <Document>
        <Page wrap>
          <Text>Section #1</Text>
    
          <View style={{ flexDirection: "row", margin: "2", color: "blue" }}>
            <Text style={{ padding: 10 }}>Name : </Text>
            <Text style={{ padding: 10 }}>{value}</Text>
            {console.log("name", value)}
          </View>
    
          <View style={{ flexDirection: "row", margin: "2", color: "blue" }}>
            <Text style={{ padding: 10 }}>Surname : </Text>
            <Text style={{ padding: 10 }}>{surname}</Text>
            {console.log("surname", surname)}
          </View>
        </Page>
      </Document>
    );
    
    class App extends React.Component {
      state = { value: "", surname: "" };
    
      render() {
        return (
          <div >
            <div>Pdf Generator</div>
            <form>
              <div >
                <div >
                  <label>name</label>
                  <input
    
                    value={this.state.value}
                    type="text"
                    onChange={(e) => this.setState({ value: e.target.value })}
                    placeholder="name"
                  />
                </div>
    
                <div>
                  <label>surname</label>
                  <input
                    value={this.state.surname}
                    type="text"
                    onChange={(e) => this.setState({ surname: e.target.value })}
                    placeholder="surname"
                  />
                </div>
              </div>
            </form>
    
            <BlobProvider
              document={MyDoc({
                value: this.state.value,
                surname: this.state.surname,
              })}
            >
              {({ url }) => (
                <a href={url} target="_blank">
                  Print
                </a>
              )}
            </BlobProvider>
          </div>
        );
      }
    }
    
    export default App;