在UBUNTU上从Python脚本调用WKHTMLTOPDF时出现问题

2024-09-25 02:36:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从另一个服务器框架(Orthanc服务器)的Python脚本执行wkpdftohtml时遇到了一些奇怪的问题。这可能与此无关,但与Python脚本以及在我的系统上设置特权或路径等的方式有关

我将其作为pdfkit的一部分使用,并且我使用apt、binary和deb软件包以多种方式安装了wkhtmltopdf可执行文件,包括:

事实上,我之前在那里发布了一个bug报告

JazzCore / python-pdfkit

来自Github的Bug报告


不确定这是否是pdfkit的问题。我正在运行一个服务器(用.cpp编写的Orthanc开源项目)。它有一个允许将Python插件合并到其框架中的特性。我为此创建了一些Python插件,它们都工作得很好,除了一个使用pdfkit作为wkhtmltopdf包装的插件。奇怪的是,当我在前台使用如下命令从CLI启动Orhanc时:

/path/to/orhanc/path/to/config/config.json——详细

wkhtmltopdf工作正常,没有错误

当我使用init.d从CLI启动时,我喜欢:

/etc/init.d/orhanc(启动|停止|重新启动)

我的插件失败,错误如下:

未找到wkhtmltopdf可执行文件:“b”“\n如果此文件存在,请检查此进程是否可以读取它。否则,请安装wkhtmltopdf-https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf"

Orthanc以任何一种方式启动,日志显示所有插件的加载情况,Orthanc Explorer和我的大部分API也可以正常工作

Python脚本中的相关代码是:config=pdfkit.configuration(wkhtmltopdf=bytes(“/usr/local/bin/wkhtmltopdf”,'utf8'))

我的环境路径有:

路径=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

因此,当我使用init.d脚本启动时,它似乎找不到可执行文件或者没有权限

可执行文件的权限为755,所有者为root


我今天也遇到了同样的问题(实际上它可能从来没有消失过,因为我通常在OSX上工作,这是在运行UBUNTU的VPS服务器上,但设置与OSX相同)

今天我忘记了我需要从CLI启动上面提到的Orthanc才能工作,所以使用init脚本的问题仍然会导致问题

今天,当我使用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所有。我也以root身份从CLI运行

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

:

我没有尝试过的一件事是在Init.d脚本的路径中添加/usr/local/bin,请参见上面的内容:

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/home/sscotti/Desktop/Orthanc/build

虽然当我从CLI回显路径时,我得到:

[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

有点烦人。我有一些解决办法,但我想了解根本问题是什么

谢谢


Tags: thetoinlogjsonifbinusr