From 04ac2fd7c9732d7ec6f934250f441067b8c6d8d8 Mon Sep 17 00:00:00 2001 From: Adrien Brignon Date: Sat, 27 May 2023 20:10:36 +0200 Subject: [PATCH] fix: Windows-related issues --- mkdocs_exporter/browser.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mkdocs_exporter/browser.py b/mkdocs_exporter/browser.py index 5091d7f..47933f1 100644 --- a/mkdocs_exporter/browser.py +++ b/mkdocs_exporter/browser.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import asyncio from tempfile import NamedTemporaryFile @@ -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