test.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import asyncio
  2. from pyppeteer import launch
  3. async def main():
  4. # 异步代码
  5. browser = await launch({
  6. 'executablePath': 'C:\Program Files\Google\Chrome\Application',
  7. 'headless': True,
  8. 'args': ['--disable-infobars', '--no-sandbox']
  9. })
  10. page = await browser.newPage()
  11. await page.setViewport({
  12. 'width': 1200,
  13. 'height': 1697
  14. })
  15. await page.goto('http://8.136.199.33:7778/adminapi/share/rtq0vg06sE 5-16单人自由审批', {
  16. 'waitUntil': 'networkidle0',
  17. 'timeout': 3000000 # 设置超时时间为 100 秒
  18. })
  19. # 在生成PDF之前等待2秒
  20. await asyncio.sleep(15)
  21. await page.pdf({
  22. 'width': 1240,
  23. 'height': 1697,
  24. 'path': "./static/c4ca4238a0b923820dcc509a6f75849b_1200.pdf",
  25. 'printBackground': True,
  26. 'margin': {
  27. 'top': '20px',
  28. 'bottom': '20px',
  29. 'left': '20px',
  30. 'right': '20px'
  31. }
  32. })
  33. await browser.close()
  34. # 创建事件循环
  35. loop = asyncio.get_event_loop()
  36. # 使用事件循环运行main函数
  37. try:
  38. loop.run_until_complete(main())
  39. finally:
  40. # 关闭事件循环
  41. loop.close()