React Native Expo: Download a PDF generated by Rails wicked pdf gem that’s stored in tmp folder

I am new to react native and I want to create a mobile version of the the project I made in Ruby on Rails. One part there is in my RoR project it’s easy to generate and download a pdf file. I want also want to do it in a Mobile App. But well it’s not working I’ve been asking a lot of question to my github co-pilot and chatgpt for answers but I can’t still figure it out.

I tested the RoR side since I am much familiar in that side and It generates the pdf file as expected the problem is the mobile app. Whenever I press the button it says it’s successfully downloaded the file but I got prompt or I can’t find the pdf file in the android emulator or even in my phone.

First here’s my rails code:

usersquery_controller.rb

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class UsersqueryController < ApplicationController
before_action :authenticate_user!
def download_test
File.delete(Rails.root.join('tmp', 'test.pdf'))
pdf_html = ActionController::Base.new()
pdf_file = WickedPdf.new.pdf_from_string(pdf_html.render_to_string(template: 'usersquery/blank.html.erb', layout: false))
pdf_file_path = Rails.root.join('tmp', 'test.pdf')
File.open(pdf_file_path, 'wb') do |file|
file << pdf_file
end
send_file(pdf_file_path, filename: "test.pdf", type: "application/pdf", disposition: "attachment")
end
end
</code>
<code>class UsersqueryController < ApplicationController before_action :authenticate_user! def download_test File.delete(Rails.root.join('tmp', 'test.pdf')) pdf_html = ActionController::Base.new() pdf_file = WickedPdf.new.pdf_from_string(pdf_html.render_to_string(template: 'usersquery/blank.html.erb', layout: false)) pdf_file_path = Rails.root.join('tmp', 'test.pdf') File.open(pdf_file_path, 'wb') do |file| file << pdf_file end send_file(pdf_file_path, filename: "test.pdf", type: "application/pdf", disposition: "attachment") end end </code>
class UsersqueryController < ApplicationController
  before_action :authenticate_user!
  def download_test
    File.delete(Rails.root.join('tmp', 'test.pdf'))
    pdf_html = ActionController::Base.new()
    pdf_file = WickedPdf.new.pdf_from_string(pdf_html.render_to_string(template: 'usersquery/blank.html.erb', layout: false))
    pdf_file_path = Rails.root.join('tmp', 'test.pdf')
    File.open(pdf_file_path, 'wb') do |file|
      file << pdf_file
    end
    send_file(pdf_file_path, filename: "test.pdf", type: "application/pdf", disposition: "attachment")
  end
end

routes.rb

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth'
get '/download_test', to: 'usersquery#download_test'
end
</code>
<code>Rails.application.routes.draw do mount_devise_token_auth_for 'User', at: 'auth' get '/download_test', to: 'usersquery#download_test' end </code>
Rails.application.routes.draw do
  mount_devise_token_auth_for 'User', at: 'auth'
  get '/download_test', to: 'usersquery#download_test' 
end

