python - 背景图像在带有python的pdf Wkhtmltopdf中不可见
问题描述
我使用 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>
Python
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
另外,我已经使用本指南设置了 pdfkit。
推荐阅读
- node.js - GET 请求在 Postman 中有效,但在使用请求包的 NodeJS 中无效
- python - ftputil 中的 FTPHost.walk 连接到 FTPS 服务器不返回任何内容
- python - Networkx 中基于国家/地区份额的节点大小
- javascript - 为什么节点包含方法未捕获对 svg 元素的单击?
- python - 如何获取高于和低于0 python的每个周期的最大值和最小值
- c# - 如何从列表中删除所有具有重复项的值?
- java - Android 中的 MediaStore.Video.Media.DISPLAY_NAME 在哪里?
- serverless - 调用 API 时无服务器本地离线给出错误
- javascript - 如何使用 react-test-renderer 或其他替代库呈现纯 html 代码?
- python - 使用openCV从图像中提取图形数据