当前位置: 首页 > article >正文

python生成图片和pdf,快速

1、下载安装

pip install imgkit
pip install pdfkit

2、wkhtmltopdf工具包,下载安装
下载地址:https://wkhtmltopdf.org/downloads.html
在这里插入图片描述
3、生成图片

import imgkit
path_wkimg = r'D:\app\wkhtmltopdf\bin\wkhtmltoimage.exe'  # 工具路径,安装的路径
cfg = imgkit.config(wkhtmltoimage=path_wkimg)
# 1、将html文件转为图片
options = {'encoding': 'utf8'} # 解决乱码
imgkit.from_file(r'temp.html', 'helloworld.jpg', config=cfg, options=options)

# 2、从url获取html,再转为图片
# imgkit.from_url('https://httpbin.org/ip', 'ip.jpg', config=cfg, options=options)
# 3、将字符串转为图片
# imgkit.from_string('Hello!', 'hello.jpg', config=cfg)

4、转为pdf

import pdfkit
 
 
path_wkpdf = r'D:\app\wkhtmltopdf\bin\wkhtmltopdf.exe'  # 工具路径
cfg = pdfkit.configuration(wkhtmltopdf=path_wkpdf)
 
# 1、将html文件转为pdf
pdfkit.from_file(r'./helloworld.html', 'helloworld.pdf', configuration=cfg)
# 传入列表
pdfkit.from_file([r'./helloworld.html', r'./111.html', r'./222.html'], 'helloworld.pdf', configuration=cfg)
 
# 2、从url获取html,再转为pdf
pdfkit.from_url('https://httpbin.org/ip', 'ip.pdf', configuration=cfg)
# 传入列表
pdfkit.from_url(['https://httpbin.org/ip','https://httpbin.org/ip'], 'ip.pdf', configuration=cfg)
 
# 3、将字符串转为pdf
pdfkit.from_string('Hello!','hello.pdf', configuration=cfg)

5、a4纸张大小

import imgkit
path_wkimg = r'D:\app\wkhtmltopdf\bin\wkhtmltoimage.exe'  # 工具路径
cfg = imgkit.config(wkhtmltoimage=path_wkimg)
# # 1、将html文件转为图片
# A4纸张大小
# options = {
#     'encoding': 'utf8', # 解决乱码
#     'width': 599,
#     'height': 845
# }
options = {
    'encoding': 'utf8',
    'width': 599,
    # 'height': 845
}
imgkit.from_file(r'temp.html', 'helloworld.jpg', config=cfg, options=options)
<!DOCTYPE html>
<html>
<head>
    <title>打印</title>
</head>
<body style="margin: 5px">
    <!--<div style="width: 599px;height: 845px">-->
    <div style="width: 599px;height: 100%">
        <div>大得</div>
        <div>大得</div>
    </div>
</body>
</html>

5、a4纸生成图片

import imgkit
path_wkimg = r'D:\app\wkhtmltopdf\bin\wkhtmltoimage.exe'  # 工具路径
cfg = imgkit.config(wkhtmltoimage=path_wkimg)
# # 1、将html文件转为图片
# A4纸张大小
# options = {
#     'encoding': 'utf8', # 解决乱码
#     'width': 599,
#     'height': 845
# }
options = {
    'encoding': 'utf8',
    'width': 599,
    'height': 845
}
imgkit.from_file(r'temp.html', 'helloworld.jpg', config=cfg, options=options)
<!DOCTYPE html>
<html>
<head>
    <title>打印</title>
    <style>
        body {
            margin: 5px;
        }

        .container {
            width: 599px;
            height: 100%;
        }

        .row {
            height: 300px;
        }

        .row div {
            /* 允许在单词内部换行,确保长字符串能正常换行 */
            word-wrap: break-word;
            word-break: break-all;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="row">
            <div>大得6666666666669999999999999999999999999999999999999999999999999999999999999999999999977777777777777778888888888888</div>
            <div>大得</div>
        </div>
        <div class="row">
            <div>大得</div>
            <div>大得</div>
        </div>
    </div>
</body>

</html>

在这里插入图片描述
6、模糊问题,使用quality,zoom解决

import imgkit
path_wkimg = r'D:\app\wkhtmltopdf\bin\wkhtmltoimage.exe'  # 工具路径
cfg = imgkit.config(wkhtmltoimage=path_wkimg)
# # 1、将html文件转为图片
# A4纸张大小
options = {
    'encoding': 'utf8',   # 乱码
    'width': 599,
    'enable-local-file-access': '',  # 允许访问本地文件
    'quality': 100,     # 提高分辨率,100最高
    'zoom': 4       # 增加缩放比例
}
imgkit.from_file(r'temp.html', 'helloworld.jpg', config=cfg, options=options)

<!DOCTYPE html>
<html>
<head>
    <title>打印</title>
</head>
<body style="margin: 5px">
    <div style="width: 599px;height: 100%">
        <div style="height: 300px;">
            <div>大得666666666666999999999999999999999</div>
            <div>大得</div>
        </div>
        <div style="height: 300px;">
            <div>大得</div>
            <div>大得</div>
        </div>
    </div>
</body>
</html>


http://www.kler.cn/a/518345.html

相关文章:

  • 【Uniapp-Vue3】图片lazy-load懒加载
  • Alfresco Content Services docker自动化部署操作
  • flatten-maven-plugin 统一版本管理插件
  • 大厂案例——腾讯蓝鲸DevOps类应用的设计与实践
  • Unity URP 获取/设置 Light-Indirect Multiplier
  • 考研机试题:打印日期
  • 健康AI应用的逆袭:如何用“死亡时钟”撬动用户增长和媒体关注,实现应用榜快速排名第六
  • 【数据结构】_不带头非循环单向链表
  • 安全扫描Django项目解决存在敏感信息常见问题
  • redis主从集群中的哨兵机制
  • 探索 Web3 技术:如何推动数字身份的自主管理
  • 第4章 神经网络【1】——损失函数
  • css-设置元素的溢出行为为可见overflow: visible;
  • SpringBoot集成Flink-CDC,实现对数据库数据的监听
  • 解锁罗技键盘新技能:轻松锁定功能键(罗技K580)
  • NFT Insider #166:Nifty Island 推出 AI Agent Playground;Ronin 推出1000万美元资助计划
  • jQuery阶段总结(二维表+思维导图)
  • Vue 3 30天精进之旅:Day 03 - Vue实例
  • Linux(Centos、Ubuntu) 系统安装jenkins服务
  • Windows中如何查看Java进程对应的进程ID(PID),以及如何kill进程详解