'How to fix Expo Print.printToFileAsync(options) undefined is not an object error

I am trying to use Expo's Print.printToFileAsync(options but I keep on getting [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPrint.Print.printToFileAsync')].

I looked for a lot of solutions online but couldn't find a solution to this. I started using React Native libraries but as I searched, turns out I can only use Expo's library so I switched to Print.printToFileAsync().

App.js

import React, {Component} from 'react';
import { StyleSheet, Text, View, TouchableHighlight } from 'react-native';
import { Print } from 'expo-print';

export default class App extends Component {

  async createPDF() {

    let filePath = await Print.printToFileAsync({
      html: "<h1>PDF TEST</h1>",
      width : 612,
      height : 792,
      base64 : false
    });

    alert('PDF Generated', filePath.uri);
  }

  render() {
    return(
      <View>
        <TouchableHighlight onPress={this.createPDF} style={styles.Main}>
          <Text>Create PDF</Text>
        </TouchableHighlight>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  Main : { marginTop : 100 }
});

Package.JSON

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "expo": "^33.0.0",
    "expo-print": "^5.0.1",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
    "react-native-html-to-pdf": "^0.7.0",
    "react-native-share": "^1.2.1",
    "react-native-web": "^0.11.4"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.1.1"
  },
  "private": true
}

My end goal is to make a PDF file using HTML in my Expo project.



Solution 1:[1]

The Expo sdk33 requires the module to be installed and run directly.

expo install expo-print

Modular Imports

With SDK 33, we’re deprecating imports of most modules from the expo package. This means that in a future release, you won’t be able to write, for example, import { FileSystem } from 'expo';. Rather, you’ll need to install the individual packages for each module you use and import from them instead.

You can use the new expo install command to install modules; this command is a wrapper around npm install/yarn add that automatically installs a module version compatible with your SDK version. For example, for the FileSystem module, you would run expo install expo-file-system and then use import * as FileSystem from 'expo-file-system';. This change paves the way for tree-shaking and smaller JavaScript bundles. It also makes moving between the managed and bare workflows simpler.

Imports from the expo package will continue to work in SDK 33 but will generate a warning in the console, and you’ll need to import from the individual packages to make the warning disappear. To make this change easier, we’re providing a codemod that’ll automatically update all of your imports for you.

Home page with description of SDK33

Solution 2:[2]

Please use - import * as Print from 'expo-print';

instead of - import { Print } from 'expo-print';

Solution 3:[3]

follow this link https://stackoverflow.com/a/71569236/14448694

import * as Print from "expo-print"; import { shareAsync } from "expo-sharing";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hong developer
Solution 2 Gurpreet singh
Solution 3 Sayan Dey