1

I have take image of screen view in by using view shot library. and after that I convert that image to pdf using react-native-image-to-pdf library. file saved successfully but. when I try to open that file its saying file format not supported. and also it has no .pdf extension. please help.

here is my state

 this.state = {
      imagePath: "",
      pdf: "",
    };

here is my function

 onCapture = (uri) => {
    console.log("do something with ", uri);
    this.setState({ imagePath: uri.replace("file://", "") });
    console.log("this is " + this.state.imagePath);
  };
 

imageToPdf = async () => {
    try {
      const options = {
        imagePaths: [this.state.imagePath],
        name: "TablePdf" + ".pdf",
        maxSize: {         
          width: 900,
        },
        quality: 0.7,
      };
      const pdf = await RNImageToPdf.createPDFbyImages(options);
      this.setState({ pdf: pdf.filePath });

      Linking.openURL(
        `mailto:support@example.com?subject=SendMail&body=${this.state.pdf}`
      );

      console.log(pdf.filePath);
    } catch (e) {
      console.log(e);
    }
  };

here is my View Shot and Entire View


   <ScrollView>
    <IconShare onPress={this.imageToPdf}/>
        <ViewShot
            onCapture={this.onCapture}
            captureMode="mount"
            options={{ format: "png", quality: 0.9 }}
            >

         <ScrollView horizontal={true}>
            <View></View>
            <View></View>
            <View></View>
            <View></View>
            <View></View>
            <View></View>
            <View></View>
            <View></View>
            <View></View>
         </ScrollView>

        </ViewShot>
  </ScrollView>


  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of what you've already tried. – shaedrich Jun 01 '21 at 13:13
  • 1
    please check now... I have updated Code –  Jun 01 '21 at 13:24
  • Thank you. What does `console.log("this is " + this.state.imagePath)` log and what's the output when you do `console.log(pdf.filePath);` – shaedrich Jun 01 '21 at 13:30
  • for this.state.imagePtath === /data/user/0/com.medicare/cache/ReactNative-snapshot-image3689159755902100299.png. , for pdf.filePath === /storage/emulated/0/Android/data/com.medicare/files/TablePdf.pdf –  Jun 01 '21 at 13:34
  • You can write the file name as one string instead of concatinating it with a plus sign. Try to rename the resulting file that it has the file extension .pdf. What happens if you open it now? – shaedrich Jun 01 '21 at 13:49
  • then its opening but showing blank black screen –  Jun 01 '21 at 13:50
  • Okay, so the file is indeed broken. I haven't used react-native-image-to-pdf but it looks like it might have a bug. – shaedrich Jun 01 '21 at 13:52
  • ok so.. is thier any solution on this –  Jun 01 '21 at 13:53
  • I don't know. Can you open the image? – shaedrich Jun 01 '21 at 13:54
  • I have Table data on my screen. I made that table using just View and Text. I Just want to send that table data as it is in table format to email. or on message... in pdf or docx file –  Jun 01 '21 at 13:54
  • no. image not opening I dont see any image –  Jun 01 '21 at 13:56
  • Maybe the image is already broken. That might be why the pdf is broken as well. Have you tried another library yet? – shaedrich Jun 01 '21 at 13:56
  • no I dont try any library other than this two. if there is another library please tell me. –  Jun 01 '21 at 13:58
  • I don't know any specifically for react native. But maybe someone else does – shaedrich Jun 01 '21 at 14:09

0 Answers0