首页 > 解决方案 > 如何在 react-native-html-to-pdf 中动态添加值

问题描述

我正在尝试从用户那里获取输入,然后将其另存为 pdf 文件,但在示例中没有显示如何在 pdf 中添加动态值的任何方法这里是示例

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);

  }

我正在尝试做这样的事情

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);
  }

标签: reactjsreact-nativenpmhtml-to-pdf

解决方案


您可以使用模板文字

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);
  }

请注意,这假定它this.state可用于函数范围


推荐阅读