Example to Make PDF in React Native from HTML Text

Example to Make PDF in React Native from HTML Text

Here is an Example to Make PDF in React Native from HTML Text. In this example, we will create a PDF file from the HTML text after the click of a button in our App. To complete this task we will use react-native-html-to-pdf library. So let’s get started.

You can also visit Print HTML as a Document from React Native App for Android and iOS.

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 Dependency

To use RNHTMLtoPDF we need to install react-native-html-to-pdf package. To install this

Open the terminal and jump into your project

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-native-html-to-pdf.

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

Permission for Android

We are accessing external storage so we need to add some permission in AndroidManifest.xml file.

We are going to add the following permissions in the AndroidMnifest.xml

  • WRITE_EXTERNAL_STORAGE: To store the PDF in the SD card.
  • READ_EXTERNAL_STORAGE: To read the SD card.

For more about the permission, you can see this post.

Addition of MultiDexEnable for Android

To enable the multidex which is required by HTML to PDF converter library open Yourproject -> android -> app -> build.gradle and search for defaultConfig which comes under android and add

multiDexEnabled true

Please follow the below screenshot for clear understanding.

This is it. 🙂 Now we are ready to go to the code section.

Code to Make PDF in React Native from HTML Text

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

Here are the Screenshots of the application.

Asking for permission while opening the app for the first time.

Click on create PDF to create PDF

 

Have a look at the location in internal/external storage where we have created PDF from HTML

     
 

This is how you can make PDF from your React Native Application. 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. 🙂

 

36 thoughts on “Example to Make PDF in React Native from HTML Text”

    • As we can not use bootstrap or any other way to auto adjust the text width. So you have to define the width of the text manually in HTML. First you have to find the width of the page in px like for “A4″ page the size is 595px 842px so we have to define the width of the external div to width=”595px” which will restrict the width and text will automatically goes to the next line. If you are still facing the issue then can you share your content so that I can try once.

  1. hello.
    i am using expo and it brings a warning saying ” Possible unhandled Promise Rejection (id: 16):
    TypeError: undefined is not an oblective (evaluating’ _reactNativeHtmlToPdf.default.convert’) ”
    and then it doesn’t print

    • Is it a simple list or the custom component list?

      If you are willing to make a single list then you have to create the Dynamic HTML from the Array of elements like
      1. you have an array

      arr =[Coffee, Tea, Milk];

      2. make a varriable
      var x= ‘

        ‘;

        3. add the element using for loop
        for (let i = 0; i < arr.length; i++) { x = x + '

      • ‘ + arr[i] + ‘‘;
        }

        4. and at last add the closer of ul
        x = x + ‘

      ‘;

      Hope this will help you.

  2. I have used the above code to generate PDF,
    I would be getting a message
    Create PDF
    /storage/emulated/0/docs/test.pdf

    But I am not able to see pdf file in my storage.

  3. Getting, Possible Unhandled Promise Rejection(id, 0): TypeError: null is not an object(evaluating _reactNativeHtmlToPdf.default.convert’). Please provide a working solution. I am using react Native version 0.60.5

Leave a Comment

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