【问题标题】:how to dynamically add a value in react-native-html-to-pdf如何在 react-native-html-to-pdf 中动态添加值
【发布时间】:2020-03-20 16:54:27
【问题描述】:

我正在尝试从用户那里获取输入,然后将其保存为 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);
  }

【问题讨论】:

    标签: reactjs react-native npm html-to-pdf


    【解决方案1】:

    您可以使用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);
      }
    

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

    【讨论】:

      【解决方案2】:

      您可以添加一个动态创建 html 的 setter 函数,然后将其设置为字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-19
        • 2020-01-06
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 2021-05-02
        • 2020-07-18
        • 1970-01-01
        相关资源
        最近更新 更多