christopherdro / react-native-html-to-pdf

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

Crash in iOS #297

Open nishivinodbhatt opened 1 year ago

nishivinodbhatt commented 1 year ago

On iOS, a crash occurs randomly. Attaching a screenshot of the same. Please provide a solution for this.

Simulator Screenshot - iPhone 14 - 2023-07-05 at 14 22 21

stale[bot] commented 5 months 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.

Energieman commented 2 months ago

This error occurs when you have multiple call to create. You can fix it by using a state to check if document generation has ended. A thousand word is not enough, here is a code example.

const preparePDF = async (callBack = () => {}) => { try{

  if(generatingDocument){ //here is the solution. If generatingDocument is true, no further call will be made
    return;
  }

  setGeneratingDocument(true) // if  generatingDocument is not true, set it to true so no further calls will be possible until pending process is finished

  await deleteAFile(`file://${generatedFilePath}`)
  const fileName = convertToValidFileName(`${possibleFileName}_${Date.now()}`)

  let options = {
    html: HTMLTemplate(dataArray, userData, {template: currentSkin}),
    fileName,
    height: Number(CONSTANT_MESSAGES.PDFHeight),
    width: Number(CONSTANT_MESSAGES.PDFWidth),
    padding: 0,
    bgColor: '#ffffff'
  };

  let file = await RNHTMLtoPDF.convert(options)
  setGeneratedFilePath(file)
  setGeneratingDocument(false) // close the  generatingDocument check so new call can be successful without crashing your app
  callBack?.(file)

}catch(e){
  callBack?.({}, e)
}

}

This code I provided is not perfect. But should be enough for demostration purpose