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/
相关推荐
云程笔记2 分钟前
004.环境搭建基础篇:Python、CUDA、cuDNN、PyTorch/TensorFlow安装与版本兼容性踩坑
pytorch·python·tensorflow
知行合一。。。5 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y5 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange6 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium276 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499996 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉6 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi6 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^7 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool
宝贝儿好7 小时前
【LLM】第二章:文本表示:词袋模型、小案例:基于文本的推荐系统(酒店推荐)
人工智能·python·深度学习·神经网络·自然语言处理·机器人·语音识别