diff --git a/thinkhazard/views/pdf.py b/thinkhazard/views/pdf.py index 9f74647b..22f1a040 100644 --- a/thinkhazard/views/pdf.py +++ b/thinkhazard/views/pdf.py @@ -124,11 +124,12 @@ async def render_page(url): logger.info("Got: {}".format(url)) return BytesIO(await page.pdf({"format": "A4", "printBackground": True})) - chunks = await asyncio.gather(*[ - render_page(url) for url in pages - ]) - - await browser.close() + try: + chunks = await asyncio.gather(*[ + render_page(url) for url in pages + ]) + finally: + await browser.close() # merge all pages writer = PdfFileWriter()