Skip to content
Request a demo

Articles / HL7 integration

HL7 integration · 19/10/2026 · Marco Volpe

Base64 PDFs in HL7 v2 messages: one string, five decoders, different verdicts

A PDF report embedded in an OBX, with a few spaces in the Base64: some libraries throw it away, others accept even a truncated file without complaint. Why length does not tell you whether the attachment is complete, what MIME, RFC 4648 and the v2-to-FHIR guide say, and how to actually check.

This is the OBX segment of a synthetic lab report we put together as a test case. OBX-5 carries the report itself, a PDF embedded in the message and encoded in Base64. Shortened, it looks like this:

OBX|1|ED|11502-2^Laboratory report^LN||^AP^PDF^Base64^JVBERi0xLjQKJeLjz9MK…IFIgPj4K ZW5kb2JqCjIgMCBv…CnN0YXJ0eHJlZgo1MzgKJSVFT0Y =||||||F

OBX-2 is ED, encapsulated data, and the components of OBX-5 say this is a PDF in Base64. The Base64 text, though, contains six spaces at five points: some between one four-character group and the next, two inside a group, one right before the closing =. Strip them and the string decodes to a 752-byte PDF that opens with %PDF-1.4 and ends with %%EOF, byte-for-byte identical to the reference file. We checked it against the SHA-256 digest.

So the report is there. Whether it reaches the other end depends on a line of code nobody usually looks at: the function that decodes the Base64.

Three libraries, three verdicts

We fed the same string to the standard-library Base64 decoders of three languages that integration engines are commonly built on: Python 3.13, Java 21 and Node.js 22. Then we repeated the test with two variants of the same PDF: one without the trailing = padding, and one cut at about two thirds of its length, which is what happens when a message gets truncated somewhere along the way.

Decoder With spaces No trailing = Truncated
Python, strict mode rejected rejected rejected
Python, default mode intact PDF rejected rejected
Java, basic decoder rejected intact PDF 503 bytes, no %%EOF
Java, MIME decoder intact PDF intact PDF 503 bytes, no %%EOF
Node.js intact PDF intact PDF 503 bytes, no %%EOF

The two worst outcomes are exact opposites. A strict decoder throws away an intact report because of a space. A lenient one accepts a truncated PDF without a murmur: 503 bytes that start with the right header and stop halfway. Python does reject the truncated file, but for the wrong reason. It complains about padding, and with the same error it would also reject the intact PDF that arrived without its =.

What the rules say

The libraries follow two different rules, and both exist. The HL7 v2 table of encodings (table 0299, as published in HL7 Terminology) defines Base64 as the MIME encoding and points to RFC 1521. That RFC was replaced in 1996 by RFC 2045, which caps encoded lines at 76 characters and tells the decoder to ignore any character outside the Base64 alphabet, line breaks included. For a v2 message, then, a decoder that skips spaces is in line with the standard the table refers to.

RFC 4648, the general Base64 specification from 2006, says the opposite: an implementation must reject characters outside the alphabet and must include padding, unless the specification referring to it says otherwise. It names MIME as one that does. Strict decoders follow this one.

Base64 without its trailing = meets neither RFC, but it can still be decoded unambiguously when its length leaves a remainder of 2 or 3 after division by four: those are the two cases where RFC 4648 adds two = or one. Only a remainder of 1 cannot correspond to any data. Length tells you whether a string can be decoded, not whether the file inside it is complete. Our truncated PDF has a remainder of 3 and is perfectly valid Base64.

What happens in the FHIR conversion

The HL7 v2-to-FHIR guide turns an OBX of type ED with Base64 encoding into an attachment. In FHIR R4, Observation has no value of type Attachment, so the guide uses an extension borrowed from R5; there is also a map from OBX to DocumentReference, where the PDF goes into content.attachment. In the ED datatype map, the data subtype becomes contentType and the data is converted to Base64, with a note advising implementers to look at the data itself to settle the content type, because implementations are not always accurate.

Copying the string across as it is does not work. In R4, base64Binary allows whitespace only between four-character groups, and our string, with two spaces inside a group, fails its regular expression. The PDF has to be decoded, checked and re-encoded. Then there is a simpler case: if the sender declares a coded type such as CE in OBX-2 instead of ED, the guide routes OBX-5 to a CodeableConcept, and the PDF becomes the text of a code.

How it came up

In a public discussion on chat.fhir.org about a shared collection of v2-to-FHIR test cases, the developers of an open-source engine reported finding, on a public set of test messages, that their decoder rejected PDFs with spaces in the Base64. The message then fell into the coded-value path and the PDF came out as the text of a code. They fixed the decoder; we pointed out that their test for spotting a truncated file, based on length, did not hold either, and they replaced it with a check on the content. The whitespace case, with the reference PDF and its SHA-256 digest, now lives in the public test-case repository we opened for that collection.

Completeness is judged by content

If length is not enough, you have to look inside. The PDF standard (ISO 32000-1:2008) requires the first line of the file to be a header starting with %PDF- and the last line to contain only the %%EOF marker. An incrementally updated file has more than one, because each update closes its own trailer with its own %%EOF: the last one is what counts, and a line break after it should be tolerated. A converter can run this check on its own, and it would have told our truncated file from the intact one in all five tests. A test can go further and keep a digest of the expected bytes next to the message, so that passing becomes a comparison rather than an impression.

The same Base64 PDF in OBX-5 given to five decoders. With spaces in the text, strict decoders reject it and MIME or lenient ones decode it intact at 752 bytes. Without the trailing =, Python rejects it while Java and Node decode it intact. Truncated, Python rejects it for the wrong reason and Java and Node accept 503 bytes with no %%EOF marker.
One string, different verdicts. Length does not tell you whether the PDF is complete; the %PDF- header and the %%EOF marker do.

What to take away

  • How tolerant the decoder is should be a project decision, not a library default. For v2 messages the reference is MIME: ignore spaces and line breaks, accept missing padding with a warning, reject lengths that cannot correspond to any data.
  • An attachment is not copied: it is decoded, checked and re-encoded in the form FHIR accepts.
  • Completeness is judged by content: for a PDF, a %PDF- header at the start and %%EOF at the end. The content type can be read from the content too, as the guide itself suggests.
  • Check OBX-2: an ED declared as CE turns a report into a code.
  • An attachment that fails the checks must not vanish: it stays as text, with a warning in the report that says why.

Which leaves two questions to ask at the start of a project. To the sender: how do you wrap and pad the Base64 you send? To whoever builds the conversion: which decoder do you use, in which mode, and what does your report say when an attachment arrives truncated?

Seeing a similar case in your project?

Write to us: we start from your own cases.