Print documents using React Native.
Run npm install react-native-print --save
Run react-native link
- Open your project in XCode, right click on Libraries and select Add Files to "Your Project Name.
- Choose the file
node_modules/react-native-print/RNPrint.xcodeproj
- Go to
Project Manager
tab and click on your project's name. Select the name of the target and click onBuild Phases
- Add
libRNPrint.a
toLink Binary With Libraries
(Screenshot).
- Edit
android/settings.gradle
to included
include ':react-native-print'
project(':react-native-print').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-print/android')
- Edit
android/app/build.gradle
file to include
dependencies {
....
compile project(':react-native-print')
}
- Edit
MainApplication.java
to include
// import the package
import com.rnprint.RNPrint.RNPrint;
// include package
new MainReactPackage(),
new RNPrint(),
import React from 'react';
import {
Platform,
Text,
TouchableHighlight,
View,
} from 'react-native';
import {RNPrint} from 'NativeModules';
var Example = React.createClass({
printDocument(filePath) {
RNPrint.print(filePath).then((jobName) => {
console.log(`Printing ${jobName} complete!`);
});;
},
printHTML(htmlString) {
// only available on android
if (Platform.OS === 'android'){
RNPrint.printhtml(htmlString).then((jobName) => {
console.log(`Printing ${jobName} complete!`);
});
}
},
render() {
<View>
<TouchableHighlight onPress={this.printPDF(filePath)}>
<Text>Print Document</Text>
</TouchableHighlight>
</View>
}
});
The example project included demonstrates how you use react-native-html-to-pdf
to create a PDF from a html string then prompt the print dialog.