Web interface to insert PDFs

I am looking for some input on a method to create a web interface to insert PDFs into studies. There does not seem to be any project anywhere for what I guess would be a Plugin if it were completed to fruition. Please tell me if there is one somewhere. If not I am considering doing a hack and sharing the results with the brave amongst us. So far I have…

  1. I found the method to insert using command line. Although intriguing I wouldn’t expect anyone to seriously use such a method in the real world. Only the bleary-eyed programmers would even consider it and they don’t run people through doughnuts.

a) Every attempt I tried created a new separate patient entry and put the document into a study under that, which was not where I wanted it. Using Powershell…
$user = ‘admin’
$pass = ‘123456789’
$pair = “$($user):$($pass)”
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = “Basic $encodedCreds”
$Headers = @{
Authorization = $basicAuthValue
}

create the BASE64 string data

$fileInBase64 = $([Convert]::ToBase64String((gc -Path “test.pdf” -Encoding Byte)))

create the json data

$params = @{Tags = @{SeriesDescription = “PDF report”};Content= “data:application/pdf;base64,$fileInBase64”;Parent = “9212fc89-58d0fe17-29b298af-f3bc1888-fc40881e”;}
$ProgressPreference = ‘SilentlyContinue’
$reply = Invoke-RestMethod http://192.168.1.11:8042/tools/create-dicom -Method POST -Body ($params|ConvertTo-Json) -ContentType ‘application/json’ -Headers $Headers
Write-Host “The instance can be retrieved in PDF at http://192.168.1.11:8042$($reply.Path)/pdf”

b) However I was able merge the document into the correct patient where it more or less needed to go using the following.

TARGET ID <---- PDF ID

curl --user admin:123456789 http://localhost:8042/studies/4115fdd2-18bd2765-338f7c22-c6851ef4-0d390ea4/merge -d ‘{“Resources”:[“541f9166-f8718a30-3d02a904-5d898554-d36b67d4”]}’
c) Is there a way to do this with the interface without having to do a merge also? Every attempt I tried failed and created a new patient entry.

  1. I want to find a method of inserting the pdf using a php interface. There are two logical methods to attempt this. Both would call a php page in Apache to either request the pdf file to be uploaded and merged and/or maybe even interface to a twain scanner. Both would call REST interface and just cram data in using php.
    a) METHOD ONE: I want to either modify the interface.html page to add buttons to the patient entries which would pass parameters to call some php to cram data into REST interface. This would be the best looking. But the way the code is written makes it look almost impossible.
    b) METHOD TWO: If previous does seems impossible create a simple LAMP interface that pulls from the mysql database directly, gathers up the parameters, and it call php to cram data into REST interface. It wouldn’t be as pretty but it would work. I have done this stuff before.

Any thoughts on either method? You are heavily invested in RESTful interface method which seems to have complicated the results for anyone considering modification. The fact that the web interface is compiled into the executable puts the complexity of changing the code into orbit.

  1. On a whim to see what I could get away with, I tried moving over the OrthancExplorer directory under Apache. It gives the superficial impression it might work but we know better. The difficulty is finding some way to redirect the javascript code to the correct ip and port so it will pull and push data to the REST interface. As far as I can tell you use the existing web page connection to create this interface to the REST. HOW CAN I REDIRECT THIS? WHAT DO I NEED TO CHANGE? Any chance I have at all of modifying the java script and html code is going depend on this. I did try throwing a few things at it but I am stumped. Otherwise I will just interface directly with mysql which is let’s face it will be rude and crude and low rent.

I now have success at running Orthanc within Apache and using the html and javascript code within Apache2 server by redirecting all queries and data returns to the real REST interface. It actually works. It took the password and everything. Even the viewer works. I can now modify the html and javascript code without having to compile the whole package.

I am writing all this down while it is still fresh. I will try not to skip anything.

I am using Kali kali-linux-2020.2-installer-amd64.iso which is basically Debian 10 because it can apt install 1.7.2. I now realize just how easy it is to compile Orthanc. Kali is already up to 3.

On the same server that I have Orthanc installed, I installed apache2. Something like: apt install apache2

From expanded tar ball I copied over the Orthanc Explorer something like this:

cp -R /root/Orthanc-1.7.3/OrthancServer/OrthancExplorer /var/www/html

This should include the folders underneath and obviously at this point the page should come up but cannot pull data until we do the magic.

So at this point we have:

