Skip to content

Use PDF utilities


In this section teaches you how to use the PDF utilities (download and preview) in a Etendo subapp.


This tutorial requires an already functional subapp. If you don't have the environment, please follow the steps in [Getting Started]](/developer-guide/etendo-mobile/getting-started/){target="_blank"} in the Etendo Mobile section.


Checking dependencies

Before starting, make sure you have the following dependencies listed in package.json and installed:

Hook creation

In the hook folder of your subapp, create a new file called usePDF.tsx and paste the following code:

import { useState } from 'react';
import { Platform } from 'react-native';
import ReactNativeBlobUtil from 'react-native-blob-util';
import Share from 'react-native-share';

// Simple verification to check if the device is Android
const isAndroidDevice = (() => {
  return Platform.OS === 'android';

// Constants for the download
const MIME_TYPE = 'application/pdf';
const FILE_EXTENSION = 'pdf';

const usePDF = () => {
  // Loacal States
  const [isLoading, setIsLoading] = useState(false);
  const [isDownloadDone, setIsDownloadDone] = useState(false);
  const [_pdf, setPdf] = useState<any>(null);
  const [path, setPath] = useState('');

  const downloadPDF = async ({
  }: {
    url: string;
    method: Methods;
    fileName: string;
    optionsHeader: any;
    description?: string;
    callback?: (...params: any[]) => void;
  }) => {
    // Set the path of the file and start the loading
    const dirs = ReactNativeBlobUtil.fs.dirs;
    const pathFile = `${
      isAndroidDevice ? dirs.LegacyDownloadDir : dirs.DocumentDir
    // Congfigurations for the download
    const config = {
      fileCache: true,
      appendExt: FILE_EXTENSION,
      path: pathFile,
      addAndroidDownloads: {
        mime: MIME_TYPE,
        title: `${fileName}.${FILE_EXTENSION}`,
        description: description || 'PDF File is dowmloaded.',
        mediaScannable: true,
        notification: true,

      .fetch(method, url, optionsHeader)
      .then(res => {
        const filePath = res.path();
        const fileOptions = {
          path: filePath,
          mime: MIME_TYPE,
        if (isAndroidDevice) {
        } else {
          let shareOptions = {
            type: MIME_TYPE,
            url: filePath,
            saveToFiles: true,
      .then(_res => {
        // If the file is saved, set the state to true
        // and exceute the callback(if it exists)
        callback && callback();
      .catch(err => {
      .finally(() => {
        // Finally, set the loading to false

  return { downloadPDF, isDownloadDone, isLoading, path, setPdf };

export default usePDF;


There are some things to consider in this hook:

  • The hook handles only the downloading of the PDF file. To preview it, you'll need to utilize the react-native-pdf library, which will be explained in more detail beforehand.
  • The path of the file is set in the LegacyDownloadDir folder for Android devices and in the DocumentDir folder for iOS devices. This is because the react-native-blob-util library has a different way of handling the download for each platform. So the file easy to find in both platforms, but you can change it if you want. For more information, please check the react-native-blob-util documentation

Functions and States exported


  • _ downloadPDF: is the function that handles the download of the PDF file. It receives an object with the following parameters:
    • url: the url of the file
    • method: the method of the request
    • fileName: the name of the file (without the .pdf extension)
    • optionsHeader: the options of the header
    • description: the description of the file that will be displayed on Android devices during the downloading process (optional)
    • callback: is a callback function that will be executed upon successful completion.(optional)
  • isDownloadDone: is a boolean that indicates if the download is done
  • isLoading: is a boolean that indicates if the download is in progress
  • path: is the final path of the file
  • setPdf: is a function that sets the PDF file (internal usage at rendering)


Calling the hook

In the file where you want to use the hook, import it and call it:

import { Button, Layout } from 'etendo-ui-library';
import usePDF from './hooks/usePDF';

Components used

The components used in this example (Button and Layout) are from the Etendo UI Library

Downloading the PDF

To download the PDF file, you need to call the downloadPDF function and pass the parameters. In this example, we are using a button to trigger the download:

const { downloadPDF, isLoading, isDownloadDone, path, setPdf } = usePDF();

return (
        text="Download PDF"
        onPress={() =>
            url: ``,
            method: 'GET',
            fileName: 'fileNameOfPDF',
            optionsHeader: {
              'Content-Type': 'application/json',
            description: 'File description',

This will be displayed as follows:

main-screen.png loading-screen.png

Previewing the PDF

To preview the PDF file, you need to use the react-native-pdf library. Following the example above, you can use the path state to set the source of the PDF file and the setPdf function to set the PDF file:

<View style={styles.container}>
    ref={pdf => {
      isDownloadDone && {
        uri: path,

Styles used

The styles used in this example (container and pdf) are the following:

  const width = Dimensions.get('window').width;
  const height = Dimensions.get('window').height;
  const styles = StyleSheet.create({
    container: {
      flexDirection: 'row',
      justifyContent: 'center',
      alignItems: 'center',
      marginTop: 25,
      backgroundColor: 'red',
    pdf: {
      flex: 1,

After save, the final result, you should see something like this:

saving-file.png pdf-show.png