Skip to content

Commit e37ef43

Browse files
committed
update
1 parent 8e3070f commit e37ef43

4 files changed

Lines changed: 209 additions & 3 deletions

File tree

apps/mobile/web/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
<!-- Capacitor -->
1111
<script type="module" src="/capacitor.js"></script>
1212
<script src="https://unpkg.com/tesseract.js@5/dist/tesseract.min.js"></script>
13+
<script type="module">
14+
// Import PDF.js as ES module and expose globally
15+
import * as pdfjs from 'https://unpkg.com/pdfjs-dist@5.4.149/build/pdf.min.mjs';
16+
17+
// Set worker source
18+
pdfjs.GlobalWorkerOptions.workerSrc = 'https://unpkg.com/pdfjs-dist@5.4.149/build/pdf.worker.min.mjs';
19+
20+
// Expose to global scope
21+
window.pdfjsLib = pdfjs;
22+
23+
console.log('Mobile: PDF.js loaded successfully', pdfjs.version);
24+
</script>
1325
<script type="module" src="/main.js"></script>
1426

1527
<style>

apps/mobile/web/main.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,85 @@ function detectToken(token) {
4949
return null;
5050
}
5151

52+
// PDF analysis using pdfjs-dist
53+
async function analyzePdfWithPdfjs(file) {
54+
try {
55+
// Check if pdfjsLib is available
56+
if (typeof pdfjsLib === 'undefined') {
57+
console.warn('Mobile: pdfjs-dist not available, falling back to empty result');
58+
return { detections: [], pages: 1 };
59+
}
60+
61+
// Convert file to array buffer
62+
const arrayBuffer = await file.arrayBuffer();
63+
const uint8Array = new Uint8Array(arrayBuffer);
64+
65+
console.log('Mobile: Loading PDF with pdfjs-dist...');
66+
67+
// Load PDF document
68+
const loadingTask = pdfjsLib.getDocument({ data: uint8Array });
69+
const pdf = await loadingTask.promise;
70+
71+
console.log(`Mobile: PDF loaded, ${pdf.numPages} pages`);
72+
73+
const detections = [];
74+
75+
// Process each page
76+
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
77+
const page = await pdf.getPage(pageNum);
78+
const textContent = await page.getTextContent();
79+
const viewport = page.getViewport({ scale: 1.0 });
80+
81+
console.log(`Mobile: Processing page ${pageNum}, found ${textContent.items.length} text items`);
82+
83+
// Extract text items with positions
84+
for (const item of textContent.items) {
85+
if (!item.str || !item.str.trim()) continue;
86+
87+
const text = item.str.trim();
88+
const match = detectToken(text);
89+
90+
if (match) {
91+
const { kind, reason } = match;
92+
93+
// Calculate normalized bounding box
94+
const transform = item.transform;
95+
const x = transform[4];
96+
const y = transform[5];
97+
const width = item.width || 50; // fallback width
98+
const height = item.height || 12; // fallback height
99+
100+
const box = {
101+
x: x / viewport.width,
102+
y: 1 - ((y + height) / viewport.height), // PDF coordinates are bottom-up
103+
w: width / viewport.width,
104+
h: height / viewport.height,
105+
page: pageNum - 1
106+
};
107+
108+
detections.push({
109+
id: `mobile-pdf-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
110+
kind: kind,
111+
box: box,
112+
confidence: 0.9,
113+
reason: reason,
114+
preview: text
115+
});
116+
117+
console.log(`Mobile: Detected ${kind}: "${text}" on page ${pageNum}`);
118+
}
119+
}
120+
}
121+
122+
console.log(`Mobile: PDF analysis complete, found ${detections.length} sensitive detections`);
123+
return { detections, pages: pdf.numPages };
124+
125+
} catch (error) {
126+
console.error('Mobile: PDF analysis failed:', error);
127+
return { detections: [], pages: 1 };
128+
}
129+
}
130+
52131
// Initialize the application modules
53132
async function initializeModules() {
54133
try {
@@ -61,9 +140,9 @@ async function initializeModules() {
61140
console.log('Mobile: Analyzing document with Tesseract OCR:', file.name);
62141

63142
if (!file.type.startsWith('image/')) {
64-
// For PDFs, return empty detections for now (could be implemented later)
65-
console.log('Mobile: PDF analysis not implemented yet');
66-
return { detections: [], pages: 1 };
143+
// For PDFs, use pdfjs-dist for text extraction
144+
console.log('Mobile: Analyzing PDF with pdfjs-dist:', file.name);
145+
return await analyzePdfWithPdfjs(file);
67146
}
68147

69148
try {

test-pdf-analysis.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env node
2+
3+
// Test script to verify PDF analysis functionality on both platforms
4+
console.log('🔍 Testing PDF Analysis Cross-Platform\n');
5+
6+
const fs = require('fs');
7+
const path = require('path');
8+
9+
// Create a test PDF file with text containing sensitive information
10+
const testPdfContent = `%PDF-1.3
11+
1 0 obj
12+
<< /Type /Catalog /Pages 2 0 R >>
13+
endobj
14+
15+
2 0 obj
16+
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
17+
endobj
18+
19+
3 0 obj
20+
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>
21+
endobj
22+
23+
4 0 obj
24+
<< /Length 200 >>
25+
stream
26+
BT
27+
/F1 12 Tf
28+
100 700 Td
29+
(Personal Information) Tj
30+
0 -20 Td
31+
(Email: john.doe@company.com) Tj
32+
0 -20 Td
33+
(Phone: (555) 123-4567) Tj
34+
0 -20 Td
35+
(SSN: 123-45-6789) Tj
36+
0 -20 Td
37+
(Credit Card: 4532-0151-1283-0366) Tj
38+
ET
39+
endstream
40+
endobj
41+
42+
5 0 obj
43+
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
44+
endobj
45+
46+
xref
47+
0 6
48+
0000000000 65535 f
49+
0000000009 00000 n
50+
0000000058 00000 n
51+
0000000115 00000 n
52+
0000000251 00000 n
53+
0000000502 00000 n
54+
trailer
55+
<< /Size 6 /Root 1 0 R >>
56+
startxref
57+
569
58+
%%EOF`;
59+
60+
const testDir = path.join(__dirname, 'test-samples');
61+
if (!fs.existsSync(testDir)) {
62+
fs.mkdirSync(testDir, { recursive: true });
63+
}
64+
65+
const testPdfPath = path.join(testDir, 'sensitive-document.pdf');
66+
fs.writeFileSync(testPdfPath, testPdfContent);
67+
68+
console.log('✅ Created test PDF with sensitive content:');
69+
console.log(` Path: ${testPdfPath}`);
70+
console.log(' Content: Email, Phone, SSN, Credit Card');
71+
72+
console.log('\n📋 Testing Instructions:');
73+
console.log('');
74+
console.log('1. 🌐 Web App (http://localhost:3000):');
75+
console.log(' • Open the web app in your browser');
76+
console.log(' • Upload the test PDF file');
77+
console.log(' • Verify detection of: EMAIL, PHONE, SSN, PAN');
78+
console.log(' • Check console logs for "Analyzing PDF with pdfjs-dist"');
79+
console.log('');
80+
81+
console.log('2. 📱 Mobile App (http://localhost:8081):');
82+
console.log(' • Open the mobile app in your browser');
83+
console.log(' • Upload the same test PDF file');
84+
console.log(' • Verify detection of: EMAIL, PHONE, SSN, PAN');
85+
console.log(' • Check console logs for "Mobile: Analyzing PDF with pdfjs-dist"');
86+
console.log(' • Should NO LONGER see "PDF analysis not implemented yet"');
87+
console.log('');
88+
89+
console.log('3. 🔄 Cross-Platform Comparison:');
90+
console.log(' • Both platforms should detect the same sensitive items');
91+
console.log(' • Detection counts should be identical');
92+
console.log(' • Coordinate systems may differ but detections should match');
93+
console.log('');
94+
95+
console.log('4. ⚡ Performance Test:');
96+
console.log(' • PDF analysis should complete within 5-10 seconds');
97+
console.log(' • No JavaScript errors in console');
98+
console.log(' • Progress indicators should show during processing');
99+
console.log('');
100+
101+
console.log('✅ Test PDF created successfully!');
102+
console.log('📝 File size:', fs.statSync(testPdfPath).size, 'bytes');
103+
console.log('');
104+
console.log('🚀 Ready for Testing:');
105+
console.log('🔗 Web App: http://localhost:3000');
106+
console.log('🔗 Mobile App: http://localhost:8081 (FIXED - now serving mobile interface)');
107+
console.log('');
108+
console.log('📁 Test PDF Location:');
109+
console.log(` ${testPdfPath}`);
110+
console.log('');
111+
console.log('🔍 Expected Detections:');
112+
console.log(' • EMAIL: john.doe@company.com');
113+
console.log(' • PHONE: (555) 123-4567');
114+
console.log(' • SSN: 123-45-6789');
115+
console.log(' • PAN: 4532-0151-1283-0366');
757 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)