-
网页转pdf,这个工具真好用(网页如何转pdf格式)
- 网站名称:网页转pdf,这个工具真好用(网页如何转pdf格式)
- 网站分类:技术文章
- 收录时间:2025-08-07 13:38
- 网站地址:
“网页转pdf,这个工具真好用(网页如何转pdf格式)” 网站介绍
前两天有个客户需要把网页转为pdf,之前也没开发过类似的工具,就在百度搜索了一波,主要有下面三种
- 在线转pdf
- 使用浏览器打印功能转pdf
- 使用本地软件工具转pdf
在线转pdf
在百度(我一般用必应)搜索“在线网页转pdf”就有很多可以做这个事的网站,免费的如
- PDF24Tools
各种pdf的操作都有,免费使用,速度一般。
官网地址
https://tools.pdf24.org/zh
- doctron
开源免费项目,使用golang写的,提供在线转
官网地址
http://doctron.lampnick.com/
还有挺多其他的,可以自己搜索,但是都不符合我的预期。
使用浏览器打印功能转pdf
- 在浏览器右键,点击打印或者ctrl+p
- 在弹出的打印对话框中找到目标打印机选择“另存为PDF”
- 点击“保存”按钮即可下载pdf了
使用本地软件工具转pdf
Doctron,这是我今天要介绍的重头戏。
Doctron是基于Docker、无状态、简单、快速、高质量的文档转换服务。目前支持将html转为pdf、图片(使用chrome(Chromium)浏览器内核,保证转换质量)。支持PDF添加水印。
- 使用chrome内核保证高质量将HTML转为pdf/图片。
- 简易部署(提供docker镜像,Dockerfile以及k8s yaml配置文件)。支持丰富的转换参数。转为pdf和图片支持自定义大小。
- 无状态服务支持。
管他的,先把代码下载下来再说
git clone https://gitcode.net/mirrors/lampnick/doctron.git
运行
go build
./doctron --config conf/default.yaml
转pdf,访问
http://127.0.0.1:8080/convert/html2pdf?u=doctron&p=lampnick&url=<url>,更换链接中的url为你需要转换的url即可。
然后就可以写程序去批量转换需要的网页了,但是我需要转换的网页有两个需求
1、网站需要会员登录,不然只能看得到一部分
2、需要把网站的头和尾去掉的
这就为难我了,不会go语言啊,硬着头皮搞了,肯定有个地方打开这个url的,就去代码慢慢找,慢慢调试,功夫不负有心人,终于找到调用的地方了。
第一步:添加网站用户登录cookie
第二步:去掉网站头尾
chromedp.Evaluate(`$('.header').css("display" , "none");
$('.btn-group').css("display" , "none");
$('.container .container:first').css("display" , "none");
$('.breadcrumb').css("display" , "none");
$('.footer').css("display" , "none")`, &ins.buf),
打开网页后执行js代码把头尾隐藏掉
第三步:程序化,批量自动生成pdf
public static void createPDF(String folder , String cl , String pdfFile, String urlhref) {
try {
String fileName = pdfFile.replace("/", ":");
String filePath = folder + fileName;
File srcFile = new File(filePath);
File newFolder = new File("/Volumes/disk2/myproject" + File.separator + cl);
File destFile = new File(newFolder, fileName);
if(destFile.exists()){
return;
}
if(srcFile.exists()){
//移动到对应目录
if(!newFolder.exists()){
newFolder.mkdirs();
}
FileUtils.moveFile(srcFile , destFile);
return;
}
if(!newFolder.exists()){
newFolder.mkdirs();
}
String url = "http://127.0.0.1:8888/convert/html2pdf?u=doctron&p=lampnick&url="+urlhref;
HttpEntity<String> entity = new HttpEntity<String>(null, null);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<byte[]> bytes = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
if (bytes.getBody().length <= 100) {
if(urlList.containsKey(urlhref)){
Integer failCount = urlList.get(urlhref);
if(failCount > 3){
System.out.println("下载失败:" + cl + " / " + pdfFile +" " + urlhref);
return;
}
failCount++;
urlList.put(urlhref , failCount);
}else{
urlList.put(urlhref , 1);
}
createPDF(folder , cl , pdfFile , urlhref);
}else{
if (!destFile.exists()) {
try {
destFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try (FileOutputStream out = new FileOutputStream(destFile);) {
out.write(bytes.getBody(), 0, bytes.getBody().length);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
最终成果:
更多相关网站
- 如何使用Java开发在线生成 pdf 文档 ?
- springboot 2整合websocket推送消息、数据流、解析pdf图片并压缩
- pdf,word,ppt,rar,mp4等等文档在线预览
- 7款口碑炸裂的电脑软件!一个比一个好用,Windows装机必备
- 办公小技巧:杜绝意外 让字体永不丢失
- 铅锤哥:处理PDF文件的神器——完美解密、压缩、转换格式等
- 对比工具大PK(对比工具怎么用)
- 好用的迅捷pdf转ppt转换器(迅捷pdf转换器怎么把pdf转成cad)
- 界面美观功能强大,终于可以告别单调的swagger ui了——knife4j
- PDF尺寸统计软件推荐(pdf尺寸测量工具)
- 超实用ISSUU文档下载教程(isas文档)
- 如何将PDF的某一页插入到WORD(如何把pdf文件中的某一页存出来)
- C#技术分享【PDF转换成图片——13种方案】
- 网络另类下载(网络另类下载网站)
- 这些宝藏免费PDF编辑网站,建议收藏起来!
- 使用requests库和urlretrieve下载pdf文件
- 速看!2021山东“专升本”招生计划出炉
- 相见恨晚:windows十款必装的逆天神器
- 最近发表
-
- 反恐防暴警钟鸣,校园安全保和谐(校园反恐防暴的宣传小知识)
- 增强防范意识,提高反恐能力——西丰县幼儿园举行反恐防暴演练
- .NET 使用 ZXing.Net 生成带有识别符的 EAN-13 条码
- 微软Windows 11隐藏彩蛋被发现(win11显示隐藏文件)
- Windows 10 Build 21327.1010发布:测试服务管道更新
- PPT图形高效绘制技巧全指南(含8种经典图形类型)
- 跨平台复制粘贴工具,SyncClipboard软件体验
- 还在为输入易经符号发愁?手把手教你轻松搞定!
- 再见 Calibri,你好 Aptos:微软新的默认字体
- 涨姿势!微软公司LOGO的进化史(微软logo设计师是谁)
- 标签列表
-
- mydisktest_v298 (35)
- sql 日期比较 (33)
- document.appendchild (35)
- 头像打包下载 (35)
- acmecadconverter_8.52绿色版 (25)
- 梦幻诛仙表情包 (36)
- java面试宝典2019pdf (26)
- disk++ (30)
- 加密与解密第四版pdf (29)
- iteye (26)
- centos7.4下载 (32)
- intouch2014r2sp1永久授权 (33)
- usb2.0-serial驱动下载 (24)
- jdk1.8.0_191下载 (27)
- axure9注册码 (30)
- virtualdrivemaster (26)
- 数据结构c语言版严蔚敏pdf (25)
- 兔兔工程量计算软件下载 (27)
- 代码整洁之道 pdf (26)
- ccproxy破解版 (31)
- aida64模板 (28)
- engine=innodb (33)
- shiro jwt (28)
- 方格子excel破解版补丁 (25)
- segoe ui是什么字体 (27)