ORU lab results to a PDF report and email
Render each HL7v2 ORU^R01 lab result as a password-protected PDF report and email it to a clinic mailbox, as a Mirth Document Writer and SMTP Sender channel does.
Some clinics still take lab results as a printed report rather than a feed into their EHR. In Mirth, a channel like this has a Document Writer that renders the result as a PDF and an SMTP Sender that emails it. This recipe builds the same channel in MedHook: each ORU^R01 that arrives over MLLP becomes a password-protected PDF, attached to one email to the clinic's results mailbox.
Source and destination
The lab system connects to the engine's MLLP listener. A JavaScript step reads the fields the report shows, a Document Writer step renders the PDF, and an SMTP step sends it.
flowchart LR
A[Lab system] -->|ORU^R01 over MLLP| B[MLLP trigger]
B --> C[JavaScript: report fields]
C --> D[Document Writer: PDF]
D -->|document| E[SMTP Sender]
E -->|email with PDF| F[Clinic mailbox]- Source: HL7v2 ORU^R01 over MLLP.
- Destination: a mailbox behind an SMTP relay that accepts STARTTLS or SSL.
- Result: one email per result message, with the report attached as a PDF that opens only with the profile's password.
What it uses
- The MLLP trigger and its ACKs.
- A JavaScript step with the
hl7helpers. - The Document Writer adapter, action
send, withpdfPasswordon its profile. - The SMTP Sender adapter, action
send, withattachInputDocument.
The trigger takes results only:
{ "type": "mllp", "messageTypes": ["ORU^R01"], "deduplicate": true }The JavaScript step reads the patient and each OBX from the message, and returns a record whose fields the report template names. The template fills fields as plain text and does not escape them, so the step escapes every value it puts in the HTML:
const raw = input.rawMessage;
const esc = (s) =>
String(s || "")
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
let rows = "";
for (let i = 1; i <= 50 && hl7.get(raw, `OBX[${i}].3`); i++) {
rows +=
`<tr><td>${esc(hl7.get(raw, `OBX[${i}].3.2`))}</td>` +
`<td>${esc(hl7.get(raw, `OBX[${i}].5`))} ${esc(hl7.get(raw, `OBX[${i}].6.1`))}</td>` +
`<td>${esc(hl7.get(raw, `OBX[${i}].7`))}</td>` +
`<td>${esc(hl7.get(raw, `OBX[${i}].8`))}</td></tr>`;
}
return {
rawMessage: raw,
patient: esc(`${hl7.get(raw, "PID.5.2")} ${hl7.get(raw, "PID.5.1")}`),
mrn: esc(hl7.get(raw, "PID.3.1")),
collected: esc(hl7.get(raw, "OBR.7")),
rows,
};The Document Writer step renders the report. With output attachment, the PDF is the step's output, for the SMTP step to attach. encrypt makes the step refuse to write while the profile has no pdfPassword, so no report goes out unprotected:
{
"documentType": "pdf",
"output": "attachment",
"encrypt": true,
"fileName": "${messageControlId}.pdf",
"template": "<h1>Laboratory report</h1><p><b>Patient:</b> ${patient}<br><b>MRN:</b> ${mrn}<br><b>Collected:</b> ${collected}</p><table><tr><th>Test</th><th>Result</th><th>Range</th><th>Flag</th></tr>${rows}</table>"
}The SMTP step sends the email. Its fields are fixed text here: the recipients and subject come from the step, not from the message, and the subject names no patient:
{
"to": "results@clinic.example.test",
"subject": "New lab report from Demo Health Lab",
"body": "A new lab report is attached. Open it with the password your lab contact gave you.",
"attachInputDocument": true
}A test message needs only synthetic values:
MSH|^~\&|LAB|DEMO|CLINIC|DEMO|202610240101||ORU^R01|MSG0001|P|2.5
PID|1||TEST123^^^DEMO||TESTPATIENT^ALPHA||19800101|F
OBR|1|ORD0001||GLU^Glucose|||202610240800
OBX|1|NM|2345-7^Glucose||98|mg/dL|70-99|N|||F
OBX|2|NM|2823-3^Potassium||5.4|mmol/L|3.5-5.1|H|||FStatus
Available on this release. Importing a Mirth channel with a Document Writer and an SMTP Sender maps both connectors. Mirth's SMTP Sender attaches the document with ${ATTACH:...}, which the importer flags in a finding; set attachInputDocument on the SMTP step and run it after the Document Writer, as above. The PDF password is never imported: enter it as pdfPassword on the Document Writer profile.
Know the limits before you go live:
- The PDF uses the standard fonts (Western European text). Other characters print as
?. - A report is at most 1,000 pages and 100,000 drawing operations. See Document Writer limits.
- Email is not a secure channel for PHI by itself. Keep the PDF password on, and use STARTTLS or SSL on the SMTP profile.