在docker中运行headless google chrome将html转换为pdf
我正在尝试将HTML转换为Docker容器中的PDF。 Dockerfile:
FROM python:3.8
# Adding trusting keys to apt for repositories
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# Adding Google Chrome to the repositories
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# Updating apt to see and install Google Chrome
RUN apt-get -y update
# Magic happens
RUN apt-get install -y google-chrome-stable
COPY /requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt
COPY . /app
CMD [ "python3", "app.py" ]
App.py
html_file_url="file:///html_file_name.html"
pdf_file_path="pdf_file_name.pdf"
commands = [
"google-chrome",
"--headless",
"--disable-gpu",
"--no-sandbox",
"--print-to-pdf={}".format(pdf_file_path),
html_file_url,
]
subprocess.run(commands)
运行我要获得的Docker文件时:
[0404/112836.835631:ERROR:bus.cc(397)] Failed to connect to the bus: Failed to connect to socket /host/run/dbus/system_bus_socket: No such file or directory
[0404/112836.835729:WARNING:bluez_dbus_manager.cc(248)] Floss manager not present, cannot set Floss enable/disable.
[0404/112836.835786:ERROR:bus.cc(397)] Failed to connect to the bus: Failed to connect to socket /host/run/dbus/system_bus_socket: No such file or directory
[0404/112836.866694:ERROR:sandbox_linux.cc(377)] InitializeSandbox() called with multiple threads in process gpu-process.
生成的PDF为空。 HTML格式包含诸如Flexbox之类的CSS功能,并且不会通过python软件包进行转换,例如XHTML2PDF,PDFKIT等,因此我正在尝试使用Google Chrome headless。
I'm trying to convert htmls to pdfs in a docker container.
Dockerfile:
FROM python:3.8
# Adding trusting keys to apt for repositories
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# Adding Google Chrome to the repositories
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# Updating apt to see and install Google Chrome
RUN apt-get -y update
# Magic happens
RUN apt-get install -y google-chrome-stable
COPY /requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt
COPY . /app
CMD [ "python3", "app.py" ]
app.py
html_file_url="file:///html_file_name.html"
pdf_file_path="pdf_file_name.pdf"
commands = [
"google-chrome",
"--headless",
"--disable-gpu",
"--no-sandbox",
"--print-to-pdf={}".format(pdf_file_path),
html_file_url,
]
subprocess.run(commands)
On running the docker file i'm getting:
[0404/112836.835631:ERROR:bus.cc(397)] Failed to connect to the bus: Failed to connect to socket /host/run/dbus/system_bus_socket: No such file or directory
[0404/112836.835729:WARNING:bluez_dbus_manager.cc(248)] Floss manager not present, cannot set Floss enable/disable.
[0404/112836.835786:ERROR:bus.cc(397)] Failed to connect to the bus: Failed to connect to socket /host/run/dbus/system_bus_socket: No such file or directory
[0404/112836.866694:ERROR:sandbox_linux.cc(377)] InitializeSandbox() called with multiple threads in process gpu-process.
The pdf generated is empty. The html format contains recent css features like flexbox and is not being convert via python packages like xhtml2pdf, pdfkit etc so I'm trying to use google chrome headless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我错了,请原谅我,但是他们不仅会在几个评论中丢失,因此在这里收集了有关OP问题提出的几个问题的相关答案的片段。
WGET https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
但是您应该研究自己当前的Google/debian版本状态,最新的.rpm或.deb可从 https://www.google.com/chrome/?platform=linux
“ - disable-gpu”
是Windows Systems 5年前的最后使用,不应在现代中不需要“ - 无头”
勇敢/铬/铬/边缘。- headless = new
有所不同
Forgive me If I am wrong but so they are not simply lost in several comments, here are collected snippets of related answers to several issues raised by OP question.
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
but you should research your own current google/debian version status, and latest .rpm or .deb is available from https://www.google.com/chrome/?platform=linux
"--disable-gpu"
was last used by windows systems 5 years ago and should not be needed in a modern"--headless"
Brave/Chrome/Chromium/Edge.--headless=new
由于可能需要一些其他二进制文件(,对于不同的Linux口味和镀铬版本)。
考虑查看并用作镀铬的一些(流行)图像,例如
dockerfile
这样,您可以从其他开源项目的结果中学习。
包括最好的想法是,最好的运行命令较少,而在结果图像中却更少。
Building docker image with Chrome may be tricky as some additional binaries may be required (and different for different linux flavors and Chrome versions).
Consider looking at and using as base some (popular) images with Chrome, e.g.
https://github.com/puppeteer/puppeteer/blob/main/docker/Dockerfile
This way you can learn from results of other open source projects.
Including the idea that it is better to have less RUN commands, and so less layers in the resulted image.