| 20 | import RNPrint from 'react-native-print'; |
| 21 | |
| 22 | export default class RNPrintExample extends Component { |
| 23 | state = { |
| 24 | selectedPrinter: null |
| 25 | } |
| 26 | |
| 27 | // @NOTE iOS Only |
| 28 | selectPrinter = async () => { |
| 29 | const selectedPrinter = await RNPrint.selectPrinter({}) |
| 30 | this.setState({ selectedPrinter }) |
| 31 | } |
| 32 | |
| 33 | // @NOTE iOS Only |
| 34 | silentPrint = async () => { |
| 35 | if (!this.state.selectedPrinter) { |
| 36 | alert('Must Select Printer First') |
| 37 | } |
| 38 | |
| 39 | const jobName = await RNPrint.print({ |
| 40 | printerURL: this.state.selectedPrinter.url, |
| 41 | html: '<h1>Silent Print</h1>' |
| 42 | }) |
| 43 | |
| 44 | } |
| 45 | |
| 46 | async printHTML() { |
| 47 | await RNPrint.print({ |
| 48 | html: '<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3>' |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | async printPDF() { |
| 53 | const results = await RNHTMLtoPDF.convert({ |
| 54 | html: '<h1>Custom converted PDF Document</h1>', |
| 55 | fileName: 'test', |
| 56 | base64: true, |
| 57 | }) |
| 58 | |
| 59 | await RNPrint.print({ filePath: results.filePath }) |
| 60 | } |
| 61 | |
| 62 | async printRemotePDF() { |
| 63 | await RNPrint.print({ filePath: 'https://graduateland.com/api/v2/users/jesper/cv' }) |
| 64 | } |
| 65 | |
| 66 | customOptions = () => { |
| 67 | return ( |
| 68 | <View> |
| 69 | {this.state.selectedPrinter && |
| 70 | <View> |
| 71 | <Text>{`Selected Printer Name: ${this.state.selectedPrinter.name}`}</Text> |
| 72 | <Text>{`Selected Printer URI: ${this.state.selectedPrinter.url}`}</Text> |
| 73 | </View> |
| 74 | } |
| 75 | <Button onPress={this.selectPrinter} title="Select Printer" /> |
| 76 | <Button onPress={this.silentPrint} title="Silent Print" /> |
| 77 | </View> |
| 78 | |
| 79 | ) |