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

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

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 example is the updated version of our previous 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. Weare 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 option 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

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

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

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

Addition of MultiDexEnable for Android

To handle Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’ errorin 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

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

App.js

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).

Android

        
 

iOS

          

This is how you can print HTML as a document from React Native app for Android and iOS. If you have any doubt 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. 🙂

 

1 thought 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.

Leave a Comment

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