91无码高清视频在线播放|亚洲最大成人在线资源|日本黄色免费调教网站|亚洲春色一区二区三区|国产一级一区二区三区|成人免费 做受电影无码

歡迎訪(fǎng)問(wèn)漢海網(wǎng),帶你進(jìn)入知識(shí)的海洋!

網(wǎng)頁(yè)轉(zhuǎn)PDF的方法和工具推薦(網(wǎng)頁(yè)如何轉(zhuǎn)PDF格式的文件)

愛(ài)自由 分享 時(shí)間: 瀏覽:0

前兩天有個(gè)客戶(hù)需要把網(wǎng)頁(yè)轉(zhuǎn)為pdf,之前也沒(méi)開(kāi)發(fā)過(guò)類(lèi)似的工具,就在百度搜索了一波,主要有下面三種

    在線(xiàn)轉(zhuǎn)pdf使用瀏覽器打印功能轉(zhuǎn)pdf使用本地軟件工具轉(zhuǎn)pdf

在線(xiàn)轉(zhuǎn)pdf

在百度(我一般用必應(yīng))搜索“在線(xiàn)網(wǎng)頁(yè)轉(zhuǎn)pdf”就有很多可以做這個(gè)事的網(wǎng)站,免費(fèi)的如

PDF24Tools

各種pdf的操作都有,免費(fèi)使用,速度一般。

官網(wǎng)地址https://tools.pdf24.org/zh

PDF24 Tools

doctron

開(kāi)源免費(fèi)項(xiàng)目,使用golang寫(xiě)的,提供在線(xiàn)轉(zhuǎn)

官網(wǎng)地址http://doctron.lampnick.com/

doctron在線(xiàn)體驗(yàn)demo

還有挺多其他的,可以自己搜索,但是都不符合我的預(yù)期。

使用瀏覽器打印功能轉(zhuǎn)pdf

    在瀏覽器右鍵,點(diǎn)擊打印或者ctrl+p在彈出的打印對(duì)話(huà)框中找到目標(biāo)打印機(jī)選擇“另存為PDF”點(diǎn)擊“保存”按鈕即可下載pdf了

使用本地軟件工具轉(zhuǎn)pdf

Doctron,這是我今天要介紹的重頭戲。

Doctron是基于Docker、無(wú)狀態(tài)、簡(jiǎn)單、快速、高質(zhì)量的文檔轉(zhuǎn)換服務(wù)。目前支持將html轉(zhuǎn)為pdf、圖片(使用chrome(Chromium)瀏覽器內(nèi)核,保證轉(zhuǎn)換質(zhì)量)。支持PDF添加水印。

使用chrome內(nèi)核保證高質(zhì)量將HTML轉(zhuǎn)為pdf/圖片。簡(jiǎn)易部署(提供docker鏡像,Dockerfile以及k8s yaml配置文件)。支持豐富的轉(zhuǎn)換參數(shù)。轉(zhuǎn)為pdf和圖片支持自定義大小。無(wú)狀態(tài)服務(wù)支持。

管他的,先把代碼下載下來(lái)再說(shuō)

git clone https://gitcode.net/mirrors/lampnick/doctron.git

倉(cāng)庫(kù)

運(yùn)行

go build./doctron --config conf/default.yaml

運(yùn)行截圖

轉(zhuǎn)pdf,訪(fǎng)問(wèn)http://127.0.0.1:8080/convert/html2pdf?u=doctron&p=lampnick&url=,更換鏈接中的url為你需要轉(zhuǎn)換的url即可。

轉(zhuǎn)換效果

然后就可以寫(xiě)程序去批量轉(zhuǎn)換需要的網(wǎng)頁(yè)了,但是我需要轉(zhuǎn)換的網(wǎng)頁(yè)有兩個(gè)需求

1、網(wǎng)站需要會(huì)員登錄,不然只能看得到一部分

2、需要把網(wǎng)站的頭和尾去掉的

這就為難我了,不會(huì)go語(yǔ)言啊,硬著頭皮搞了,肯定有個(gè)地方打開(kāi)這個(gè)url的,就去代碼慢慢找,慢慢調(diào)試,功夫不負(fù)有心人,終于找到調(diào)用的地方了。

第一步:添加網(wǎng)站用戶(hù)登錄cookie

添加cookie之前

添加cookie之后

第二步:去掉網(wǎng)站頭尾

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),

打開(kāi)網(wǎng)頁(yè)后執(zhí)行js代碼把頭尾隱藏掉

第三步:程序化,批量自動(dòng)生成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()){//移動(dòng)到對(duì)應(yīng)目錄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 entity = new HttpEntity(null, null);RestTemplate restTemplate = new RestTemplate();ResponseEntity 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();}}

最終成果:

文件夾分類(lèi)存放

pdf文件

本站部分文章來(lái)自網(wǎng)絡(luò)或用戶(hù)投稿。涉及到的言論觀點(diǎn)不代表本站立場(chǎng)。閱讀前請(qǐng)查看【免責(zé)聲明】發(fā)布者:愛(ài)自由,如若本篇文章侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。本文鏈接:http://www.gdyuanyu.cn/dnxx/dnjq/132167.html

221381