christopherdro / react-native-html-to-pdf

Convert html strings to PDF documents using React Native
MIT License
434 stars 264 forks source link

How to pass data from react native to html content and receive data from html to react native #176

Closed sagarrs closed 3 years ago

sagarrs commented 4 years ago

Hi i am making a API call in react native i want to pass the response of that API call to html and receive data from html to react native can someone please tell me how to achieve it ? there is no "options" to send or receive data.

import React, { Component } from 'react';
import {Text,TouchableHighlight,View,} from 'react-native';
import RNHTMLtoPDF from 'react-native-html-to-pdf';
import htmlContent from './htmlContent'

export default class Example extends Component {
  state = {
      apiData : []  
  }

  componentDidMount(){
      fetch(`http://API`)
          .then((response) => response.json())
          .then((responseJson) => {
              **console.log("DATA", responseJson) // NEED TO SEND THIS DATA TO "HTML"**
              this.setState(() => ({
                  apiData: responseJson
              }))
        })
  }

  async createPDF() {
    let options = {
      **html: htmlContent,**
      fileName: 'test',
      directory: 'Documents',
    };

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

  render() {
    return(
      <View>
        <TouchableHighlight onPress={this.createPDF}>
          <Text>Create PDF</Text>
        </TouchableHighlight>
      </View>
    )
  }
}
14BCS1464 commented 3 years ago

Here is the solution

const createpdf=()=>{ options = { html:htmlForInvoice("Sunil","praja@gmail.com","8437513006","$100"), fileName: 'tGenieInvoice_' + uuid.v4(), directory: 'Documents', };

    try {
        let file = await RNHTMLtoPDF.convert(options)
        console.log(file.filePath);
        openGeneratedPDF(file.filePath, 'application/pdf');
    } catch (err) {
        console.error(err)
    }

}

"export default function htmlForInvoice( name: string, email: string, phone: string, pay: string, ) { let temp = 'sunil'; return `

${name}

`; }"

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.