From bad151e693026bd3a6563e9ea4714ad21ad377ce Mon Sep 17 00:00:00 2001 From: Rakshit Kumar Singh Date: Sat, 18 Apr 2026 14:52:34 +0530 Subject: [PATCH] fix sharing location error --- src/screens/ViewerScreen.tsx | 2 +- src/utils/generatePdf.ts | 56 +++++++++++++++++------------------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/screens/ViewerScreen.tsx b/src/screens/ViewerScreen.tsx index 54835a9..9da8f33 100644 --- a/src/screens/ViewerScreen.tsx +++ b/src/screens/ViewerScreen.tsx @@ -46,7 +46,7 @@ export default function ViewerScreen({document, onBack, onDelete, onRename}: Pro await Share.open({ title: label, type: 'application/pdf', - url: `file://${pdfPath}`, + url: pdfPath, // already normalised to file:// by generatePdf failOnCancel: false, }); } catch (err: any) { diff --git a/src/utils/generatePdf.ts b/src/utils/generatePdf.ts index c786de4..5780f61 100644 --- a/src/utils/generatePdf.ts +++ b/src/utils/generatePdf.ts @@ -2,19 +2,23 @@ import {generatePDF as rnGeneratePDF} from 'react-native-html-to-pdf'; import RNFS from 'react-native-fs'; import {ScannedPage} from '../types'; -const PDF_DIR = `${RNFS.DocumentDirectoryPath}/pdfs`; +// On Android, `directory` is a relative subdirectory name appended to +// getExternalFilesDir()/filesDir — NOT an absolute path. +const PDF_SUBDIR = 'pdfs'; async function pageToBase64(uri: string): Promise { const path = uri.startsWith('file://') ? uri.slice(7) : uri; return RNFS.readFile(path, 'base64'); } +function toFileUri(p: string): string { + return p.startsWith('file://') ? p : `file://${p}`; +} + export async function generatePdf( pages: ScannedPage[], name: string, ): Promise { - await RNFS.mkdir(PDF_DIR); - const imageHtml = await Promise.all( pages.map(async p => { const b64 = await pageToBase64(p.uri); @@ -22,39 +26,33 @@ export async function generatePdf( }), ); - const html = ` - - - - - - - ${imageHtml.join('\n')} - - `; + const html = ` + + + + + + ${imageHtml.join('\n')} +`; + + const fileName = name.replace(/[^a-z0-9_\-]/gi, '_'); const result = await rnGeneratePDF({ html, - fileName: name.replace(/[^a-z0-9_\-]/gi, '_'), - directory: PDF_DIR, + fileName, + directory: PDF_SUBDIR, base64: false, }); if (!result.filePath) { - throw new Error('PDF generation failed — no output path returned'); + throw new Error('PDF generation failed: library returned no file path'); } - return result.filePath; + return toFileUri(result.filePath); }