membership: refactor form filling, add smoke tests and debug-guard fallback; fix mappings
This commit is contained in:
77
scripts/inspect-forms.js
Normal file
77
scripts/inspect-forms.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { PDFDocument, StandardFonts } from 'pdf-lib'
|
||||
|
||||
async function inspect(pdfPath) {
|
||||
console.log('\n--- Inspecting', pdfPath)
|
||||
if (!fs.existsSync(pdfPath)) {
|
||||
console.log('MISSING:', pdfPath)
|
||||
return
|
||||
}
|
||||
const bytes = fs.readFileSync(pdfPath)
|
||||
const pdfDoc = await PDFDocument.load(bytes)
|
||||
let form = null
|
||||
try { form = pdfDoc.getForm() } catch (e) { form = null }
|
||||
if (!form) { console.log('No AcroForm found') ; return }
|
||||
const fields = form.getFields()
|
||||
console.log('Field count:', fields.length)
|
||||
for (const f of fields) {
|
||||
const name = f.getName()
|
||||
let value = null
|
||||
try {
|
||||
if (typeof f.getText === 'function') value = f.getText()
|
||||
else if (typeof f.isChecked === 'function') value = f.isChecked()
|
||||
else value = '(no getter)'
|
||||
} catch (e) {
|
||||
value = `(error reading: ${e.message})`
|
||||
}
|
||||
// widgets
|
||||
let widgetsInfo = []
|
||||
try {
|
||||
const acro = f.acroField
|
||||
const widgets = acro.getWidgets()
|
||||
for (const w of widgets) {
|
||||
try {
|
||||
const rect = w.getRectangle()
|
||||
// try to find page index by searching pages for an annotation with same ref
|
||||
let pageIndex = null
|
||||
try {
|
||||
const pages = pdfDoc.getPages()
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const page = pages[i]
|
||||
const annots = page.node.Annots ? page.node.Annots() : null
|
||||
// can't reliably map here; just record rect
|
||||
}
|
||||
} catch (e) {}
|
||||
widgetsInfo.push({ rect })
|
||||
} catch (e) {
|
||||
widgetsInfo.push({ error: e.message })
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
widgetsInfo = [`error widgets: ${e.message}`]
|
||||
}
|
||||
console.log(`- ${name}: value='${value}' widgets=${widgetsInfo.length}`)
|
||||
for (const wi of widgetsInfo) console.log(' ', JSON.stringify(wi))
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const repoRoot = process.cwd()
|
||||
const template = path.join(repoRoot, 'server', 'templates', 'mitgliedschaft-fillable.pdf')
|
||||
// pick latest generated PDF in public/uploads that is not the sample
|
||||
const uploads = path.join(repoRoot, 'public', 'uploads')
|
||||
let pdfFiles = []
|
||||
if (fs.existsSync(uploads)) {
|
||||
pdfFiles = fs.readdirSync(uploads).filter(f => f.toLowerCase().endsWith('.pdf'))
|
||||
.map(f => ({ f, mtime: fs.statSync(path.join(uploads, f)).mtimeMs }))
|
||||
.sort((a,b) => b.mtime - a.mtime)
|
||||
.map(x => x.f)
|
||||
}
|
||||
const apiPdf = pdfFiles.find(n => !n.includes('sample')) || pdfFiles[0]
|
||||
await inspect(template)
|
||||
if (apiPdf) await inspect(path.join(uploads, apiPdf))
|
||||
else console.log('No API-generated PDF found in public/uploads')
|
||||
}
|
||||
|
||||
main().catch(e => { console.error(e); process.exit(1) })
|
||||
Reference in New Issue
Block a user