Skip to content

Commit

Permalink
fix: enable hardware acceleration using env variable (#68)
Browse files Browse the repository at this point in the history
Refs: #62
  • Loading branch information
grigoriev authored Sep 19, 2024
1 parent 269ee1c commit 58c8198
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
66 changes: 45 additions & 21 deletions app/SvgUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ def replace_img_base64(match):
return f'<img{match.group("intermediate")}image/svg+xml;base64, {replaced_content_base64}"'


# Checks that base64 encoded content is a svg image and replaces it with the png screenshot made by chrome
# Checks that base64 encoded content is a svg image and replaces it with the png screenshot made by chromium
def replace_svg_with_png(svg_content):

chrome_executable = os.environ.get('CHROME_EXECUTABLE_PATH')
if not chrome_executable:
logging.error('CHROME_EXECUTABLE_PATH not set')
chromium_executable = os.environ.get('CHROMIUM_EXECUTABLE_PATH')
if not chromium_executable:
logging.error('CHROMIUM_EXECUTABLE_PATH not set')
return svg_content

# Fetch width & height from root svg tag
Expand All @@ -72,7 +71,7 @@ def replace_svg_with_png(svg_content):
else:
logging.error('Cannot find svg height in ' + svg_content)
return svg_content

# Log large svg content size
svg_content_length = len(svg_content)
if svg_content_length > 100_000:
Expand All @@ -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)
Expand All @@ -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
4 changes: 3 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down

0 comments on commit 58c8198

Please sign in to comment.