網(wǎng)站首頁 編程語言 正文
一、html網(wǎng)頁文件轉(zhuǎn)pdf
#將HTML文件導出為PDF def html_to_pdf(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltopdf.exe'): ''' 將HTML文件導出為PDF :param html_path:str類型,目標HTML文件的路徑,可以是一個路徑,也可以是多個路徑,以list方式傳入路徑;或者一個或者多個網(wǎng)址;或者為一個字符串 :param pdf_path:str類型,需要導出的PDF文件的路徑 :param html_encoding:str類型,html的編碼格式,具體要看html頁面到底是以什么編碼格式保存的 :param path_wkpdf:str類型,path_wkpdf = r'.\Tools\wkhtmltopdf.exe' # 工具路徑 :return: ''' cfg = pdfkit.configuration(wkhtmltopdf=path_wkpdf) options = { "encoding": html_encoding # 這個具體要看html頁面到底是以什么編碼格式保存的 } if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)): #判斷是否為非網(wǎng)址 #從url獲取html,再轉(zhuǎn)為pdf print('http=>pdf') # pdfkit.from_url('https://httpbin.org/ip', 'ip.pdf', options=options, configuration=cfg) # pdfkit.from_url(['https://httpbin.org/ip', 'https://httpbin.org/ip'], 'ip.pdf', options=options,configuration=cfg) # 傳入列表 pdfkit.from_url(html_path, pdf_path, options=options, configuration=cfg) elif 'html' in str(html_path) or 'HTML' in str(html_path): #判斷是否為HTML文件 #將html文件轉(zhuǎn)為pdf print('html,str=>pdf') # pdfkit.from_file(r'./helloworld.html', 'helloworld.pdf',options=options, configuration=cfg) pdfkit.from_file(html_path, pdf_path, options=options, configuration=cfg) elif isinstance(html_path, list) and ('html' in str(html_path) or 'HTML' in str(html_path)): #判斷html目標是否為list, # 如:[r'./helloworld.html', r'./111.html', r'./222.html'] print('html,list=>pdf') pdfkit.from_file(html_path, pdf_path,options=options, configuration=cfg) # 傳入列表 else: #將字符串轉(zhuǎn)為pdf print('from_string=>pdf') pdfkit.from_string(html_path, pdf_path,options=options, configuration=cfg)
所需要用的附件程序:
wkhtmltopdf.exe
二、html網(wǎng)頁文件轉(zhuǎn)png
#將HTML文件導出為圖片 def html_to_png(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltoimage.exe'): ''' 將HTML文件導出為圖片 :param html_path:str類型,目標HTML文件的路徑,可以是一個路徑,也可以是多個路徑,以list方式傳入路徑;或者一個或者多個網(wǎng)址;或者為一個字符串 :param pdf_path:str類型,需要導出的圖片文件的路徑 :param html_encoding:str類型,html的編碼格式,具體要看html頁面到底是以什么編碼格式保存的 :param path_wkpdf:str類型,path_wkpdf = r'.\Tools\wwkhtmltoimage.exe' # 工具路徑 :return: ''' cfg = imgkit.config(wkhtmltoimage=path_wkpdf) options = { "encoding": html_encoding # 這個具體要看html頁面到底是以什么編碼格式保存的 } if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)): #判斷是否為非網(wǎng)址 #從url獲取html,再轉(zhuǎn)為pdf print('http=>png') # pdfkit.from_url('https://httpbin.org/ip', 'ip.png', options=options, configuration=cfg) # pdfkit.from_url(['https://httpbin.org/ip', 'https://httpbin.org/ip'], 'ip.png', options=options,configuration=cfg) # 傳入列表 imgkit.from_url(html_path, pdf_path, options=options, config=cfg) elif 'html' in str(html_path) or 'HTML' in str(html_path): #判斷是否為HTML文件 #將html文件轉(zhuǎn)為pdf print('html,str=>png') # pdfkit.from_file(r'./helloworld.html', 'helloworld.png',options=options, configuration=cfg) imgkit.from_file(html_path, pdf_path, options=options, config=cfg) elif isinstance(html_path, list) and ('html' in str(html_path) or 'HTML' in str(html_path)): #判斷html目標是否為list, # 如:[r'./helloworld.html', r'./111.html', r'./222.html'] print('html,list=>png') imgkit.from_file(html_path, pdf_path,options=options, config=cfg) # 傳入列表 else: #將字符串轉(zhuǎn)為pdf print('from_string=>png') imgkit.from_string(html_path, pdf_path,options=options, config=cfg)
所需要用的附件程序:
wkhtmltoimage.exe
原文鏈接:https://blog.csdn.net/zh6526157/article/details/122430864
相關(guān)推薦
- 2022-08-10 Python多任務(wù)版靜態(tài)Web服務(wù)器實現(xiàn)示例_python
- 2023-11-25 全局后置路由守衛(wèi)(afterEach)
- 2024-07-13 SpringBoot入門(解決JDK8不存在問題)
- 2022-05-05 C語言的三種條件判斷語句你都了解嗎_C 語言
- 2022-11-02 react組件中過渡動畫的問題解決_React
- 2022-02-18 Redis - Redis command timed out nested exception i
- 2022-02-17 在react中使用antv g2繪制帶有sider滑塊的chart圖表
- 2023-01-27 Python中np.argmax()函數(shù)用法示例_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支