python使用wkhtmltopdf将html字符串保存pdf,解决出现方框的问题

出现的问题:
解决办法:
html 复制代码
<html>
<head>
    <meta charset="UTF-8"/>
</head>
<style>
* {
    font-family:  Arial,SimSun !important;
}
</style>
</html>
在html字符串前面加上上面代码,意思是设置字体编码和样式
html示例:
html 复制代码
<!DOCTYPE html>
<html>
      <head>
    <meta charset="UTF-8"/>
<style>
* {
    font-family:  Arial,SimSun;
}
</style>
</head>
 <br><br>
<body>
<table style="border:1px #A8a8a8 solid; " border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#f4f4f4"> 
 <tbody> 
    <table border="0" cellpadding="0" cellspacing="0" width="100%"> 
     <tbody> 
      <tr> 
       <td class="buleB16" height="80" valign="middle" align="center">中国人民银行青岛市分行行政处罚信息公示表(2023年12月22日)</td> 
      </tr> 
     </tbody> 
  <br><br></body></html>
python代码示例:
python 复制代码
import pdfkit
html_str = """上述html代码"""
# 设置wkhtmltopdf路径
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
options = {
    'encoding': "utf-8",
}
pdfkit.from_string(html_str, 'output.pdf', configuration=config, options=options)
结果如下:
安装wkhtmltopd包参考:https://blog.csdn.net/qq_25647857/article/details/138248214
转pdf报错参考:http://kaito-kidd.com/2015/03/12/python-html2pdf/
相关推荐
Sirius.z2 小时前
第R6周:LSTM实现糖尿病探索与预测
python
名字还没想好☜2 小时前
Python f-string 进阶:数字格式化、对齐填充、调试 = 号与嵌套表达式
开发语言·数据库·python·字符串格式化·f-string
·薯条大王3 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
Dxy12393102164 小时前
Python 如何使用 MySQL 的事务
python·mysql
eric-sjq4 小时前
10 分钟实战:用 0.6B 本地模型生成中文网页(WanlyFrontend + 编译器全流程)
javascript·机器学习·自然语言处理·html
今儿敲了吗8 小时前
Python ——第三方包
笔记·python
麒麟水手8 小时前
国产银河麒麟系统开发实录:跑通Streamlit,我踩过的6个坑
python
麒麟水手8 小时前
麒麟系统部署检查清单:上线前30分钟,逐项自查这4类问题
python
用户0332126663679 小时前
使用 Python 在 PDF 中添加或删除数字签名
python
代码方舟10 小时前
零信任架构实战:基于天远柠檬查出险-登记证API构建自动化车辆履约评估网关
人工智能·python·架构·自动化