1. 程式人生 > 其它 >使用pdfkit生成網頁的pdf 異常彙總

使用pdfkit生成網頁的pdf 異常彙總

技術標籤:Pythonpythonxpdfvue.jshtml5java

使用pdfkit生成pdf

1. 採用Selenium、ChormeDriver和pdfkit生成網頁的pdf

2.使用pdfkit生成網頁的pdf 異常彙總(本文)

1、OSError: wkhtmltopdf reported an error

程式碼示例:

import pdfkit, time, pprint
from selenium import webdriver

options_chrome = webdriver.ChromeOptions()
# 以最高許可權執行
options_chrome.add_argument('--no-sandbox')
# 瀏覽器不提供視覺化頁面,linux下如果系統不支援視覺化不加這條會啟動失敗
options_chrome.add_argument('--headless')
# executable_path為chromedriver的位置
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', chrome_options=options_chrome)
# 瀏覽器全屏
driver.fullscreen_window()

url = 'http://www.tn666.com?type=1'
driver.get(url)
# sleep 1秒
time.sleep(1)
source_text = driver.page_source

options_pdf = {
    'page-size': 'A4'
}
result = pdfkit.from_string(source_text, 'test.pdf', options=options_pdf)

driver.quit()

詳細報錯資訊:

Traceback(mostrecentcalllast):File"pdfkit_selenium_test.py",line25,in<module>result=pdfkit.from_string(source_text,'/home/tn/code/python/test.pdf',options=options_pdf)File"/home/tn/anaconda3/envs/python3.6.6/lib/python3.6/site-packages/pdfkit/api.py",line72,infrom_stringreturnr.to_pdf(output_path)
File"/home/tn/anaconda3/envs/python3.6.6/lib/python3.6/site-packages/pdfkit/pdfkit.py",line156,into_pdfraiseIOError('wkhtmltopdfreportedanerror:\n'+stderr)OSError: wkhtmltopdf reported an error:

報錯原因:引用的外部資源載入不到

解決:獲取頁面原始碼後,外部資源增加host域名,使之可以載入,然後再生成pdf,比如:

source_text = source_text.replace('/static/js/index.js', 'http://www.tn666.com/static/js/index.js')

2、pdf生成異常:unknown error: DevToolsActivePort file doesn't exist

詳細報錯資訊:

(unknownerror:DevToolsActivePortfiledoesn'texist)(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

現象:

ps aux | grep google-chrome有幾十個程序

臨時解決辦法:

google-chrome程序只剩一個,其他的全部殺掉後,執行程式沒有問題了

Q&A:

每次執行程式都啟動一個新程序,如何指令碼結束後,自動把程序殺掉?

解決辦法:

程式碼中用的是 driver.quit(),先說一下driver.quit()和driver.close()的區別:

driver.close():只會關閉當前頁面

driver.quit():會退出驅動並且關閉所關聯的所有視窗

通過selenium.webdriver.chrome.service中的Service來控制ChromeDriver程序的生死

程式碼示例:

import pdfkit, time, pprint
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service_driver = Service('/usr/local/bin/chromedriver')
service_driver.command_line_args()
service_driver.start()

options_chrome = webdriver.ChromeOptions()
# 以最高許可權執行
options_chrome.add_argument('--no-sandbox')
# 瀏覽器不提供視覺化頁面,linux下如果系統不支援視覺化不加這條會啟動失敗
options_chrome.add_argument('--headless')

driver = webdriver.Chrome(chrome_options=options_chrome)
# 瀏覽器全屏
driver.fullscreen_window()

url = 'http://www.tn666.com?type=1'
driver.get(url)
# sleep 1秒
time.sleep(1)
source_text = driver.page_source

options_pdf = {
    'page-size': 'A4'
}
result = pdfkit.from_string(source_text, 'test.pdf', options=options_pdf)

driver.quit()
service_driver.stop()

3、頁面生成空白,帶loading狀態

現象:生成的pdf為帶loading狀態的空白頁

原因:頁面未加載出來時,生成了pdf

解決:優化頁面載入速度,或者driver.get後,time.sleep加長,再去獲取原始碼driver.page_source

4、頁面生成空白,完全空白頁

現象:生成的pdf為空白頁,把driver.page_source打印出來,再生成html頁面,也是空白頁

原因:其中的某些js可能會重置頁面,把頁面重置為空了

解決:逐個嘗試把js替換為空,看看頁面是否會加載出來

請將程式碼中的url換為您想轉為pdf的url

更多內容,請掃碼關注公眾號~