背景图像在 pdf Wkhtmltopdf 中不可见 python

Background Image is not visible in pdf Wkhtmltopdf with python

我使用 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,请检查文件的所有权和权限。否则,您可以使用直接静态图像网络 URL.

我测试了下面的代码,它完美地解决了背景图像。

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 Whosebug.html test.pdf

命令行输出

This file is called with 2 arguments
['./test2.py', 'Whosebug.html', 'test.pdf']
['Whosebug.html', 'test.pdf']
test.pdf
Loading page (1/2)
Printing pages (2/2)                                               
Done  

生成的 pdf 的屏幕截图

此外,我已经使用 this 指南设置了 pdfkit。