html2pdf
selenium-python
Selenium是一种开源工具,用于在Web浏览器上执行手动化测试(使用任何Web浏览器进行Web应用程序测试)。
Selenium通过使用WebDriver支持市场上所有主流浏览器的手动化。Webdriver是一个API和合同,它定义了一个语言中立的插口linux应用程序开发 pdflinux命令tar,用于控制web浏览器的行为。每位浏览器都有一个特定的WebDriver实现,称为驱动程序。驱动程序是负责指派给浏览器的组件,并处理与Selenium和浏览器之间的通讯。
Selenium不仅测试功能外linux应用程序开发 pdf红旗 linux,我们还可以借助它的浏览器功能实现html转pdf
selenium驱动
# selenium 驱动
https://selenium-python.readthedocs.io/installation.html#drivers
https://selenium-python.readthedocs.io/api.html
主要依赖
pip install selenium
测试代码
import os
import sys
import base64
from selenium import webdriver
platform = 'win' if sys.platform == 'win32' else 'linux'
current = os.path.abspath(os.path.dirname(__file__))
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument("--start-maximized")
# options.add_argument('--disable-gpu')
browser = webdriver.Firefox(options=options)
browser.get(
'file://{}'.format(os.path.join(current, '..', 'index.html'))
)
data = browser.print_page()
buffer = base64.b64decode(data)
version = sys.version_info
file = os.path.join(
current, 'python{}{}{}_{}.pdf'.format(
version.major,
version.minor,
version.micro,
platform
)
)
with open(file, 'wb') as fd:
fd.write(buffer)
browser.quit()
疗效预览
selenium/python379_win.pdf·yjihrp/linux-html2pdf-demo-
selenium/python3818_linux.pdf·yjihrp/linux-html2pdf-demo-
测试结果
测试结果
下一篇7-LINUXHTML转PDF-weasyprint