Then here’s my react native
download.jsx

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React from 'react';
import { View, Button, Alert, Platform } from 'react-native';
import * as FileSystem from 'expo-file-system';
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
import { encode } from 'base-64';
const getAuthHeaders = async () => {
const accessToken = await AsyncStorage.getItem('accessToken');
const tokenType = await AsyncStorage.getItem('tokenType');
const client = await AsyncStorage.getItem('client');
const expiry = await AsyncStorage.getItem('expiry');
const uid = await AsyncStorage.getItem('uid');
return {
'access-token': accessToken,
'token-type': tokenType,
'client': client,
'expiry': expiry,
'uid': uid,
};
};
const DownloadPDFButton = () => {
const handleDownload = async () => {
try {
const headers = await getAuthHeaders();
const response = await axios.get('http://192.168.0.102:3000/download_test', {
headers: headers,
responseType: 'arraybuffer',
});
const arrayBuffer = response.data;
const base64String = encode(arrayBuffer);
// Determine the directory based on the platform
let directory;
if (Platform.OS === 'android') {
directory = FileSystem.cacheDirectory;
} else if (Platform.OS === 'ios') {
directory = FileSystem.documentDirectory;
}
const fileName = 'test.pdf';
const filePath = `${directory}${fileName}`;
// Download the file directly without sharing
await FileSystem.writeAsStringAsync(filePath, base64String, {
encoding: FileSystem.EncodingType.Base64,
});
Alert.alert(
'PDF Downloaded',
'The PDF has been downloaded to your device.',
[{ text: 'OK', onPress: () => {} }]
);
} catch (error) {
console.error('Error downloading PDF: ', error);
}
};
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button title="Download PDF" onPress={handleDownload} />
</View>
);
};
export default DownloadPDFButton;
</code>
<code>import React from 'react'; import { View, Button, Alert, Platform } from 'react-native'; import * as FileSystem from 'expo-file-system'; import AsyncStorage from '@react-native-async-storage/async-storage'; import axios from 'axios'; import { encode } from 'base-64'; const getAuthHeaders = async () => { const accessToken = await AsyncStorage.getItem('accessToken'); const tokenType = await AsyncStorage.getItem('tokenType'); const client = await AsyncStorage.getItem('client'); const expiry = await AsyncStorage.getItem('expiry'); const uid = await AsyncStorage.getItem('uid'); return { 'access-token': accessToken, 'token-type': tokenType, 'client': client, 'expiry': expiry, 'uid': uid, }; }; const DownloadPDFButton = () => { const handleDownload = async () => { try { const headers = await getAuthHeaders(); const response = await axios.get('http://192.168.0.102:3000/download_test', { headers: headers, responseType: 'arraybuffer', }); const arrayBuffer = response.data; const base64String = encode(arrayBuffer); // Determine the directory based on the platform let directory; if (Platform.OS === 'android') { directory = FileSystem.cacheDirectory; } else if (Platform.OS === 'ios') { directory = FileSystem.documentDirectory; } const fileName = 'test.pdf'; const filePath = `${directory}${fileName}`; // Download the file directly without sharing await FileSystem.writeAsStringAsync(filePath, base64String, { encoding: FileSystem.EncodingType.Base64, }); Alert.alert( 'PDF Downloaded', 'The PDF has been downloaded to your device.', [{ text: 'OK', onPress: () => {} }] ); } catch (error) { console.error('Error downloading PDF: ', error); } }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Button title="Download PDF" onPress={handleDownload} /> </View> ); }; export default DownloadPDFButton; </code>
import React from 'react';
import { View, Button, Alert, Platform } from 'react-native';
import * as FileSystem from 'expo-file-system';
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
import { encode } from 'base-64';


const getAuthHeaders = async () => {
  const accessToken = await AsyncStorage.getItem('accessToken');
  const tokenType = await AsyncStorage.getItem('tokenType');
  const client = await AsyncStorage.getItem('client');
  const expiry = await AsyncStorage.getItem('expiry');
  const uid = await AsyncStorage.getItem('uid');

  return {
    'access-token': accessToken,
    'token-type': tokenType,
    'client': client,
    'expiry': expiry,
    'uid': uid,
  };
};

const DownloadPDFButton = () => {
  const handleDownload = async () => {
    try {
      const headers = await getAuthHeaders();
      const response = await axios.get('http://192.168.0.102:3000/download_test', {
        headers: headers,
        responseType: 'arraybuffer',
      });

      const arrayBuffer = response.data;
      const base64String = encode(arrayBuffer);

      // Determine the directory based on the platform
      let directory;
      if (Platform.OS === 'android') {
        directory = FileSystem.cacheDirectory;
      } else if (Platform.OS === 'ios') {
        directory = FileSystem.documentDirectory;
      }

      const fileName = 'test.pdf';
      const filePath = `${directory}${fileName}`;

      // Download the file directly without sharing
      await FileSystem.writeAsStringAsync(filePath, base64String, {
        encoding: FileSystem.EncodingType.Base64,
      });

      Alert.alert(
        'PDF Downloaded',
        'The PDF has been downloaded to your device.',
        [{ text: 'OK', onPress: () => {} }]
      );
    } catch (error) {
      console.error('Error downloading PDF: ', error);
    }
  };

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Download PDF" onPress={handleDownload} />
    </View>
  );
};

export default DownloadPDFButton;

Here are the output:
Rails terminal:
enter image description here
Rails tmp folder:
enter image description here
React native Expo go App:
enter image description here

I am expecting a Download prompt in android emulator/phone.

New contributor

user24720087 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật