Print HTML as a Document from React Native App for Android and iOS

Print a Document from React Native App

Here is an Example to Print HTML as a Document from React Native App for Android and iOS. In this example, we will see how to print HTML as a document, make pdf from HTML and print remote PDF from URL.

This is an extended example fo  Example to Make PDF in React Native from HTML Text.

Print HTML as Document means to print any HTML from our React Native application as a PDF document. We can also save the same HTML as PDF and can also print the remote PDF document.

We are using RNPrint component provided from react-native-print to trigger the default print option of the device and will use RNHTMLtoPDF component provided by react-native-html-to-pdf to make pdf from HTML.

In this example, you will see two extra options available for the iOS which are to choose the printer and silent print (Direct printing in the background). So let’s get started with the print HTML as a document example.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

Open the terminal and jump into your project

cd ProjectName

1. For the printing option, we will use RNPrint component and for that install react-native-print dependency using

npm install react-native-print --save

2. To make PDF we will use RNHTMLtoPDF component and for that install react-native-html-to-pdf dependency using

npm install react-native-html-to-pdf --save

This command will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use

cd ios && pod install && cd ..

Addition of MultiDexEnable for Android

To handle Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’ error in Android we have to enable multiDex. To enable the multidex open Yourproject -> android -> app -> build.gradle and search for defaultConfig and dependencies and add the following lines

defaultConfig {
    multiDexEnabled true
dependencies {
    implementation ''

Please follow the below screenshot for better understanding For more about multiDex you can visit Enable multidex for apps with over 64K methods This is it. 🙂 Now we are ready to go to the code section.

Code to Print HTML as a Document

Open App.js in any code editor and replace the code with the following code


//Example to Print a Document from React Native App
import React, { Component } from 'react';
import {
} from 'react-native';
import RNHTMLtoPDF from 'react-native-html-to-pdf';
import RNPrint from 'react-native-print';

export default class App extends Component {
  state = {
    selectedPrinter: null,

  // Only for iOS
  selectPrinter = async () => {
    const selectedPrinter = await RNPrint.selectPrinter({ x: 100, y: 100 });
    this.setState({ selectedPrinter });

  // Only for iOS
  silentPrint = async () => {
    if (!this.state.selectedPrinter) {
      alert('Must Select Printer First');
    const jobName = await RNPrint.print({
      printerURL: this.state.selectedPrinter.url,
      html: '<h1>Silent Print clicked</h1>',

  async printHTML() {
    await RNPrint.print({
        '<h1>Here will be Heading 1</h1><h2>Here will be Heading 2</h2><h3>Here will be Heading 3</h3>',

  async printPDF() {
    const results = await RNHTMLtoPDF.convert({
      html: '<h1>Demo Text to converted to PDF</h1>',
      fileName: 'test',
      base64: true,
    await RNPrint.print({ filePath: results.filePath });

  async printRemotePDF() {
    await RNPrint.print({
      filePath: '',

  customOptions = () => {
    return (
        {this.state.selectedPrinter && (
            <Text>{`Selected Printer Name: ${
            <Text>{`Selected Printer URI: ${
        <TouchableOpacity style={styles.button} onPress={this.selectPrinter}>
          <Text>Click to Select Printer</Text>
        <TouchableOpacity style={styles.button} onPress={this.silentPrint}>
          <Text>Click for Silent Print</Text>

  render() {
    return (
      <View style={styles.container}>
        <Text style={{ fontSize: 25, textAlign: 'center', marginBottom: 20 }}>
          Example to Print a Document from React Native App
        {Platform.OS === 'ios' && this.customOptions()}
        <TouchableOpacity style={styles.button} onPress={this.printHTML}>
          <Text>Click to Print HTML</Text>
        <TouchableOpacity style={styles.button} onPress={this.printPDF}>
          <Text>Click to Print PDF</Text>
        <TouchableOpacity style={styles.button} onPress={this.printRemotePDF}>
          <Text>Click to Print Remote PDF</Text>

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10,
    width: 300,
    marginVertical: 10,

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots





This is how you can print HTML as a document from the React Native app for Android and iOS. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned! Hope you liked it. 🙂


Related Posts

2 thoughts on “Print HTML as a Document from React Native App for Android and iOS”

  1. It gives me error “Unfortunately, App name stopped working”?
    Please can someone help me. Following the the exact steps as above.

  2. [Unhandled promise rejection: TypeError: null is not an object (evaluating ‘_reactNativePrint.default.print’)]
    – node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
    – node_modules\regenerator-runtime\runtime.js:274:30 in invoke
    – node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
    – node_modules\regenerator-runtime\runtime.js:135:28 in invoke
    – node_modules\regenerator-runtime\runtime.js:170:17 in PromiseImpl$argument_0
    – node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.