Skip to content

Commit

Permalink
Merge pull request #470 from GSA-TTS/main
Browse files Browse the repository at this point in the history
Deploy to demo
  • Loading branch information
danielnaab authored Jan 29, 2025
2 parents 28ae2e1 + b2bfed0 commit 7c4d2e5
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 142 deletions.
40 changes: 20 additions & 20 deletions packages/forms/src/documents/pdf/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { PDFDocument, type PDFForm } from 'pdf-lib';
import {
PDFDocument,
PDFName,
createPDFAcroFields,
type PDFForm,
} from 'pdf-lib';

import { Result } from '@atj/common';
import { type FormOutput } from '../../index.js';
Expand Down Expand Up @@ -38,7 +43,6 @@ export const fillPDF = async (
setFormFieldData(form, value.type, name, value.value);
});
} catch (error: any) {
// console.log('fieldData is:', fieldData);
const fieldDataNames = Object.keys(fieldData); // names we got from API
const fields = form.getFields();
const fieldNames = fields.map(field => field.getName()); // fieldnames we ripped from the PDF
Expand All @@ -64,15 +68,6 @@ export const fillPDF = async (
.map(([name, sources]) => ({ name, sources }))
.sort((a, b) => a.name.localeCompare(b.name));

// Console log the resulting array
console.log('uniqueNamesArray:', uniqueNamesArray);

// fields.map(field => {
// console.log('field name is:', field.getName());
// });

// console.log('form.getFields() is:', form.getFields());
// console.log('pdf form is:', form);
if (error?.message) {
return {
success: false,
Expand Down Expand Up @@ -117,19 +112,24 @@ const setFormFieldData = (
const field = form.getDropdown(fieldName);
field.select(fieldValue);
} else if (fieldType === 'RadioGroup') {
// TODO: remove this when we have a better way to handle radio groups
// TODO: harmonize the option ids between pdf-lib and the API at ingestion time
try {
const field = form.getRadioGroup(fieldName);
field.select(fieldValue);
} catch (error: any) {
console.error(
`error setting radio field: ${fieldName}: ${error.message}`
);
const field = form.getCheckBox(fieldName);
if (fieldValue) {
field.check();
} else {
field.uncheck();
// This logic should work even if pdf-lib misidentifies the field type
// TODO: radioParent should contain the name, not the id
const [radioParent, radioChild] = fieldValue.split('.');
if (radioChild) {
// TODO: resolve import failure when spaces are present in name, id
const radioChildWithSpace = radioChild.replace('_', ' ');
const field = form.getField(fieldName);
const acroField = field.acroField;
acroField.dict.set(PDFName.of('V'), PDFName.of(radioChildWithSpace));
const kids = createPDFAcroFields(acroField.Kids()).map(_ => _[0]);
kids.forEach(kid => {
kid.dict.set(PDFName.of('AS'), PDFName.of(radioChildWithSpace));
});
}
}
} else if (fieldType === 'Paragraph' || fieldType === 'RichText') {
Expand Down
4 changes: 1 addition & 3 deletions packages/forms/src/documents/pdf/parsing-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const RadioGroupOption = z.object({
});

const RadioGroup = z.object({
// id: z.string(),
id: z.string(),
component_type: z.literal('radio_group'),
legend: z.string(),
options: RadioGroupOption.array(),
Expand Down Expand Up @@ -236,7 +236,6 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
pagePatterns[element.page] = (pagePatterns[element.page] || []).concat(
radioGroupPattern.id
);
/*
parsedPdf.outputs[radioGroupPattern.id] = {
type: 'RadioGroup',
name: element.id,
Expand All @@ -250,7 +249,6 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
value: '',
required: true,
};
*/
}
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
dist/
# generated types
.astro/

# database
# development database
main.db

# dependencies
Expand Down
Loading

0 comments on commit 7c4d2e5

Please sign in to comment.