PDF Report Auto Export

Hi everyone
my name is Mirco and I write from Italy.
I’m absolutely new with DICOM but I’m going to use it in our clinic.
After the first tests it seems our environment is working well. We have a Windows-based server with Orthanc server installed, cURL and DCMTK.
Our Web-based CRM generates a CSV file with a unique PatientID, Patient Name, Patient Surname, Birthday and gender everytime a new patient is registered by the receptionists.

Twice a minute a batch file uses the CSV data to populate an empty XML file that is converted to a Worklist entry using xml2dcm command from DCMTK. After the patient made his exams they are all stored in Orthanc DB.

Now for legal reasons we need to export the report that is stored in every study and pass it to the CRM with a procedure already in use for other reasons.
I’m not able to find a way to build a batch file which get the PDF Report of a study just having available the data contained in the CSV file.

Can anyone help me? Thanks to everyone

Hello,

I’m unsure to understand your message. There exists a route named /instances/{id}/pdf in the REST API of Orthanc if you want to retrieve a PDF file that is encapsulated inside a DICOM file.

Regards,
Sébastien-

Hello Sébastien
this is a wonderful thing, I didn’t know that path existed.
Tomorrow I’ll try it and let you know, thanks a lot.

This may sound silly: isn’t there a route for all PDFs in a study?

Mirco

No, but you can easily implement it using the following Python plugin:

import io
import json
import orthanc
import zipfile

def GetAllPdf(output, uri, **request):
  study = request['groups'][0]
  print('Accessing study: %s' % study)

  zip_buffer = io.BytesIO()

  with zipfile.ZipFile(zip_buffer, 'a') as zip_file:
    for instance in json.loads(orthanc.RestApiGet('/studies/%s/instances' % study)):
      try:
        pdf = orthanc.RestApiGet('/instances/%s/pdf' % instance['ID'])
        zip_file.writestr('instance-%s.pdf' % instance['ID'], pdf)
      except:
        # The DICOM file doesn't contain a PDF
        pass
  
  output.AnswerBuffer(zip_buffer.getvalue(), 'application/zip')

orthanc.RegisterRestCallback('/studies/([0-9a-f-]+)/pdf', GetAllPdf)

Thanks to the plugin above, if you are interested in retrieving all the PDF that are encapsulated within a study whose Orthanc identifier is, say, ef2ce55f-9342856a-aee23907-2667e859-9f3b734d, you would open:

http://localhost:8042/studies/ef2ce55f-9342856a-aee23907-2667e859-9f3b734d/pdf

HTH,
Sébastien-

1 Like

Hi Sebastian
I see that you know your job very well but I know absolutely nothing about programming higher than machine code, I’ll have to study the whole procedure carefully to install and activate the plugin you suggested. Can you point me to a step-by-step guide for absolute beginners?
I have to ask you 2 more questions: first of all I would like to know if there is a way to discriminate which study is the most recent in case a patient has more than one. I saw that the patient ID is a SHA1 Hash of the ID with which I insert it in the worklist, is there also a link between the study ID and the execution date?
The second question is: if a patient takes more than one test on different dates, are the studies merged under the same patient ID? I read somewhere that if two patients have the same ID the newest one is discarded, but this doesn’t happen if the same patient performs several studies on different dates, right? The patient ID (0010, 0020) with which the CRM generates the entry in the worklist is the same for the 2 exams, same name and surname, same date of birth…
Thank you for your patience and to all those who can help me.
Have a nice day

Hello,

Please check out the documentation of the Python plugin in the Orthanc Book. If you need an introduction to Python, there are plenty of resources over Internet.

Yes, check out the LastUpdated field that is associated with each study if calling the /patients/{id}/studies?expand route in the REST API of Orthanc.

For instance, here is a direct link to a patient on our demo server: https://orthanc.uclouvain.be/demo/patients/da39a3ee-5e6b4b0d-3255bfef-95601890-afd80709/studies?expand

If your imaging modalities are correctly configured, yes, any imaging study for the same patient should have the same Patient ID (0010,0020). Studies with the same Patient ID are transparently grouped together by Orthanc.

Regards,
Sébastien-

Good morning Sébastien
I’ve already passed that step, now I’ve almost achieved the result I wanted.
Right now my CRM generates a file that I transform into a worklist entry for every imaging modalities, the imaging modalities access the worklist and at the end of the exams deposit the data into Orthanc DB.
Every 10 seconds I query Orthanc with the API to check if any device has added new instances using curl.exe http://myOrthancServer:8042/changes, in case of new instances I download the list of instances of the day, I check if I haven’t already analyzed them, if they are new I download the PDF content and send it to the CRM, all this via some batch files.
Since I don’t know how to parse JSON from batch files, I was able to get the data I was looking for here https://discourse.orthanc-server.org/t/unexpected-value-in-response-to-a-query/ via an XML generated by the modalities themself, and I pass them via an API to the CRM.
I don’t know why but the imaging modalities doesn’t correctly populate all DICOM fields as decleared in their DICOM Conformance statement, no big deal because I solved it with the XML file.
I hope that the query every 10 seconds is the best way to check for new instances, it would be easier if there was an automatic LOG in real time containing only a list of instances with date and time of creation so as not to have to download and check all the instances of the day every time, but for now everything works fine like this. Anyway I’d say I’m 100% operational.
Thank you very much for your help and for the valuable software I am using.

1 Like

Fine, great to read that you are operational!

@oladeleayomide831 Check this out

1 Like