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

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 pdfkitpath_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.lryc.cn/news/526110.html

相关文章:

  • 剑指Offer|LCR 044.在每个树行中找最大值
  • PWM信号概述
  • 关于BAR(PCIE BAR或AXI BAR)的解释
  • 计算机的错误计算(二百二十一)
  • 【力扣Hot 100】矩阵1
  • 移动端VR处理器和传统显卡的不同
  • 「 机器人 」利用数据驱动模型替代仿真器:加速策略训练并降低硬件依赖
  • MATLAB 如何避免复杂shp文件对inpolygon的影响
  • 【2024年华为OD机试】 (C卷,200分)- 贪吃的猴子(JavaScriptJava PythonC/C++)
  • PostgreSQL中级专家是什么意思?
  • 从根源分析,调试,定位和解决MacOS ld: unsupported tapi file type ‘!tapi-tbd‘ in YAML file
  • 【Uniapp-Vue3】previewImage图片预览
  • doris:Insert Into Values
  • 15 分布式锁和分布式session
  • 迅为RK3568开发板篇OpenHarmony实操HDF驱动控制LED-添加内核编译
  • C语言练习(23)
  • LabVIEW 太阳能光伏发电系统智能监控
  • 大唐杯赛道一国一备赛思路
  • 用户中心项目教程(五)---MyBatis-Plus完成后端初始化+测试方法
  • 深圳市云盟智慧科技有限公司智慧停车管理系统 SQL注入漏洞复现(附脚本)
  • PySide(PyQT)进行SQLite数据库编辑和前端展示的基本操作
  • 利用 SAM2 模型探测卫星图像中的农田边界
  • 前端路由的hash模式和history模式
  • 日志收集Day005
  • 代码随想录 二叉树 test 2
  • 浏览器默认语言与页面访问统计问题二三则
  • 用Python绘制一只懒羊羊
  • 虹科分享 | 汽车NVH小课堂之听音辨故障
  • 论文速读|SigLIP:Sigmoid Loss for Language Image Pre-Training.ICCV23
  • 深度学习笔记——循环神经网络之LSTM