Docker容器内安装wkhtmltox用来html转pdf
生活随笔
收集整理的這篇文章主要介紹了
Docker容器内安装wkhtmltox用来html转pdf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明:運行一個openjdk的基礎容器(在空白基礎鏡像內安裝后,指定程序,如springboot程序打包成docker鏡像并運行使用哈),并進入docker容器內部,操作安裝,建議看完全文再操作哈,坑是一點點踩的。
啟動openjdk容器命令如下:
0、需要將apt-get升級:apt-get update
root@c0802ee02685:/data# apt-get update Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB] Get:2 http://deb.debian.org/debian bullseye InRelease [116 kB] Get:3 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [118 kB] Get:4 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB] Get:5 http://deb.debian.org/debian bullseye/main amd64 Packages [8183 kB] Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [2596 B] Fetched 8503 kB in 12min 50s (11.0 kB/s) Reading package lists... Done1、下載wkhtmltox_0.12.6-1.buster_amd64.deb
root@wdy-service-7744995878-lqdjw:/data# wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb --2022-02-11 16:14:28-- https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb Resolving github.com (github.com)... 20.205.243.166 Connecting to github.com (github.com)|20.205.243.166|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/131323182/5696a480-ab83-11ea-8f1c-bcb4433414a0?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220211%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220211T081406Z&X-Amz-Expires=300&X-Amz-Signature=8118c09853d12d045e00d5f91fd74f03abd905384aaf14834933f5b01590a282&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=131323182&response-content-disposition=attachment%3B%20filename%3Dwkhtmltox_0.12.6-1.buster_amd64.deb&response-content-type=application%2Foctet-stream [following] --2022-02-11 16:14:28-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/131323182/5696a480-ab83-11ea-8f1c-bcb4433414a0?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220211%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220211T081406Z&X-Amz-Expires=300&X-Amz-Signature=8118c09853d12d045e00d5f91fd74f03abd905384aaf14834933f5b01590a282&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=131323182&response-content-disposition=attachment%3B%20filename%3Dwkhtmltox_0.12.6-1.buster_amd64.deb&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.110.133, 185.199.109.133, 185.199.111.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 17007914 (16M) [application/octet-stream] Saving to: ‘wkhtmltox_0.12.6-1.buster_amd64.deb’wkhtmltox_0.12.6-1.buster_amd64.deb 100%[============================================================================================================================================================>] 16.22M 19.9KB/s in 11m 21s 2022-02-11 16:25:52 (24.4 KB/s) - ‘wkhtmltox_0.12.6-1.buster_amd64.deb.1’ saved [17007914/17007914]root@wdy-service-7744995878-lqdjw:/data# ls wkhtmltox_0.12.6-1.buster_amd64.deb2、下載完成后,安裝wkhtmltox_0.12.6-1.buster_amd64.deb
dpkg -i wkhtmltox_0.12.6-1.buster_amd64.deb
root@wdy-service-7744995878-lqdjw:/data# dpkg -i wkhtmltox_0.12.6-1.buster_amd64.deb Selecting previously unselected package wkhtmltox. (Reading database ... 14656 files and directories currently installed.) Preparing to unpack wkhtmltox_0.12.6-1.buster_amd64.deb ... Unpacking wkhtmltox (1:0.12.6-1.buster) ... dpkg: dependency problems prevent configuration of wkhtmltox:wkhtmltox depends on libjpeg62-turbo; however:Package libjpeg62-turbo is not installed.wkhtmltox depends on libx11-6; however:Package libx11-6 is not installed.wkhtmltox depends on libxcb1; however:Package libxcb1 is not installed.wkhtmltox depends on libxext6; however:Package libxext6 is not installed.wkhtmltox depends on libxrender1; however:Package libxrender1 is not installed.wkhtmltox depends on xfonts-75dpi; however:Package xfonts-75dpi is not installed.wkhtmltox depends on xfonts-base; however:Package xfonts-base is not installed.dpkg: error processing package wkhtmltox (--install):dependency problems - leaving unconfigured Errors were encountered while processing:wkhtmltox3、上述提示缺少依賴,故安裝之:
root@wdy-service-7744995878-lqdjw:/data# apt-get -f -y install Reading package lists... Done Building dependency tree... Done Reading state information... Done Correcting dependencies... Done The following additional packages will be installed:libfontenc1 libjpeg62-turbo libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxrender1 x11-common xfonts-75dpi xfonts-base xfonts-encodings xfonts-utils Suggested packages:xfs | xserver The following NEW packages will be installed:libfontenc1 libjpeg62-turbo libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxrender1 x11-common xfonts-75dpi xfonts-base xfonts-encodings xfonts-utils 0 upgraded, 14 newly installed, 0 to remove and 15 not upgraded. 1 not fully installed or removed. Need to get 11.7 MB of archives. After this operation, 17.1 MB of additional disk space will be used. Get:1 http://deb.debian.org/debian bullseye/main amd64 libjpeg62-turbo amd64 1:2.0.6-4 [151 kB] Get:2 http://deb.debian.org/debian bullseye/main amd64 libxau6 amd64 1:1.0.9-1 [19.7 kB] Get:3 http://deb.debian.org/debian bullseye/main amd64 libxdmcp6 amd64 1:1.1.2-3 [26.3 kB] Get:4 http://deb.debian.org/debian bullseye/main amd64 libxcb1 amd64 1.14-3 [140 kB] Get:5 http://deb.debian.org/debian bullseye/main amd64 libx11-data all 2:1.7.2-1 [311 kB] Get:6 http://deb.debian.org/debian bullseye/main amd64 libx11-6 amd64 2:1.7.2-1 [772 kB] Get:7 http://deb.debian.org/debian bullseye/main amd64 libxext6 amd64 2:1.3.3-1.1 [52.7 kB] Get:8 http://deb.debian.org/debian bullseye/main amd64 libxrender1 amd64 1:0.9.10-1 [33.0 kB] Get:9 http://deb.debian.org/debian bullseye/main amd64 libfontenc1 amd64 1:1.1.4-1 [24.3 kB] Get:10 http://deb.debian.org/debian bullseye/main amd64 x11-common all 1:7.7+22 [252 kB] Get:11 http://deb.debian.org/debian bullseye/main amd64 xfonts-encodings all 1:1.0.4-2.1 [573 kB] Get:12 http://deb.debian.org/debian bullseye/main amd64 xfonts-utils amd64 1:7.7+6 [93.0 kB] Get:13 http://deb.debian.org/debian bullseye/main amd64 xfonts-75dpi all 1:1.0.4+nmu1.1 [3365 kB] Get:14 http://deb.debian.org/debian bullseye/main amd64 xfonts-base all 1:1.0.5 [5897 kB] Fetched 11.7 MB in 12min 41s (15.4 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package libjpeg62-turbo:amd64. (Reading database ... 14678 files and directories currently installed.) Preparing to unpack .../00-libjpeg62-turbo_1%3a2.0.6-4_amd64.deb ... Unpacking libjpeg62-turbo:amd64 (1:2.0.6-4) ... Selecting previously unselected package libxau6:amd64. Preparing to unpack .../01-libxau6_1%3a1.0.9-1_amd64.deb ... Unpacking libxau6:amd64 (1:1.0.9-1) ... Selecting previously unselected package libxdmcp6:amd64. Preparing to unpack .../02-libxdmcp6_1%3a1.1.2-3_amd64.deb ... Unpacking libxdmcp6:amd64 (1:1.1.2-3) ... Selecting previously unselected package libxcb1:amd64. Preparing to unpack .../03-libxcb1_1.14-3_amd64.deb ... Unpacking libxcb1:amd64 (1.14-3) ... Selecting previously unselected package libx11-data. Preparing to unpack .../04-libx11-data_2%3a1.7.2-1_all.deb ... Unpacking libx11-data (2:1.7.2-1) ... Selecting previously unselected package libx11-6:amd64. Preparing to unpack .../05-libx11-6_2%3a1.7.2-1_amd64.deb ... Unpacking libx11-6:amd64 (2:1.7.2-1) ... Selecting previously unselected package libxext6:amd64. Preparing to unpack .../06-libxext6_2%3a1.3.3-1.1_amd64.deb ... Unpacking libxext6:amd64 (2:1.3.3-1.1) ... Selecting previously unselected package libxrender1:amd64. Preparing to unpack .../07-libxrender1_1%3a0.9.10-1_amd64.deb ... Unpacking libxrender1:amd64 (1:0.9.10-1) ... Selecting previously unselected package libfontenc1:amd64. Preparing to unpack .../08-libfontenc1_1%3a1.1.4-1_amd64.deb ... Unpacking libfontenc1:amd64 (1:1.1.4-1) ... Selecting previously unselected package x11-common. Preparing to unpack .../09-x11-common_1%3a7.7+22_all.deb ... Unpacking x11-common (1:7.7+22) ... Selecting previously unselected package xfonts-encodings. Preparing to unpack .../10-xfonts-encodings_1%3a1.0.4-2.1_all.deb ... Unpacking xfonts-encodings (1:1.0.4-2.1) ... Selecting previously unselected package xfonts-utils. Preparing to unpack .../11-xfonts-utils_1%3a7.7+6_amd64.deb ... Unpacking xfonts-utils (1:7.7+6) ... Selecting previously unselected package xfonts-75dpi. Preparing to unpack .../12-xfonts-75dpi_1%3a1.0.4+nmu1.1_all.deb ... Unpacking xfonts-75dpi (1:1.0.4+nmu1.1) ... Selecting previously unselected package xfonts-base. Preparing to unpack .../13-xfonts-base_1%3a1.0.5_all.deb ... Unpacking xfonts-base (1:1.0.5) ... Setting up libxau6:amd64 (1:1.0.9-1) ... Setting up libxdmcp6:amd64 (1:1.1.2-3) ... Setting up libxcb1:amd64 (1.14-3) ... Setting up x11-common (1:7.7+22) ... debconf: unable to initialize frontend: Dialog debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.) debconf: falling back to frontend: Readline invoke-rc.d: could not determine current runlevel invoke-rc.d: policy-rc.d denied execution of start. Setting up libfontenc1:amd64 (1:1.1.4-1) ... Setting up libjpeg62-turbo:amd64 (1:2.0.6-4) ... Setting up libx11-data (2:1.7.2-1) ... Setting up xfonts-encodings (1:1.0.4-2.1) ... Setting up libx11-6:amd64 (2:1.7.2-1) ... Setting up libxrender1:amd64 (1:0.9.10-1) ... Setting up libxext6:amd64 (2:1.3.3-1.1) ... Setting up xfonts-utils (1:7.7+6) ... Setting up xfonts-base (1:1.0.5) ... Setting up xfonts-75dpi (1:1.0.4+nmu1.1) ... Setting up wkhtmltox (1:0.12.6-1.buster) ... Processing triggers for libc-bin (2.31-13+deb11u2) ... Processing triggers for fontconfig (2.13.1-4.2) ...4、安裝好依賴后,重新安裝 wkhtmltox_0.12.6-1.buster_amd64.deb
root@c0802ee02685:/data# dpkg -i wkhtmltox_0.12.6-1.buster_amd64.deb (Reading database ... 13929 files and directories currently installed.) Preparing to unpack wkhtmltox_0.12.6-1.buster_amd64.deb ... Unpacking wkhtmltox (1:0.12.6-1.buster) over (1:0.12.6-1.buster) ... Setting up wkhtmltox (1:0.12.6-1.buster) ...安裝后,發現/usr/local/bin 目錄下已經有了所需工具
root@c0802ee02685:/data# cd /usr/local/bin/ root@c0802ee02685:/usr/local/bin# ls docker-java-home wkhtmltoimage wkhtmltopdf經過測試ok。
打包dokcer鏡像
注意:(本機用的話,去掉reg.wangdy.com:8443/k8snb/)
# 打包 docker commit -a "wangdy" c0802ee02685 reg.wangdy.com:8443/k8snb/openjdk:wangdy8wkhtml2pdfnew # 推送到私有harbor倉庫 docker push reg.wangdy.com:8443/k8snb/openjdk:wangdy8wkhtml2pdfnew5、一些用到的其他命令:
docker 將文件從 本機 傳輸到 docker容器內
# 把當前路徑下的simsun.ttc文件傳輸到容器(id)7d36669b3e44 的/data/html2pdf目錄下 docker cp simsun.ttc 7d36669b3e44:/data/html2pdf/docker 將文件從 docker容器內 傳輸 到本機
# 將容器(id)7d36669b3e44的/data/html2pdf/S2.pdf的文件傳輸到當前目錄 docker cp 7d36669b3e44:/data/html2pdf/S2.pdf .6、Debian系統中安裝字體(解決中文亂碼)
依次執行以下命令:
mkdir /usr/share/fonts/myfont
mv simsun.ttc /usr/share/fonts/myfont
chmod 644 /usr/share/fonts/myfont/*
cd /usr/share/fonts/myfont
mkfontdir
fc-cache -fv
全過程如下:
然后就可以基于該鏡像,打包程序了
如下Dockerfile(注意:本機用的話,去掉reg.wangdy.com:8443/k8snb/)
建議: 將容器內用過的安裝包,測試文件等無用文件刪除,減少鏡像大小。
END
總結
以上是生活随笔為你收集整理的Docker容器内安装wkhtmltox用来html转pdf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二十年后的回眸(3)——快乐的单身汉
- 下一篇: java生成docx文件、pdf文件、d