Skip to content

Commit

Permalink
fix: Windows-related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbrignon committed May 27, 2023
1 parent 0474b0f commit 04ac2fd
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions mkdocs_exporter/browser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import asyncio

from tempfile import NamedTemporaryFile
Expand Down Expand Up @@ -73,16 +74,21 @@ async def print(self, html: str) -> bytes:
"""Prints some HTML to PDF."""

page = await self.context.new_page()
file = NamedTemporaryFile(suffix='.html', mode='w+', encoding='utf-8', delete=False)

with NamedTemporaryFile(suffix='.html', mode='w+', encoding='utf-8') as file:
file.write(html)
file.flush()
file.write(html)
file.close()

await page.goto('file://' + file.name, wait_until='networkidle')
await page.locator('.pagedjs_pages').wait_for(timeout=30000)
await page.goto('file://' + file.name, wait_until='networkidle')
await page.locator('.pagedjs_pages').wait_for(timeout=30000)

pdf = await page.pdf(prefer_css_page_size=True, print_background=True, display_header_footer=False)

try:
os.unlink(file)
except Exception:
pass

await page.close()

return pdf

0 comments on commit 04ac2fd

Please sign in to comment.