问题描述
我使用 Wkhtmltopdf 库使用 python 从 html 文件创建 pdf。背景图像没有出现在 pdf 中。它在 html 中可见,但在 pdf 中不可见。我已经尝试从服务器提供图像路径也仍然无法正常工作。
wkhtmltopdf 版本 - wkhtmltopdf 0.12.6(带补丁的 qt)
HTML -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
margin: 0;
padding: 0;
height: 100%;
font-family: "Poppins Regular",sans-serif;
}
html {
min-height: 100% !important;
height: 100%;
}
.page-banner-area {
width: 100%;
background: url("D:/Automation/images/network-tel.png") !important;
/* background: url("http://localhost:8080/Automation/assets/images/equal.png") !important;*/
background-repeat: no-repeat;
height: 100%;
background-size: cover;
}
</style>
<title>TCMT</title>
<Meta charset="utf-8" />
<Meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
</head>
<body>
<div class="page-banner-area">
<div style="font-size: 50px;">Test BG Image</div>
</div>
</body>
</html>
Python 代码 -
import pdfkit
import sys
path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
options = {
"enable-local-file-access": None
}
argumentsLength = len(sys.argv) - 1
argumentList = sys.argv
print("This file is called with %i arguments" % (argumentsLength))
print(argumentList)
# remove first argument
argumentList.pop(0)
# argument list after removal of first element from list
print(argumentList)
lastItemInList = argumentList[-1]
print(lastItemInList)
del argumentList[-1]
pdfkit.from_file(
argumentList,lastItemInList,configuration=config,options=options),
创建 pdf 的命令 = python ./<python_file.py> <html_file.html> test.pdf
解决方法
如果您使用的是 Linux,请检查文件的所有权和权限。否则,您可以使用直接的静态图片网址。
我测试了下面的 ode 代码,它完美地解析了背景图像。
HTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
margin: 0;
padding: 0;
height: 100%;
font-family: "Poppins Regular",sans-serif;
}
html {
min-height: 100% !important;
height: 100%;
}
.page-banner-area {
width: 100%;
background: url("https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/-127wiki.jpg/1200px--127wiki.jpg") !important;
/* background: url("http://localhost:8080/Automation/assets/images/equal.png") !important;*/
background-repeat: no-repeat;
height: 100%;
background-size: cover;
}
</style>
<title>TCMT</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
</head>
<body>
<div class="page-banner-area">
<div style="font-size: 50px;">Test BG Image</div>
</div>
</body>
</html>
蟒蛇
import pdfkit
import sys
argumentsLength = len(sys.argv) - 1
argumentList = sys.argv
options = {
"enable-local-file-access": None
}
print("This file is called with %i arguments" % (argumentsLength))
print(argumentList)
# remove first argument
argumentList.pop(0)
# argument list after removal of first element from list
print(argumentList)
lastItemInList = argumentList[-1]
print(lastItemInList)
del argumentList[-1]
pdfkit.from_file(
argumentList,lastItemInList,options=options)
我稍微重构了代码以在我自己的机器上运行。
命令
python ./test2.py stackoverflow.html test.pdf
命令行输出
This file is called with 2 arguments
['./test2.py','stackoverflow.html','test.pdf']
['stackoverflow.html','test.pdf']
test.pdf
Loading page (1/2)
Printing pages (2/2)
Done
此外,我已经使用 this 指南设置了 pdfkit。