Reading FileSystem files does not work

I want to test sending via email PDFs generated in my Expo Client app. I was doing some testing in a separate project and I was not able to make it work.

import React from 'react';
import { TouchableHighlight, Text, View } from 'react-native';
import * as Print from 'expo-print';
import * as FileSystem from 'expo-file-system';
import * as MailComposer from 'expo-mail-composer';

export default class App extends React.Component {
  state = { uri: '' };

  createPDF = () => {
    let options = {
      html: '<h1>PDF TEST</h1>'
    };

    Print.printToFileAsync(options)
      .then(res => {
        console.log(res);
        this.setState({ uri: res.uri });
      })
      .catch(e => console.log(e));
  };

  sendEmail = () => {
    FileSystem.readAsStringAsync(this.state.uri)
      .then(doc => console.log(doc)) // send email..
      .catch(e => console.log(e));
  };

  render() {
    return (
      <View style={{ justifyContent: 'center', alignItems: 'center', flex: 1 }}>
        <TouchableHighlight style={{ padding: 10 }} onPress={this.createPDF}>
          <Text>Create PDF</Text>
        </TouchableHighlight>
        <TouchableHighlight style={{ padding: 10 }} onPress={this.sendEmail}>
          <Text>Send Email</Text>
        </TouchableHighlight>
      </View>
    );
  }
}

The error I’m getting is:
File 'file:///var/mobile/Containers/Data/Application/CF......862/Library/Caches/ExponentExperienceData/%2540turnosya%252FGeneradorPDF/Print/F.......5E1.pdf' could not be read.

why are you using readAsStringAsync? you should just pass the uri as an attachment

I checked again the documentation and it seems like I was confused. I had thought I had to read it before attaching it, but I see you can simply attach it with the uri. That was a distraction from my part. Thank you…

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.