pdfkit,把 HTML+CSS 格式的文件转换成 PDF 格式文档的一个工具。

其实,pdfkit 是 html 转成 pdf 工具包 wkhtmltopdf 的 Python 封装。所以,首先安装 wkhtmltopdf 。 一般情况下,wkhtmltopdf需要手动安装,网站是 https://wkhtmltopdf.org/downloads.html,根据自己的操作系统下载对应的版本即可。ps:记住安装目录啊,下面要用到。

上面说到了pdfkit这个模块,这个是第三方模块,需要安装,使用pip安装即可。

pip install pdfkit

示例

pdfkit 可以将网页、html文件以及字符串生成pdf文件

import pdfkitconfg = pdfkit.configuration(wkhtmltopdf='C:\Python35\wkhtmltopdf.exe')# 这里指定一下wkhtmltopdf的路径,这就是我为啥在前面让记住这个路径
url = 'https://blog.csdn.net/fenglepeng/article/details/103670893'
pdfkit.from_url(url, 'aaa.pdf', configuration=confg)
# from_url这个函数是从url里面获取内容
# 这有3个参数,第一个是url,第二个是文件名,第三个就是khtmltopdf的路径pdfkit.from_file('my.html', 'bbb.pdf', configuration=confg)
# from_file这个函数是从文件里面获取内容
# 这有3个参数,第一个是一个html文件,第二个是文生成的pdf的名字,第三个就是khtmltopdf的路径html = '''
<div>
<h1>title</h1>
<p>content</p>
</div>
'''
pdfkit.from_string(html, 'ccc.pdf', configuration=confg)
# from_file这个函数是从一个字符串里面获取内容
# 这有3个参数,第一个是一个字符串,第二个是文生成的pdf的名字,第三个就是khtmltopdf的路径

API

def from_url(url, output_path, options=None, toc=None, cover=None, configuration=None, cover_first=False): """ 把从URL获取文件转换为PDF文件 :param url: URL 或 URL列表 :param output_path: 输出PDF文件的路径。如果是参数等于False,意味着文件将会以字符串的形式返回,得到文本文件。:param options: (可选) dict with wkhtmltopdf global and page options, with or w/o '--' :param toc: (可选) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (可选) string with url/filename with a cover html page :param configuration: (可选)实例化 pdfkit.configuration.Configuration() :param configuration_first: (可选) if True, cover always precedes TOC Returns:成功返回True """ def from_file(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False): """ Convert HTML file or files to PDF document :param input: path to HTML file or list with paths or file-like object  :param output_path: path to output PDF file. False means file will be returned as string. :param options: (optional) dict with wkhtmltopdf options, with or w/o '--':param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (optional) string with url/filename with a cover html page :param css: (optional) string with path to css file which will be added to a single input file :param configuration: (optional) instance of pdfkit.configuration.Configuration() :param configuration_first: (optional) if True, cover always precedes TOC Returns: True on success """ def from_string(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False):"""Convert given string or strings to PDF document:param input: string with a desired text. Could be a raw text or a html file:param output_path: path to output PDF file. False means file will be returned as string.:param options: (optional) dict with wkhtmltopdf options, with or w/o '--':param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--':param cover: (optional) string with url/filename with a cover html page:param css: (optional) string with path to css file which will be added to a input string:param configuration: (optional) instance of pdfkit.configuration.Configuration():param configuration_first: (optional) if True, cover always precedes TOCReturns: True on success"""

Python 第三方模块之 pdfkit相关推荐

  1. mac如何导入python第三方库_Mac系统中python idle导入第三方模块成功,ecplise导入python第三方模块失败解决方法...

    遇到一个比较纠结了4个月的问题,一直没有在意,今天实在忍受不了,尝试各种解决办法,终于把这个烦人的问题完美解决,不敢独享,写出来和各位大神共享. 问题:在mac OSx操作系统下,安装了python第 ...

  2. python第三方模块安装路径_Python第三方Window模块文件的几种安装方法

    python安装第三方模块 使用软件管理工具pip python自带了包管理工具,就像手机app商城,91助手等软件的功能一样. python2与python3安装模块的方法相似,值得注意的是,你在p ...

  3. Python: 第三方模块(modules)的安装位置 (2014-08-24 23:19:18)转载▼ 标签: site-packages dist-packages 默认安装目录 分类: Pyth

    Python: 第三方模块(modules)的安装位置 在默认情况下,Python的module的默认安装目录是 /usr/local/lib/pythonX.Y/site-packages.例如,我 ...

  4. Python第三方模块:PyQt5简介

    CSDN话题挑战赛第1期 活动详情地址:https://marketing.csdn.net/p/bb5081d88a77db8d6ef45bb7b6ef3d7f 参赛话题:Python精彩第三方模块 ...

  5. 因修改了用户文件夹名而无法使用pip安装python第三方模块的两种实用解决方法

    在之前因为把user文件夹里的中文名修改了,同时记得也要把电脑的环境变量修改过来,这个是前提! 然而,环境变量修改也无法使用pip安装python第三方模块,我在这里提供两种实用方法! 下面以安装关于 ...

  6. python第三方模块—psutil模块

    系统基础信息采集模块作为监控模块的重要组成部分,能够帮助运维人员了解当前系统的健康程度,同时也是衡量业务的服务质量的依据,比如系统资源吃紧,会直接影响业务的服务质量及用户体验,另外获取设备的流量信息, ...

  7. Python 第三方模块 统计1 statsmodels模块1 简介,回归

    官方文档:https://www.statsmodels.org/stable/user-guide.html\quadhttps://www.statsmodels.org/stable/api.h ...

  8. Python~第三方模块

    第三方库还有MySQL的驱动:MySQL-python,用于科学计算的NumPy库:numpy,用于生成文本的模板工具Jinja2 模块搜索路径 Windows下: 双\\   sys.path.ap ...

  9. Python 第三方模块之 beautifulsoup(bs4)- 解析 HTML

    简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下:官网文档 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索. ...

最新文章

  1. java 设计一个geometricobject类,geometricobject类
  2. 百度网络监控实战:NetRadar横空出世(上)
  3. 【Java8】@FunctionalInterface
  4. oracle大于当前月,oracle 现阶段年 当前月 当前周 当前日
  5. java 无侵入监控_MyPerf4J 一个高性能、无侵入的Java性能监控和统计工具
  6. docker mysql主从_使用docker 实现MySQL主从同步/读写分离
  7. c语言 算术平均滤波法_单片机数字滤波的算法
  8. VS2013(InstallShield2015LimitedEdition)打包程序详解
  9. 命令行请求网站地址带token_利用gitlab或gitee作为网站免费图床的C#实现
  10. 在C#中将集合用作键
  11. Jsp+Servlet+Mysql实现的在线鲜花商城源码
  12. 东航期货行情接口和交易接口(20190509)
  13. 知易游戏开发教程cocos2d-x移植版
  14. 新浪自媒体重新开放注册   一点资讯全面布局?
  15. 9.企业安全建设入门(基于开源软件打造企业网络安全) --- SOC系统建设
  16. php mud游戏源码,mud 文字游戏 - 源码下载|游戏|源代码 - 源码中国
  17. 无线局域网安全协议(WEP、WPA、WAPI)
  18. 串口调试助手、网络调试助手
  19. BetaFlight开源代码框架简介
  20. java web web.xml配置_web.xml配置详解

热门文章

  1. 制动刹车片六个养护要点
  2. Maven Helper 安装使用(jar 包管理工具)
  3. 【git】----- clone 及上传文件
  4. 快速解决 GRADLE 项目下载 gradle-*-all.zip 慢的问题
  5. 记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
  6. Linux inode与文件系统关系
  7. oracle 中的trunc()函数及加一个月,一天,一小时,一分钟,一秒钟方法
  8. hdu 1176 馅饼
  9. offsetTop测试见解
  10. LINQ学习笔记(9) LINQ to Objects---查询内存中对象的集合