Проблема с вызовом WKHTMLTOPDF из скрипта Python на UBUNTU
У меня возникли странные проблемы с выполнением wkpdftohtml из скрипта Python, который является частью другой серверной инфраструктуры (Orthanc Server). Вероятно, это мало связано с этим, но со сценарием Python и тем, как привилегии, пути и т. Д. Настраиваются в моей системе.
Я использую его как часть pdfkit, и я установил исполняемый файл wkhtmltopdf несколькими способами, используя пакеты apt, двоичные и deb, в том числе из:
Я уже опубликовал там отчет об ошибке.
https://github.com/JazzCore/python-pdfkit
Отчет об ошибке от Github.
Не уверен, что проблема в pdfkit. Я запускаю сервер (проект с открытым исходным кодом Orthanc, написанный на.cpp). У него есть функция, позволяющая включать плагины Python в свою структуру. Я создал для этого несколько подключаемых модулей Python, и все они работают нормально, за исключением того, который использует pdfkit в качестве оболочки для wkhtmltopdf. Странно то, что когда я запускаю Orthanc из интерфейса командной строки, он находится на переднем плане с помощью такой команды:
/ путь / к / orthanc /path/to/config/config.json --verbose
wkhtmltopdf работает и ошибок нет.
Когда я запускаю из интерфейса командной строки, используя init.d, например:
/etc/init.d/orthanc (запуск | остановка | перезапуск)
мой плагин не работает с ошибкой:
"Исполняемый файл wkhtmltopdf не найден: "b''"\n Если этот файл существует, убедитесь, что этот процесс может его прочитать. В противном случае установите wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf%22
Orthanc запускается в любом случае, и журналы показывают загрузку всех плагинов, и Orthanc Explorer и большая часть моего API также работают нормально.
Соответствующий код в скрипте Python: config = pdfkit.configuration(wkhtmltopdf=bytes("/usr/local/bin/wkhtmltopdf", 'utf8'))
В моем пути ENV есть:
ПУТЬ =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
Поэтому кажется, что он не может найти исполняемый файл или по-другому не имеет разрешений при запуске с помощью сценария init.d.
Разрешения для исполняемого файла - 755, владелец - root.
Сегодня я столкнулся с той же проблемой (возможно, она никогда не исчезла, потому что я обычно работаю на OS X, а это на сервере VPS с запущенным UBUNTU, но такой же настройкой, как и OS X).
Сегодня я забыл, что мне нужно запустить Orthanc, как указано выше, из интерфейса командной строки, чтобы он заработал, поэтому проблема с использованием сценария инициализации по-прежнему вызывает проблему.
Я также получил сегодня еще одну ошибку, когда я запустил из каталога с исполняемым файлом Orthanc без указания абсолютного пути:
РАБОТАЕТ:
./Orthanc /home/sscotti/Desktop/OrthancConfigs/Configuration.json -verbose
OR
/home/sscotti/Desktop/Orthanc/build/Orthanc /home/sscotti/Desktop/OrthancConfigs/Configuration.json --verbose </dev/null &>/dev/null &
вместо:
НЕ РАБОТАЕТ и кидает:
/home/sscotti/Desktop/Orthanc/build/Orthanc /home/sscotti/Desktop/OrthancConfigs/Configuration.json --verbose
WARNING:root:Suggest using Python 3.x.x, using: 3.8.6
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
PyThreadState_Clear: warning: thread still has a frame
E1124 05:29:29.568863 PluginsErrorDictionary.cpp:111] Exception inside the plugin engine: Error encountered within the plugin engine
E1124 05:30:08.581543 PluginsManager.cpp:197] Exception while invoking plugin service 2000: Error in the network protocol
E1124 05:30:08.581738 HttpOutput.cpp:69] This HTTP answer has not sent the proper number of bytes in its body
E1124 05:30:08.606843 PluginsManager.cpp:197] Exception while invoking plugin service 2000: Error in the network protocol
E1124 05:30:08.607007 HttpOutput.cpp:69] This HTTP answer has not sent the proper number of bytes in its body
Исполняемый файл находится в каталоге / usr / local / bin с правами 755 и принадлежит пользователю root. Я также использую CLI как root.
Скрипт Python в основном таков:
def attachbase64pdftostudy(query):
attachresponse = dict()
if query['studyuuid'] != "":
query = '{"Tags" : {"Modality":"OT", "SeriesDescription":"' + query['title'] + '","SOPClassUID":"1.2.840.10008.5.1.4.1.1.104.1"},"Content" : "data:application/pdf;base64,' + query['base64'] + '", "Parent":"' + query['studyuuid']+ '"}'
orthanc.RestApiPost('/tools/create-dicom',query)
attachresponse['create-dicom-response'] = "Sent to PACS."
else:
attachresponse['create-dicom-response'] = "Missing UUID for parent study."
return attachresponse;
def getpdf(query, output):
response = dict()
if query['method'] == "html":
try:
options = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
}
config = pdfkit.configuration(wkhtmltopdf=bytes("/usr/local/bin/wkhtmltopdf", 'utf-8'))
pdf = pdfkit.from_string(query['html'], False,options=options)
encoded = base64.b64encode(pdf).decode()
# If attach flag is 1 then attach it to the studyuuid
if query['attach'] == 1:
query['base64'] = encoded
response['attachresponse'] = attachbase64pdftostudy(query)
elif query['return'] == 1:
response['base64'] = encoded
output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')
except Exception as e:
response['error'] = str(e)
output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')
elif query['method'] == "base64":
response['attachresponse'] = attachbase64pdftostudy(query)
output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')
else:
response['error'] = "Invalid Method"
output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')
def HTMLTOPDF(output, uri, **request):
if request['method'] != 'POST':
output.SendMethodNotAllowed('POST')
else:
query = json.loads(request['body'])
pdf = getpdf(query, output)
orthanc.RegisterRestCallback('/pdfkit/htmltopdf', HTMLTOPDF)
Скрипт init.d:
# PATH should only include /usr/* if it runs after the mountnfs.sh script
# SDS Added /root/OrthancBuild
#PATH=/sbin:/usr/sbin:/bin:/usr/bin
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/home/sscotti/Desktop/Orthanc/build
# You can modify the variables below
DESC="Orthanc"
NAME=Orthanc
DAEMON=/home/sscotti/Desktop/Orthanc/build/Orthanc
LOGDIR=/home/sscotti/Desktop//OrthancLogs
DAEMON_ARGS="--logdir=${LOGDIR} /home/sscotti/Desktop/OrthancConfigs/Configuration.json"
PIDFILE=/run/orthanc.pid
SCRIPTNAME=/etc/init.d/orthanc
#ORTHANC_USER=orthanc:orthanc
ORTHANC_USER=root:root
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Prepare a directory to store the Orthanc logs
mkdir -p $LOGDIR
chown $ORTHANC_USER $LOGDIR
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $ORTHANC_USER --background --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --chuid $ORTHANC_USER --background --exec $DAEMON -- $DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $ORTHANC_USER --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/1/KILL/5 --chuid $ORTHANC_USER --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --chuid $ORTHANC_USER --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
Одна вещь, которую я не пробовал, - это добавление / usr / local / bin в PATH в сценарии Init.d, см. Сверху:
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/home/sscotti/Desktop/Orthanc/build
хотя, когда я повторяю PATH из интерфейса командной строки, я получаю:
[05:55] [server1.sias.dev ~] # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Вид раздражает. У меня есть несколько обходных путей, но я хотел бы понять, в чем заключается основная проблема.
Благодарю.