From aa843a122944577d7ff999d0b8ea7639444d1b07 Mon Sep 17 00:00:00 2001 From: "adrian.crainic@bgasoft.com" Date: Wed, 25 Nov 2026 21:44:56 +0200 Subject: [PATCH] Read multi-page PDF reports and dates without a time A template field can be limited to page 1 (`first_page_only`), so a footer that repeats a label with a different value on later pages no longer makes the report invalid. The newborn screening preset reads `dob` on page 1 only, accepts `dob` and `collected` with or without a time, and writes PID-7, OBR-7 and SPM-17 to the minute or as a date. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 18 +++- .../2026-11-25-pdf-report-pages-and-dates.md | 20 ++++ console/src/ipc/generated/PdfField.ts | 4 + crates/packie-config/profiles/profiles.json | 102 ++++++++++++++---- crates/packie-config/src/protocol/pdf.rs | 3 + crates/packie-config/tests/profiles.rs | 11 ++ crates/packie-engine/src/pdf.rs | 1 + crates/packie-pdf/src/lib.rs | 1 + crates/packie-pdf/src/template.rs | 7 +- crates/packie-pdf/tests/conformance.rs | 33 ++++++ crates/packie-pdf/tests/properties.rs | 1 + docs/spec/configuration.md | 1 + docs/spec/pdf-reports.md | 9 +- 13 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 changelog.d/2026-11-25-pdf-report-pages-and-dates.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 745c25c..f619b89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ What changed in OpenBGA Interface Engine, grouped by release with unreleased changes on top, then by impact. Types follow [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). -## Unreleased (2026-11-12 to 2026-11-24) +## Unreleased (2026-11-12 to 2026-11-25) ### High impact @@ -173,6 +173,22 @@ top, then by impact. Types follow [Keep a Changelog](https://keepachangelog.com/ ### Medium impact +- **Fixed:** Multi-page newborn screening reports and dates without a time are read (2026-11-25) + + The newborn screening report preset now reads reports that run to two or more pages, where + the footer repeats the date of birth without its time, and reports that print the date of + birth or the collection date without a time. Such reports were refused as not matching the + template; now the HL7 message carries the date alone when the report prints no time. + + Technical notes: + + New PDF template field setting `first_page_only` (default `false`): the field is read on + page 1 only. Profile `pdf_newborn_screening_report`: `dob` has `first_page_only: true`; + `dob` and `collected` are `type: date` with formats `M/d/yyyy HH:mm` and `M/d/yyyy`. + Mapping `pdf_newborn_screening_to_hl7_oru`: PID-7, OBR-7 and SPM-17 are written as + `yyyyMMddHHmm` when the value has a time, otherwise `yyyyMMdd`. Channels created from the + earlier presets keep their settings; apply the presets again or make the same edits. + - **Changed:** PDF reports are filed by date inside their outcome folders (2026-11-24) When a PDF report channel moves an incoming PDF after reading it, the engine now places the diff --git a/changelog.d/2026-11-25-pdf-report-pages-and-dates.md b/changelog.d/2026-11-25-pdf-report-pages-and-dates.md new file mode 100644 index 0000000..e472236 --- /dev/null +++ b/changelog.d/2026-11-25-pdf-report-pages-and-dates.md @@ -0,0 +1,20 @@ +--- +date: 2026-11-25 +type: fixed +impact: medium +title: Multi-page newborn screening reports and dates without a time are read +area: Channels +--- +The newborn screening report preset now reads reports that run to two or more pages, where +the footer repeats the date of birth without its time, and reports that print the date of +birth or the collection date without a time. Such reports were refused as not matching the +template; now the HL7 message carries the date alone when the report prints no time. + +## Technical notes + +New PDF template field setting `first_page_only` (default `false`): the field is read on +page 1 only. Profile `pdf_newborn_screening_report`: `dob` has `first_page_only: true`; +`dob` and `collected` are `type: date` with formats `M/d/yyyy HH:mm` and `M/d/yyyy`. +Mapping `pdf_newborn_screening_to_hl7_oru`: PID-7, OBR-7 and SPM-17 are written as +`yyyyMMddHHmm` when the value has a time, otherwise `yyyyMMdd`. Channels created from the +earlier presets keep their settings; apply the presets again or make the same edits. diff --git a/console/src/ipc/generated/PdfField.ts b/console/src/ipc/generated/PdfField.ts index 7eabfc0..0f82e09 100644 --- a/console/src/ipc/generated/PdfField.ts +++ b/console/src/ipc/generated/PdfField.ts @@ -24,6 +24,10 @@ position: FieldPosition, * Which match of the label on a page: 0 the first, −1 the last; −1 to 20. Default 0. */ occurrence: number, +/** + * Read on page 1 only; the label on later pages is ignored. Default `false`. + */ +first_page_only: boolean, /** * `below` only: the block line, 1–20. Default 1. */ diff --git a/crates/packie-config/profiles/profiles.json b/crates/packie-config/profiles/profiles.json index e3d547e..00d6190 100644 --- a/crates/packie-config/profiles/profiles.json +++ b/crates/packie-config/profiles/profiles.json @@ -117,8 +117,12 @@ { "name": "dob", "label": "DOB:", - "type": "datetime", - "format": "M/d/yyyy HH:mm" + "first_page_only": true, + "type": "date", + "format": [ + "M/d/yyyy HH:mm", + "M/d/yyyy" + ] }, { "name": "birth_order", @@ -163,8 +167,11 @@ { "name": "collected", "label": "Date Collected:", - "type": "datetime", - "format": "M/d/yyyy HH:mm" + "type": "date", + "format": [ + "M/d/yyyy HH:mm", + "M/d/yyyy" + ] }, { "name": "sample_id", @@ -1416,11 +1423,30 @@ ] }, { - "op": "datetime", - "from": "field.dob", - "to": "PID.7", - "input": "M/d/yyyy HH:mm", - "output": "yyyyMMddHHmm" + "op": "when", + "condition": { + "path": "field.dob", + "op": "matches", + "value": ":" + }, + "then": [ + { + "op": "datetime", + "from": "field.dob", + "to": "PID.7", + "input": "M/d/yyyy HH:mm", + "output": "yyyyMMddHHmm" + } + ], + "else": [ + { + "op": "datetime", + "from": "field.dob", + "to": "PID.7", + "input": "M/d/yyyy", + "output": "yyyyMMdd" + } + ] }, { "op": "lookup", @@ -1650,11 +1676,30 @@ "to": "OBR.4.3" }, { - "op": "datetime", - "from": "field.collected", - "to": "OBR.7", - "input": "M/d/yyyy HH:mm", - "output": "yyyyMMddHHmm" + "op": "when", + "condition": { + "path": "field.collected", + "op": "matches", + "value": ":" + }, + "then": [ + { + "op": "datetime", + "from": "field.collected", + "to": "OBR.7", + "input": "M/d/yyyy HH:mm", + "output": "yyyyMMddHHmm" + } + ], + "else": [ + { + "op": "datetime", + "from": "field.collected", + "to": "OBR.7", + "input": "M/d/yyyy", + "output": "yyyyMMdd" + } + ] }, { "op": "datetime", @@ -1779,11 +1824,30 @@ "to": "SPM.2" }, { - "op": "datetime", - "from": "field.collected", - "to": "SPM.17", - "input": "M/d/yyyy HH:mm", - "output": "yyyyMMddHHmm" + "op": "when", + "condition": { + "path": "field.collected", + "op": "matches", + "value": ":" + }, + "then": [ + { + "op": "datetime", + "from": "field.collected", + "to": "SPM.17", + "input": "M/d/yyyy HH:mm", + "output": "yyyyMMddHHmm" + } + ], + "else": [ + { + "op": "datetime", + "from": "field.collected", + "to": "SPM.17", + "input": "M/d/yyyy", + "output": "yyyyMMdd" + } + ] }, { "op": "datetime", diff --git a/crates/packie-config/src/protocol/pdf.rs b/crates/packie-config/src/protocol/pdf.rs index 4ff0596..91f31f5 100644 --- a/crates/packie-config/src/protocol/pdf.rs +++ b/crates/packie-config/src/protocol/pdf.rs @@ -54,6 +54,9 @@ pub struct PdfField { /// Which match of the label on a page: 0 the first, −1 the last; −1 to 20. Default 0. #[serde(default)] pub occurrence: i32, + /// Read on page 1 only; the label on later pages is ignored. Default `false`. + #[serde(default)] + pub first_page_only: bool, /// `below` only: the block line, 1–20. Default 1. #[serde(default)] pub line: Option, diff --git a/crates/packie-config/tests/profiles.rs b/crates/packie-config/tests/profiles.rs index 154ffa5..e8e38ea 100644 --- a/crates/packie-config/tests/profiles.rs +++ b/crates/packie-config/tests/profiles.rs @@ -470,3 +470,14 @@ fn an_oml_specimen_id_with_a_namespace_is_sent_as_the_bare_identifier() { let sent = run_on(&preset, &input).expect("accepted"); assert!(sent.contains("O|1|SPEC1234|"), "{sent:?}"); } + +#[test] +fn a_newborn_report_without_times_sends_dates() { + let preset = preset("pdf_newborn_screening_to_hl7_oru"); + let input = changed(&preset.sample.input, "dob: 1/2/2026 08:05", "dob: 1/2/2026"); + let input = changed(&input, "collected: 1/3/2026 10:00", "collected: 1/3/2026"); + let sent = run_on(&preset, &input).expect("accepted"); + assert!(sent.contains("|DOE^TEST BABY||20260102|F|"), "{sent:?}"); + assert!(sent.contains("|INITIAL^Initial^L|||20260103|"), "{sent:?}"); + assert!(sent.ends_with("|20260103|20260104\r"), "{sent:?}"); +} diff --git a/crates/packie-engine/src/pdf.rs b/crates/packie-engine/src/pdf.rs index 8ec40e3..4ba8a7e 100644 --- a/crates/packie-engine/src/pdf.rs +++ b/crates/packie-engine/src/pdf.rs @@ -150,6 +150,7 @@ fn field(field: &PdfField) -> packie_pdf::Field { labels: field.label.to_vec(), position, occurrence: field.occurrence, + first_page_only: field.first_page_only, required: field.required, } } diff --git a/crates/packie-pdf/src/lib.rs b/crates/packie-pdf/src/lib.rs index a91cd8d..f6d82c0 100644 --- a/crates/packie-pdf/src/lib.rs +++ b/crates/packie-pdf/src/lib.rs @@ -30,6 +30,7 @@ //! labels: vec!["Lab Number:".into()], //! position: Position::Right, //! occurrence: 0, +//! first_page_only: false, //! required: true, //! }], //! ..Template::default() diff --git a/crates/packie-pdf/src/template.rs b/crates/packie-pdf/src/template.rs index 6e54152..13b91e0 100644 --- a/crates/packie-pdf/src/template.rs +++ b/crates/packie-pdf/src/template.rs @@ -35,6 +35,8 @@ pub struct Field { pub position: Position, /// Which match of the label on a page: 0 the first, 1 the second, −1 the last. pub occurrence: i32, + /// Read on page 1 only; the label on later pages (a footer repeating it) is ignored. + pub first_page_only: bool, /// The label must be on page 1 with a value. pub required: bool, } @@ -125,7 +127,10 @@ impl Template { if number == 0 { page.check_label_area(&hits, &ends)?; } - for (index, value) in page.values(&hits, &self.fields) { + let fields = page.values(&hits, &self.fields).into_iter(); + for (index, value) in + fields.filter(|(i, _)| number == 0 || !self.fields[*i].first_page_only) + { found[index].push((number, value)); } } diff --git a/crates/packie-pdf/tests/conformance.rs b/crates/packie-pdf/tests/conformance.rs index eb299d9..2952fd6 100644 --- a/crates/packie-pdf/tests/conformance.rs +++ b/crates/packie-pdf/tests/conformance.rs @@ -129,6 +129,7 @@ fn right(name: &str, label: &str) -> Field { labels: vec![label.into()], position: Position::Right, occurrence: 0, + first_page_only: false, required: false, } } @@ -321,6 +322,38 @@ fn conflicting_pages_are_refused() { ); } +#[test] +fn a_first_page_field_ignores_its_label_on_later_pages() { + // Page 2 carries only the footer, which repeats the birth date without its time. + let footer = |page: &str| Run { + size: 8.0, + ..run(400.0, 760.0, &format!("Page {page} of 2 DOB: 1/2/2026")) + }; + let mut first = report_page(); + first.runs.retain(|r| !r.text.starts_with("Page ")); + first.runs.push(footer("1")); + let bytes = pdf(&[first, Page::new(vec![footer("2")])]); + let (reason, detail) = reason(read(&bytes)); + assert_eq!(reason, Reason::TemplateMismatch); + assert_eq!( + detail, + "field \"dob\" has different values on pages 1 and 2" + ); + + let mut template = template(); + let dob = template + .fields + .iter_mut() + .find(|f| f.name == "dob") + .unwrap(); + dob.first_page_only = true; + let report = template + .read(&extract(&bytes, &Limits::default()).unwrap()) + .unwrap(); + assert_eq!(report.pages, 2); + assert_values(&report); +} + #[test] fn unknown_label_in_the_label_area_is_refused() { let mut page = report_page(); diff --git a/crates/packie-pdf/tests/properties.rs b/crates/packie-pdf/tests/properties.rs index 5dc232d..b100b48 100644 --- a/crates/packie-pdf/tests/properties.rs +++ b/crates/packie-pdf/tests/properties.rs @@ -41,6 +41,7 @@ fn template() -> Template { labels: vec![label.into()], position, occurrence: 0, + first_page_only: false, required: false, }; Template { diff --git a/docs/spec/configuration.md b/docs/spec/configuration.md index 9ad481f..70ef295 100644 --- a/docs/spec/configuration.md +++ b/docs/spec/configuration.md @@ -503,6 +503,7 @@ Source only. Behavior, matching rules and the model are in [pdf-reports.md](pdf- | `label` | string or array | none | 1–100 characters, or an array of 1–8 such strings. | | `position` | string | `right` | `right` or `below`. | | `occurrence` | integer | `0` | −1 to 20. | +| `first_page_only` | boolean | `false` | | | `line` | integer | `1` | 1–20; only with `below`. | | `phrase` | integer | `0` | −1 to 20 (−1 the last phrase of the line); only with `below`. | | `indent` | number | `4` | 0–600 points, at most 2 decimal places; only with `below`. | diff --git a/docs/spec/pdf-reports.md b/docs/spec/pdf-reports.md index ecbd97f..012f4bd 100644 --- a/docs/spec/pdf-reports.md +++ b/docs/spec/pdf-reports.md @@ -86,6 +86,7 @@ that follow them on the same line (a footer written as one run of text, | `label` | One label, or 1–8 alternative labels; the first that is found on the page is used. | | `position` | `right` (default): the value is on the label's line. `below`: the value is in the block of lines under the label. | | `occurrence` | Which match of the label on a page: 0 the first in reading order (default), 1 the second, −1 the last. | +| `first_page_only` | `true`: the field is read on page 1 only and its label on later pages is ignored, for a label that a footer repeats with a different value (a grid's `DOB: 1/2/2026 08:05` and a footer's `DOB: 1/2/2026`). Default `false`. | | `line`, `phrase`, `indent` | `below` only, see below. | | `required` | `true`: the label is found on page 1 and its value is not empty; otherwise `template_mismatch`. `false` (default): a missing label or value is an empty field. | | `type`, `format` | As delimited columns: `string` (default), `integer`, `decimal`, `date`, `datetime`; `format` is required for dates and may be an array of 1–4 formats, the first that matches the whole value wins. A non-empty value that matches no format, or is not an integer or decimal, is `field_invalid`. Values stay text in the model. | @@ -107,8 +108,8 @@ default 1); a missing line or phrase is an empty value. The last phrase finds a the right of a row whatever precedes it, such as a facility code after a name the report may leave blank (give `indent` enough room for a row that starts at the code). -**Pages.** A field is evaluated on every page where its label occurs, with `occurrence` -counted on that page. Every page that yields a non-empty value must yield the same value; +**Pages.** A field is evaluated on every page where its label occurs (page 1 only with +`first_page_only`), with `occurrence` counted on that page. Every page that yields a non-empty value must yield the same value; otherwise the report is `invalid` (`template_mismatch`, the field and pages named). A file that holds several reports is refused this way, because their patient fields differ. @@ -191,8 +192,8 @@ own reports before clinical use. | Profile | Content | | --- | --- | -| `pdf_newborn_screening_report` (connector, source) | Every label of the patient, specimen and collection grid as a `right` field, none required (`patient_name`, `gender`, `mother_name`, `dob`, `birth_order`, `mother_dob`, `race`, `birth_weight`, `mother_phone`, `medical_record`, `gestational_age`, `meconium_ileus`, `kit_number`, `nicu`, `collected`, `sample_id` = `Lab Number:`, `age_at_collection`, `received`, `specimen_type`, `transfusion_date`, `reported`, `food_source`, `collection_weight`, `printed`); the footer's `last_name` (`occurrence` −1), `accession_number` and `Reported Date:`; the report recipient's first line as a `below` field (`recipient`, `indent` 60; the rows after it vary between reports and are not read); the birth hospital, collection facility and physician or clinic (`Physician/Clinic`, printed without a colon) as `below` fields, each with its facility code as the last phrase of the row (`phrase` −1, `indent` 170: `birth_hospital_code`, `collection_facility_code`, `physician_facility_code`), which is found also when the report leaves the facility's name blank. `label_area_end`: `Results`. | -| `pdf_newborn_screening_to_hl7_oru` (mapping) | `ORU^R01^ORU_R01` for an `hl7_file` destination; the Lab Number identifies the patient and the order. `PID-2`, `PID-3` (`^^^^PI`), `PID-18` = `sample_id`; `PID-5` family name `last_name`, given names `patient_name` without it (`strip_suffix`); `PID-7` `dob`; `PID-8` through the `sex` table; `PID-10` through the `race` table; `PID-16` `S`. `NK1` when the report names the mother: `NK1-2` `mother_name`, `NK1-3` `MTH`, `NK1-5` `mother_phone`, `NK1-7` `N`. `PV1-2` `N`; `PV1-3.4` `birth_hospital_code`; `PV1-4` `N`; `PV1-7` and `PV1-8` the report recipient (the ordering provider); `PV1-19` `sample_id`; `PV1-44` `received`. `ORC-1` `RE`, `ORC-3` `sample_id`, `ORC-21` physician or clinic with its facility code, `ORC-23` the laboratory's phone. `OBR-3` `sample_id`; `OBR-4` the panel from `specimen_type`; `OBR-7` `collected`; `OBR-14` `received`; `OBR-16` the report recipient (name without NPI); `OBR-17` the laboratory's phone; `OBR-22` `reported`; `OBR-25` `F`. One `OBX` of type `ED`: `OBX-3` the panel, `OBX-5` `^AP^PDF^Base64^` with `report.file_base64`, `OBX-11` `F`, `OBX-14` `reported`. `SPM-2` `kit_number`, `SPM-17` `collected`, `SPM-18` `received`. The laboratory's phone, the assigning authority and the panel's coding system are constants a deployment fills in; MSH-3 to MSH-6 come from the destination settings. A field the report does not carry leaves its HL7 field empty: no rule is `required` and none writes the HL7 null `""`, which a receiver would read as "delete the stored value". | +| `pdf_newborn_screening_report` (connector, source) | Every label of the patient, specimen and collection grid as a `right` field, none required (`patient_name`, `gender`, `mother_name`, `dob`, `birth_order`, `mother_dob`, `race`, `birth_weight`, `mother_phone`, `medical_record`, `gestational_age`, `meconium_ileus`, `kit_number`, `nicu`, `collected`, `sample_id` = `Lab Number:`, `age_at_collection`, `received`, `specimen_type`, `transfusion_date`, `reported`, `food_source`, `collection_weight`, `printed`); `dob` and `collected` are dates printed with or without a time (`M/d/yyyy HH:mm` or `M/d/yyyy`), and `dob` is read on page 1 only (`first_page_only`), because the footer repeats `DOB:` without the time on every page; the footer's `last_name` (`occurrence` −1), `accession_number` and `Reported Date:`; the report recipient's first line as a `below` field (`recipient`, `indent` 60; the rows after it vary between reports and are not read); the birth hospital, collection facility and physician or clinic (`Physician/Clinic`, printed without a colon) as `below` fields, each with its facility code as the last phrase of the row (`phrase` −1, `indent` 170: `birth_hospital_code`, `collection_facility_code`, `physician_facility_code`), which is found also when the report leaves the facility's name blank. `label_area_end`: `Results`. | +| `pdf_newborn_screening_to_hl7_oru` (mapping) | `ORU^R01^ORU_R01` for an `hl7_file` destination; the Lab Number identifies the patient and the order. `PID-2`, `PID-3` (`^^^^PI`), `PID-18` = `sample_id`; `PID-5` family name `last_name`, given names `patient_name` without it (`strip_suffix`); `PID-7` `dob` (to the minute when the report prints a time, otherwise the date; likewise `OBR-7` and `SPM-17`); `PID-8` through the `sex` table; `PID-10` through the `race` table; `PID-16` `S`. `NK1` when the report names the mother: `NK1-2` `mother_name`, `NK1-3` `MTH`, `NK1-5` `mother_phone`, `NK1-7` `N`. `PV1-2` `N`; `PV1-3.4` `birth_hospital_code`; `PV1-4` `N`; `PV1-7` and `PV1-8` the report recipient (the ordering provider); `PV1-19` `sample_id`; `PV1-44` `received`. `ORC-1` `RE`, `ORC-3` `sample_id`, `ORC-21` physician or clinic with its facility code, `ORC-23` the laboratory's phone. `OBR-3` `sample_id`; `OBR-4` the panel from `specimen_type`; `OBR-7` `collected`; `OBR-14` `received`; `OBR-16` the report recipient (name without NPI); `OBR-17` the laboratory's phone; `OBR-22` `reported`; `OBR-25` `F`. One `OBX` of type `ED`: `OBX-3` the panel, `OBX-5` `^AP^PDF^Base64^` with `report.file_base64`, `OBX-11` `F`, `OBX-14` `reported`. `SPM-2` `kit_number`, `SPM-17` `collected`, `SPM-18` `received`. The laboratory's phone, the assigning authority and the panel's coding system are constants a deployment fills in; MSH-3 to MSH-6 come from the destination settings. A field the report does not carry leaves its HL7 field empty: no rule is `required` and none writes the HL7 null `""`, which a receiver would read as "delete the stored value". | A channel that converts only some hospitals' reports adds a source filter on the birth hospital's facility code, for example