* Open `url` via PdfCacheRangeTransport and return form metadata. * Uses `disableAutoFetch` so PDFs without an AcroForm are probed with only * the trailer/xref/catalog (~5-25% of bytes); PDFs with forms still walk * every page via extractFormFieldInfo but those are typically small
( url: string, totalBytes: number, readPdfRange: PdfCache["readPdfRange"], )
| 1027 | * {@link PdfCacheRangeTransport.failed}) resolve to empty results. |
| 1028 | */ |
| 1029 | async function probeFormFields( |
| 1030 | url: string, |
| 1031 | totalBytes: number, |
| 1032 | readPdfRange: PdfCache["readPdfRange"], |
| 1033 | ): Promise<{ |
| 1034 | formSchema: Awaited<ReturnType<typeof extractFormSchema>>; |
| 1035 | fieldInfo: FormFieldInfo[]; |
| 1036 | }> { |
| 1037 | // Assigned sequentially below so a throw in extractFormFieldInfo (no per-page |
| 1038 | // guard, unlike extractFormSchema) doesn't discard an already-computed schema. |
| 1039 | let formSchema: Awaited<ReturnType<typeof extractFormSchema>> = null; |
| 1040 | let fieldInfo: FormFieldInfo[] = []; |
| 1041 | try { |
| 1042 | const transport = new PdfCacheRangeTransport(url, totalBytes, readPdfRange); |
| 1043 | const orFail = <T>(p: Promise<T>): Promise<T> => |
| 1044 | Promise.race([p, transport.failed]); |
| 1045 | const pdfDoc = await orFail( |
| 1046 | getDocument({ |
| 1047 | range: transport, |
| 1048 | length: totalBytes, |
| 1049 | disableAutoFetch: true, |
| 1050 | disableStream: true, |
| 1051 | rangeChunkSize: 64 * 1024, |
| 1052 | standardFontDataUrl: STANDARD_FONT_DATA_URL, |
| 1053 | StandardFontDataFactory: FetchStandardFontDataFactory, |
| 1054 | verbosity: VerbosityLevel.ERRORS, |
| 1055 | }).promise, |
| 1056 | ); |
| 1057 | try { |
| 1058 | const fieldObjects = (await orFail(pdfDoc.getFieldObjects())) as Record< |
| 1059 | string, |
| 1060 | PdfJsFieldObject[] |
| 1061 | > | null; |
| 1062 | if (fieldObjects && Object.keys(fieldObjects).length > 0) { |
| 1063 | formSchema = await orFail(extractFormSchema(pdfDoc, fieldObjects)); |
| 1064 | fieldInfo = await orFail(extractFormFieldInfo(pdfDoc)); |
| 1065 | } |
| 1066 | } finally { |
| 1067 | pdfDoc.destroy(); |
| 1068 | } |
| 1069 | } catch { |
| 1070 | // Non-fatal — return whatever was assigned before the throw. |
| 1071 | } |
| 1072 | return { formSchema, fieldInfo }; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * Extract detailed form field info (name, type, page, bounding box, label) |
no test coverage detected
searching dependent graphs…