Search code examples
reactjsreact-nativenpmhtml-to-pdf

how to dynamically add a value in react-native-html-to-pdf


I am trying to take an Input from the user and and then save it as a pdf file but in the example there isn't any way shown on how to add a dynamic value in the pdf here is the example

async createPDF() {
    let options = {
      html: '<h1>PDF TEST</h1>',
      fileName: 'test',
      directory: 'Documents',
    };

    let file = await RNHTMLtoPDF.convert(options)
    // console.log(file.filePath);
    alert(file.filePath);

  }

and I am trying to do some thing like this

async createPDF() {
    let options = {
      html: '<h1>PDF {this.state.value}</h1>',
      fileName: 'test',
      directory: 'Documents',
    };

    let file = await RNHTMLtoPDF.convert(options)
    // console.log(file.filePath);
    alert(file.filePath);
  }

Solution

  • You can use Template Literals

    async createPDF() {
        let options = {
          html: `<h1>PDF ${this.state.value}</h1>`,
          fileName: 'test',
          directory: 'Documents',
        };
    
        let file = await RNHTMLtoPDF.convert(options)
        // console.log(file.filePath);
        alert(file.filePath);
      }
    

    Note that this assumes that this.state is available to the function scope