fix sharing location error

This commit is contained in:
2026-04-18 14:52:34 +05:30
parent 5e5af1054a
commit bad151e693
2 changed files with 28 additions and 30 deletions

View File

@@ -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) {

View File

@@ -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<string> {
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<string> {
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 = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #fff; }
.page {
width: 100%;
page-break-after: always;
display: flex;
justify-content: center;
align-items: center;
}
.page:last-child { page-break-after: avoid; }
img { width: 100%; height: auto; display: block; }
</style>
</head>
<body>${imageHtml.join('\n')}</body>
</html>
`;
const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #fff; }
.page { width: 100%; page-break-after: always; }
.page:last-child { page-break-after: avoid; }
img { width: 100%; height: auto; display: block; }
</style>
</head>
<body>${imageHtml.join('\n')}</body>
</html>`;
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);
}