http:// your-ip /explorer.html pulls up the page within a big fat yellow error message.

http:// your-ip:8042/ pulls up page into a working copy or Orthanc.

Now let’s blow your mind and make your eyeballs pop out of your head.

Install the following apache2 modules: (I think these will be temporary)

a2enmod proxy

a2enmod proxy_http

a2enmod proxy_balancer

a2enmod lbmethod_byrequests

add the following lines to /etc/apache2/sites-available/000-default.conf

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

ProxyPass /plugins http://127.0.0.1:8042/plugins

ProxyPassReverse /plugins http://127.0.0.1:8042/plugins

ProxyPass /system http://127.0.0.1:8042/system

ProxyPassReverse /system http://127.0.0.1:8042/system

ProxyPass /studies http://127.0.0.1:8042/studies

ProxyPassReverse /studies http://127.0.0.1:8042/studies

ProxyPass /patients http://127.0.0.1:8042/patients

ProxyPassReverse /patients http://127.0.0.1:8042/patients

ProxyPass /series http://127.0.0.1:8042/series

ProxyPassReverse /series http://127.0.0.1:8042/series

ProxyPass /web-viewer http://127.0.0.1:8042/web-viewer

ProxyPassReverse /web-viewer http://127.0.0.1:8042/web-viewer

ProxyPass /favicon.ico http://127.0.0.1:8042/favicon.ico

More may be needed. Track the log files by:

Set apache2 to debug mode. In /etc/apache2/apache2.conf change LogLevel to debug.

cd /var/log/apache2

tail -f access.log

cat error.log | grep File

[Sun Sep 06 13:21:00.934497 2020] [core:info] [pid 31292] [client 192.168.95.100:13218] AH00128: File does not exist: /var/www/html/web-viewer/app/viewer.html, referer: http://192.168.95.181/explorer.htm told me I needed the 2 web-viewer lines above.

Make sure the settings change take place.

systemctl restart apache2

And ¡Viola!

Hit http://your-ip/explorer.html and it will now sign into Orthanc.

Drum roll please… and the hacking begins.

That’s interesting. Never tried setting it up so that I could revamp the Orthanc Explorer front-end that way. Might have to try that.

There are some other things you might find useful:

There is a “ServeFolders” plug-in that uses an embedded CivetWeb server as an add-on to Orthanc. You can basically serve another static site out of that folders, although I guess you could use an API of sorts with AJAX to PHP/MySQL or some other database. You would still need a proxy setup for that, but that is a nice feature because you can avoid some of the issues with CORS, but you’ll still need to setup proxies if you are accessing remotely.

