diff --git a/Dockerfile b/Dockerfile index 8625472..750e14c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* ENV WORKING_DIR=/opt/weasyprint -ENV CHROME_EXECUTABLE_PATH=/usr/bin/chromium +ENV CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium ENV WEASYPRINT_SERVICE_VERSION=$APP_IMAGE_VERSION WORKDIR ${WORKING_DIR} diff --git a/app/SvgUtils.py b/app/SvgUtils.py index cd3ea4a..baa709b 100644 --- a/app/SvgUtils.py +++ b/app/SvgUtils.py @@ -50,12 +50,11 @@ def replace_img_base64(match): return f' 100_000: @@ -89,22 +88,18 @@ def replace_svg_with_png(svg_content): f.write(svg_content) f.close() - # Feed svg file to chrome + # Feed svg file to chromium png_filepath = os.path.join(temp_folder, uuid + '.png') - result = subprocess.run([ - f'{chrome_executable}', - '--headless=old', - '--no-sandbox', - '--disable-gpu', - '--disable-software-rasterizer', - '--disable-dev-shm-usage', - '--default-background-color=00000000', - '--hide-scrollbars', - '--enable-features=ConversionMeasurement,AttributionReportingCrossAppWeb', - f'--screenshot={png_filepath}', - f'--window-size={width},{height}', - f'{svg_filepath}', - ]) + + chromium_command = create_chromium_command( + chromium_executable, + height, + width, + png_filepath, + svg_filepath, + ) + + result = subprocess.run(chromium_command) # Remove tmp svg file os.remove(svg_filepath) @@ -122,3 +117,32 @@ def replace_svg_with_png(svg_content): os.remove(png_filepath) return png_base64 + + +def create_chromium_command(chromium_executable, height, width, png_filepath, svg_filepath): + # Check if the ENABLE_HARDWARE_ACCELERATION environment variable is set to true + enable_hardware_acceleration = os.getenv('ENABLE_HARDWARE_ACCELERATION', 'false').lower() == 'true' + + command = [ + f'{chromium_executable}', + ] + + if not enable_hardware_acceleration: + command.extend([ + '--disable-gpu', + '--disable-software-rasterizer', + '--disable-dev-shm-usage', + ]) + + command.extend([ + '--headless=old', # because of issue in new with SVG conversion + '--no-sandbox', + '--default-background-color=00000000', + '--hide-scrollbars', + '--enable-features=ConversionMeasurement,AttributionReportingCrossAppWeb', + f'--screenshot={png_filepath}', + f'--window-size={width},{height}', + f'{svg_filepath}', + ]) + + return command diff --git a/entrypoint.sh b/entrypoint.sh index a803896..cc5247c 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -4,7 +4,9 @@ if ! pgrep -x 'dbus-daemon' > /dev/null; then if [ -f /run/dbus/pid ]; then rm /run/dbus/pid fi - dbus-daemon --system --fork; + dbus_session_bus_address_filename="/tmp/dbus_session_bus_address"; + dbus-daemon --system --fork --print-address > ${dbus_session_bus_address_filename}; + export DBUS_SESSION_BUS_ADDRESS=$(cat ${dbus_session_bus_address_filename}) fi python app/WeasyprintServiceApplication.py &