当前位置:网站首页>强烈推荐这款神器,一行命令将网页转PDF!
强烈推荐这款神器,一行命令将网页转PDF!
2022-06-11 03:37:00 【Python 技术】

文 | 闲欢
来源:Python 技术「ID: pythonall」

在日常工作生活中,我们常常会遇到一个需求:将网页保存为PDF。
比如,我比较喜欢某个公众号的文章,为了防止文章被删除,我想把这个公众号的历史文章保存为 PDF 文件,放到本地硬盘里,随时可以拿出来看。
我之前使用的方法是浏览器自带的功能,使用快捷键 Ctrl + P 或者在浏览器上右键点击页面,然后选择“打印”选项 ,调出浏览器打印功能。

接着在“打印机”选项中选择“另存为PDF”,就可以输出 PDF 文件了。
这一套流程耗时有点长,偶尔操作个别页面是可以的,如果页面多了之后就会比较焦躁。
今天无意中发现一个好用的工具,它只需要一行命令就可以实现网页转 PDF 文件保存。
这款神器就是——wkhtmltopdf!
安装
要使用 wkhtmltopdf ,需要先安装这个软件。
wkhtmltopdf 的官网地址是:https://wkhtmltopdf.org/
在官网的 Downloads 导航下,我们可以看到这款软件支持多种操作系统,选择你正在使用的操作系统对应的安装文件即可。

我这里下载的是 windows 版本,下载完成之后,直接安装。安装过程比较简单,总共才几步,直接默认配置即可。
使用
网页转 PDF
软件安装完成之后,就可以使用了。
这里以百度首页为例,展示如何将百度首页保存为 PDF 文件。
我们打开终端,找到安装路径下的 bin 目录,我的电脑路径是这样的:

然后输入命令:
.\wkhtmltopdf.exe https://baidu.com C:\baidu.pdf
运行这个命令,等待运行完成后,你就可以在指定的目录下找到 baidu.pdf 文件了,我这里是 C 盘根目录下。

这里不限于网站上的网页,本地的 html 文件也是可以的,你只需要把 https://baidu.com 替换成你本地的网页路径就行。
这里,需要进入到软件的安装目录下才可以运行命令,比较麻烦。你可以将软件的 bin 目录添加到环境变量中,添加完成后就可以在终端中即开即用了。
网页转图片
wkhtmltopdf 不仅可以将网页转 PDF 文件保存,还可以将网页转为图片保存,只需要使用另一个命令即可:
.\wkhtmltoimage.exe https://baidu.com C:\baidu.png
同样地,等待命令运行完毕,你就可以在对应目录下找到 baidu.png 的图片了。

总结
wkhtmltopdf 的使用非常简单,甚至于官网对其使用方法也是几句话介绍。基于这个工具,我们也许可以做更多的事情,比如调用这个脚本命令,批量将爬虫爬取下来的网页转为 PDF 或者图片保存,这个有待各位去探索和使用!
PS:公号内回复「Python」即可进入Python 新手学习交流群,一起 100 天计划!
老规矩,兄弟们还记得么,右下角的 “在看” 点一下,如果感觉文章内容不错的话,记得分享朋友圈让更多的人知道!

【神秘礼包获取方式】
识别文末二维码,回复:1024

边栏推荐
- 难忘时刻
- [elt.zip] openharmony paper Club - Data high-throughput lossless compression scheme
- 如何做编程知识投资及减少知识失效的影响
- Le tri le plus couramment utilisé de JS - - série JS déchiquetée à la main
- [dataset] | UAV Perspective
- VNC remote configuration of Galaxy Kirin server system
- three. JS cool technology background H5 animation
- SSL interaction process
- How to improve the efficiency of regression testing
- PMM monitoring Oracle
猜你喜欢

代码复现CSRF攻击并解决它

Lvgl Chinese font production

OpenGL Chapter 11 multiple light sources

Promise use

The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack

regular expression

Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?

SQL query users logged in for three consecutive days

/The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter

开源项目 英雄联盟 之WPF
随机推荐
Thoughts on the number of threads and CPU caused by the CPU load high alarm
Path count 2 (DP + number of combinations)
Azure Kubernates Service 更新|提升开发体验和效率
SSL library selection
Implementation of publish and subscribe mode ----- hand tearing JS series
[interpretable] | axiomatic attribute for deep networks
Manual testing cannot be changed to automated testing. What is missing?
[elt.zip] openharmony paper Club - fast random access string compression
Product milestones in May 2022
Notes on redisson distributed lock usage
Quartz: an old and robust open source task scheduling framework, which is smooth enough to use
C. Jump and Treasure(dp + 单调队列优化)
Nsthread of the multithreaded Trilogy
WPF of open source project hero alliance
SSL interaction process
Course classification tree structure display
[dataset] | UAV Perspective
RHEL7 切换字符编码为GBK
Object storage Minio tutorial
Le tri le plus couramment utilisé de JS - - série JS déchiquetée à la main