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/
相关推荐
曲幽4 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户8356290780519 小时前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞12 小时前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派14 小时前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪16 小时前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户03321266636716 小时前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫17 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派18 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风20 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽1 天前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic