Python 第三方模块之 pdfkit

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"""

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/453989.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LNMP环境添加第三方模块

一.在LNMP环境下添加memcache模块 1.安装依赖库(libevent) [rootnode1 ~]# tar xvf libevent-2.0.21-stable.tar.gz [rootnode1 ~]# cd libevent-2.0.21-stable [rootnode1 libevent-2.0.21-stable]# ./configure --prefix/usr/local/libevent [rootnode1 libevent-2.0.21-sta…

学生成绩管理系统-程序维护

托管平台地址&#xff1a;https://gitee.com/lucess/StudentMarkManage.git 小组名称:干翻沈师 程序运行方法: 1、后台服务&#xff1a;进入项目文件夹执行 python TeamProject.py runsercer 0.0.0.0:5050 2、前台服务&#xff1a;进入./WEB-INFO/TeamProjectWeb 文件夹执行 ya…

改需求

转载于:https://www.cnblogs.com/gw2010/p/7856484.html

架构师一般做到多少岁_软件测试可以做到多大岁数?

做这个行业也几年了&#xff0c;经常听到有人问&#xff0c;软件测试这个行业能干到多少岁&#xff0c;当然里边包含想要进入这个行业的和已经在这个行业里边发展的&#xff01;基本上软件测试可以分为三条职业发展路线&#xff1a;技术路线、管理路线、产品路线&#xff01;目…

Python 第三方模块之 MySQL数据库连接模块 PyMySQL

PyMySQL的安装 pip install PyMySQL python连接数据库 import pymysqlconn pymysql.connect(hostlocalhost, userroot, password"root",databasedb, port3306, # 数字3306charsetutf8, # 不是utf-8autocommitTrue # autocommitTrue 让每次提交都去调用…

初学Spring Boot

1.Spring Boot注解 (1)SpringBootApplication开启了Spring的组件扫描和Spring Boot的自动配置,实际上&#xff0c;SpringBootApplication是将三个注解组合在了一起&#xff0c;这三个注解分别是 SpringBootConfiguration&#xff0c;ComponentScan&#xff0c;Ena…

15条常用的视频音频编辑脚本命令(mencoder/ffmpeg等)

可以把它当快速简易参考看&#xff0c;主要的功能有&#xff1a; 视频格式转换音频格式转换切割视频及音频连接两段视频视频音频同步将图像系列转换成视频 这里是百鬼丸以前收集的一部分命令行视频音频编辑脚本命令&#xff0c;一直在自己的记事本里随时用&#xff0c;现在…

python rowcount_PyQt(Python+Qt)学习随笔:QTableWidget的currentItem、rowCount、columnCount等部件状态属性访问方法...

老猿将QTableWidget表格部件中反映部件当前情况的一些方法归类为部件状态访问方法&#xff0c;包括部件的行数、列数、当前项、当前行、当前列等属性访问方法。1、行数rowCountQTableWidget的rowCount属性保存表格部件中的行数&#xff0c;在QTableWidget创建时如果没有指定行数…

Python 内置模块之 random

常用API import random# 随机小数 print(random.random()) # 大于0且小于1之间的小数。0< n<1.0 print(random.uniform(1,3)) # 大于1小于3的小数# 随机整数 print(random.randint(1,5)) # 大于等于1且小于等于5之间的整数#从指定范围内&#xff0c;按指定基…

微信jssdk遇到的一些问题汇总

1.用户手动去触发的接口可以直接调用比如wx.startRecord(); 但是写在页面加载完成里就无效&#xff0c;需要写在 wx.ready(function(){wx.startRecord(); }); 才会有效。 2.h5 的audio标签只支持ogg,mp3,wav格式的音频&#xff0c;微信jssdk录制的是amr格式的语音文件&#xf…

mongodb简单的增删改查

数据库操作&#xff1a; show dbs;#查看数据库use test;#如果没有就创建一个db;#查看当前数据库db.dropDatabase();#删除数据库 数据操作&#xff1a;show collections&#xff1b;#查看集合创建集合、插入&#xff1a;create collection;#创建集合db.student.insert({"na…

ffmpeg-0.8 开源编码解码库从linux下移植到windows vs2005

最新 ffmpeg-0.8 开源编码解码库&#xff0c;从linux下移植到windows vs2005&#xff0c;全部开源。需要 Intel C Compile 和 开源的SDL库支持&#xff0c;由于 Intel C Compile支持C99语法&#xff0c;所以源代码改动很小很小。主要的修改1&#xff1a;添加了linux中有而windo…

python3.5.2使用教程_Python3.5.2-初级教程.docx

Python3.5.2-初级教程Python 初级教程Release:3.5.2引言Python 是一门简单易学且功能强大的编程语言。它拥有高效的高级数据结构&#xff0c;并且能够用简单而又高效的方式进行面向对象编程。Python 优雅的语法和动态类型&#xff0c;再结合它的解释性&#xff0c;使其在大多数…

Flask 离线脚本

1. 在 __init__.py中创建db对象from flask_sqlalchemy import SQLAlchemy# 包含了SQLAlchemy相关的所有操作db SQLAlchemy()2. 在 __init__.py中create_app函数中让将app传入到db中def create_app():app Flask(__name__)app.config.from_object(settings.DevelopmentConfig)f…

day13 迭代器和生成器

一、上节回顾和作业讲解&#xff1a; 1、如果这个网页没有被爬取过就真的去访问这个网页&#xff0c;否则就返回之前访问的时候缓存文件中的内容 &#xff08;重要的例子&#xff09; from urllib.request import urlopen def wrapper(func):def inner(*args, **kwargs):with o…

Centos7.0 搭建Zabbix环境

实验环境&#xff1a;Centos7.0IP:192.168.47.140关闭iptables及setenforce导入源 rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm安装zabbix包安装完成安装mysql源wget http://repo.mysql.com/mysql-community-release-el7-5…

用线性插值算法实现图像缩放

用线性插值算法实现图像缩放 猛禽[Mental Studio](个人专栏)(BLOG) http://mental.mentsu.com 在Windows中做过图像方面程序的人应该都知道Windows的GDI有一个API函数&#xff1a;StretchBlt&#xff0c;对应在VCL中是TCanvas类的StretchDraw方法。它可以很简单地实现图像的缩放…

蒙特卡洛分析 pmp_PMP基础名词介绍 | 59. 实施定量风险分析

点击上方蓝字关注我们你好&#xff0c;这是“兔子研习社”为管理新手推出的“PMP基础名词介绍”系列内容。如果你正打算转到管理岗位&#xff0c;或者想要学习国际通行的项目管理知识&#xff0c;那恭喜你&#xff0c;这里满满的干货会让你不虚此行。实施定量风险分析是就已识别…

深度学习案例之基于 CNN 的 MNIST 手写数字识别

一、模型结构 本文只涉及利用Tensorflow实现CNN的手写数字识别,CNN的内容请参考:卷积神经网络(CNN) MNIST数据集的格式与数据预处理代码input_data.py的讲解请参考 :Tutorial (2) 二、实验代码 # -*- coding:utf-8 -*- """Time : Author: Feng LepengFile …

怎样获取linux命令帮助?

获得命令使用帮助&#xff1a;内部命令&#xff1a;help COMMAND外部命令&#xff1a;COMMAND --help &#xff08;大多数命令有help选项&#xff09;命令手册&#xff1a;manualman [章节号] COMMAND其中man数据库是分章节的&#xff0c;相同的COMMAND出现在不同的章节表示…