pyqt / python-qt5

Unofficial PyQt5 via PyPI for Python 2.7 64-bit on Windows
GNU General Public License v3.0
282 stars 79 forks source link

Running PyQT QWebEngineView application using docker without using host display #71

Open godomainz opened 2 years ago

godomainz commented 2 years ago

I have a Dockerfile like this

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
RUN adduser --quiet --disabled-password qtuser && usermod -a -G video qtuser
RUN apt-get update -y \
    && apt-get install alsa -y \
    && apt-get install libnss3 -y \
    && apt-get install -y python3-pyqt5 \
    && apt-get install python3-pip -y \
    && pip3 install pyqtwebengine
# RUN apt-get install xauth -y
# RUN apt-get install xvfb -y
# RUN apt-get install x11vnc -y
# RUN mkdir ~/.vnc
# RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Set display port and dbus env to avoid hanging
ENV DISPLAY=:99
ENV DBUS_SESSION_BUS_ADDRESS=/dev/null
USER qtuser
ENV HOME /home/qtuser
WORKDIR /htmltopdf

whenever I run this image I'm getting below error

qt.qpa.xcb: could not connect to display :99
    qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
    This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

I want to run this without using host support only way to run above image is using below command

docker run --rm -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v $(pwd):/htmltopdf --device=/dev/dri:/dev/dri htmltopdf:latest python3 htmlToPdfnew.py --url https://www.w3schools.com/howto/howto_css_register_form.asp

Is it possible to run PyQT without using these ??? -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --device=/dev/dri:/dev/dri

I tried everything in internet

installed xvfb, xorg,vnc

still no luck

this is my python code

import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse

def _fullScreenRequested(request):
    request.accept()
    loader.showFullScreen()

def main():
    url = ''
    parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--url", help="Type url")
    args = parser.parse_args()
    config = vars(args)
    url = config['url']

    app = QtWidgets.QApplication(sys.argv)
    loader = QtWebEngineWidgets.QWebEngineView()
    loader.setZoomFactor(1)
    layout = QPageLayout()
    layout.setPageSize(QPageSize(QPageSize.A4Extra))
    layout.setOrientation(QPageLayout.Portrait)
    loader.load(QUrl(url))
    loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())

    def emit_pdf(finished):
        QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))

    loader.loadFinished.connect(emit_pdf)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

So how can I run above code without using these ??

-v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --device=/dev/dri:/dev/dri