I’ve played around with a number of arrangements and have a couple of demos that are open with a dev server. (Would appreciate it if you don’t mess with it too much, or I’ll have to take it down. There are a bunch of test exams on there.

  1. https://sias.dev:8000/orthanc/app/explorer.html, Proxied link to the Explorer. It is somewhat easy to setup an auth mechanism using NGINX http_auth_request module, but I’m not using that now. That gives you direct access to the explorer and viewer from the internet.

  2. Sample front-end using the Civet Web Browser that is built-in. There are some advantages and disadvantages using that setup, although you can make AJAX calls directly if it is proxied because it also runs on localhost and gets around some of the CORS issues that way.

  3. https://sias.dev:8000/browsestudies/index.html, using an NGINX / PHP-FPM server running on the same host as Orthanc. Many more possibilities because it is a LEMP / LAMP stack.

  4. https://sias.dev:8443/browser/uploader/upload.html, just kind of threw that together, but that is one way you can attach a PDF to a study. Needs some work, but I had some libraries available to just see if it works. The “test” study is:

https://sias.dev:8000/orthanc/app/explorer.html#study?uuid=660e7608-23e3eecb-da0a810a-c4e4619f-eb394b26

The backend is hardcoded to attach the PDF to that “Parent”. If you just upload a pdf using the upload page, it’ll take a few seconds to upload the pdf image, and there is some logging stuff that shows up, as well as an alert about base64 encoding. The script doesn’t do that, but you could probably base64 encode on the client side and then just use AJAX to call tools/create, rather than doing it with PHP in the background. I have a PHP script that does the conversion and then calls tools/create-dicom.

If you try it, you can just leave any attached PDF with the study, but don’t delete the study. If you know how to just delete the series (i.e. the PDF, go ahead and do that). Be happy to work with you on some stuff if you are interested.

Hello,

From the last line of your second post, it looks like your issue is now solved.

If this is not the case, please ask. But keep in mind to keep your message as short as possible, as the core Orthanc team doesn’t have infinite bandwidth to process user questions.

Sébastien-

PDF web interface upload using PHP is complete and operational WHO WANTS THE CODE? Linux only I think anyway. I need to upload this I think. Somewhere.

I modified explorer.js to create a popup window to my php code and I am off to the races. I used the php code to input a file and and a study name. Then I queried and inputted data into the REST interface using php-curl! I will upload the php code if the moderator allows me. lol I am amazed how little this actually took to do. I am starting to like REST APIs. I will stick the 4 php files in the next post.

You need apache2 and php and of course apt-get install php7.4-curl

I uploaded the fire to /tmp so…
So to stop wacky names like this :/tmp/systemd-private-eb7a2066fcd9465bb109b57830ed06d3-apache2.service-6XDa5g/
vi /usr/lib/systemd/system/apache2.service
comment out (with a #) the PrivateTmp parameter
systemctl daemon-reload
systemctl restart apache2

find within explorer.js this

In explorer.html
Make dah button. I added on the 2nd line

Hi.
It looks like we both were working on the same solution.

Earlier this morning, I posted a very similar approach, but on Nginx.
It uses the pdfjs library to allow PDF preview but I couldn’t manage to get working a window popup, so I solved this issue with some redirects.
The sources are at available at https://github.com/Terabuck/Elessar

Regards
Ludwig

Similar approach:

https://sias.dev:8443/browser/browser.php

The red paperclip should allow you to attach a pdf. It apparently works with .png and probably .jpg and .gif images. I tried an .mp4 and Orthanc did not like that, but I wonder if you can attach video files as well ? Clicking on the paperclip again will close the dialog for uploading (toggle switch).

The little icon at the far left should open up a study in the viewer in a new tab, so you can attach an image or document and then view the study after attachment, or direct to Explorer here: https://sias.dev:8000/orthanc/

It doesn’t like videos. The api interface is expecting a pdf.

But hey It didn’t explode. It threw a reasonably worded error message.

Study or ID = 190e4be3-b6a8b79e-b571b6e5-1c3eb1b8-fa02a89d
PatientName = testtest Target = /tmp/685.mov

The file 685.mov has been uploaded.

Base64 size of /tmp/685.mov = 1309616

Results of Dicom Insert:

N_ParentStudy = Bad file format

N_Status = Bad file format PDF

name set to = “PDF Report 685.mov”

The api interface only wants a pdf. No png files. No text files.

In uploadpdf2.php move one line down to inside braces.

I am working on a paper scanner if anyone is interested. I will probably just enslave this guys code: http://www.gssezisoft.com/main/cmdtwain/ It’s command line and seems to work well. It’s stable and even pops up with a progress bar. Has a separate pop up box to chose the twain that is just begging for separate PHP call and a button.

Then I started thinking. Yeah I was just going to use a compiled autoit script and call his executable call it quits. But then I thought that I might get “sooophisticated” and call it with javascript from the local browser rather than server-side in PHP. That would be slick. The Orthanc programmers might even take my name off the black list. That sounds easy but it’s not.That cracked open a fat can of worms. Because then I remembered that old jabberscript only uses websockets, you know for “seeeecurity”. And every time someone in the usability department tries to add simple ports some guy in security whacks down the concept. God help us if someone hacks into a printer off a web site and jumps on port 9100 and runs all the paper out on the floor. So if I follow that route I will have to write it into Visual Studio and at least in c/cpp, because websockets is not something that autoit supports much. If I have to do that I almost might as well just find the scanner twains and interface with them myself. Grinding the images into a pdf can’t be that difficult. And once going that far we should just shove it up the REST interface in base64. But that will not be a weekend project.

In the mean time for my own purpose at my radiologist buddy’s place I will probably just finish it in PHP and use the AUTOIT script. A couple of commands and a couple of buttons and it’s done. Autoit will sit there and wait forever in a pc in the background taking up no cpu waiting on a tcp port for the “go for it” command. It does use 3mb of ram but that’s almost nothing. As long as I set the firewall to leave port 8888 open, it will run for years. I will have to keep an eye on programs like Malwarebytes. They can mistake AutoIt code as a trojan.

There is an online PACS company here in Florida that uses the following software to interface to paper scanners over the internet. There is a download and it installs onto the windows PC. In this new world of insane seeeeecurity the only way to make a paper eater work on the internet is to have a software agent running on the PC. Correct me if I am wrong.

https://www.dynamsoft.com/Secure/imaging-web-application-buyit.aspx#DynamicWebTWAIN.That’s a $1000 USD/yr every year. This is what is required just to make it work in the Javascript. Here is the other one that must be the cat’s meow. https://asprise.com/document-scan-upload-image-browser/web-scanner-source-code-open-order.html. Their uncrippled version is $20K USD one time. They sure must be proud of their product.

The world needs an opensource web scanner interface project that can use websockets from JavaScript[t. We will see if I write one or not. All the opensource projects I have investigated are old, dead, and paved over by time. One used python and the instructions were a complete mind-bending dead end. After I did a full face plant and begged for help the guy did answer with an apology. Another one used cab files and was done 15 yrs ago. Try to make that work now.

So… my Apache2 mod with the ability to use PHP is looking pretty good. Well to me anyway. I am sure the Orthanc software developer thinks I am an irritating nuisance. But PHP opens up a whole world of possibilities. I will have my PHP paper-eater with a compiled Autoit executable running this weekend. A monkey could write a paper-eater interface in PHP for Orthanc beating on a keyboard with half a coconut. Me, the monkey, has part of it already done.

However, my first draft PHP code will only work on local networks. PHP will have to open a tcp connection directly to the PC on port 8888. The PC and Orthanc will have to be able to see each other bidirectionally. No proxies, ssh tunneling, or port forwarding in the mix.

A tunnel over the internet could be used like openvpn. We have crammed dicom studies over openvpn for 20 years. Openvpn is rock solid, uses only 3% more overhead than IPSEC and runs in Linux and Windoze. They of course even have a Mac version. Don’t even try to tell me they didn’t steal their whole OS from Linux .

“I am sure the Orthanc software developer thinks I am an irritating nuisance.”

=> I don’t think so, I just need clear, sharp, focused questions to join the party. Feel free to discourse, but don’t expect an answer from myself as long as your messages are too long and broad.

Thanks for your input and guidance.

I have the Paper Scanning Autoit code compiled, and working. It accepts commands over a tcp port and calls http://www.gssezisoft.com/main/cmdtwain/. Telnetting from Linux already initiates scanning responding with a pid. Using this guy’s code in this manner seems to be what he intended as its use. My Autoit code is fairly small tight simple solid and effective, as much as the compiler allows anyway. A days worth of work, no more is all that will be required to do the PHP. Calling it from PHP will be very easy. I built in some utilities to test if the cmdtwain exe is in place, if the share is there; I even gave it the ability to insert a credential manager entry so as to use some authentication with the samba share. We have seen Win 7 have a tendency to drop credential entries on a reboot, W10 not so much.

For my purpose in a controlled environment my code will be fine. Turning it loose on someone not fully aware of what it is doing in some unknown environment that requires more security might be irresponsible of me without a warning. It needs at least some rudimentary authentication and maybe some form of encryption since all the commands are viewable by any packet sniffer.

Should I post my code for the savvy ones when I have the PHP working? It’s difficult for me to measure whether it has any usefulness to the community. Running your web code from Apache might be little over most people’s heads? Maybe too radical for them? It’s not like it’s going to harm anything. It either works or it doesn’t. The main interface is untouched.

If nothing else my efforts might pave the way for a proper JavaScript version that is written in the style of the existing code. Autoit shows some potential to support webconnect sockets with some coding. Re-doing it in C/CPP might be more appetizing.

Would be interesting in finding a solution for that on LINUX with a Web Interface. WebCams are pretty easy to integrate. TWAIN not so much. It isn’t too difficult to just use the intermediate step of saving a file to the local file system and then uploading that to Orthanc, but having a GUI through the web interface that could stream the image data through to Orthanc would be nice.

BTW, the create-dicom callback does support .png images, and possibly .jpg and others, but not .mov or .mp4 from what I can tell. .png’s definitely work. Maybe Sebastian could comment on what MIME types are supported by that as far as what Orthanc will accept and what MIME types will be displayed by the viewer.

Here is the link to the source code related to “create-dicom”:
https://hg.orthanc-server.com/orthanc/file/Orthanc-1.7.3/OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp#l1173

As can be seen, PNG, JPEG, PAM and PDF are supported.

Regarding videos, this is something you can contribute to, as written in this page of the Orthanc Book:
https://book.orthanc-server.com/contributing.html