1

I'm using

Django=3.1
pyppeteer=0.2.5 #for printing page as PDF

I don't know where is the problem here, because it has worked fine before.
I'm using new ability of Django for making async response. Maybe here is the problem?

the code for printing PDF

async def generate_pdf(request, pk=None, blueprint=''):
    browser = await launch(
        executablePath='/usr/bin/chromium-browser',
        headless=True,
        args=['--no-sandbox'],
        handleSIGINT=False,
        handleSIGTERM=False,
        handleSIGHUP=False
    )
    page = await browser.newPage()
    url = 'https://{}/reports/{}/{}/'.format(request.get_host(), pk, blueprint)
    await page.goto(url, {'waitUntil': 'networkidle0', 'timeout': 60 * 1000})
    pdf_file = await page.pdf({
        "width": "210mm",
        "height": "297mm",
        "printBackground": True,
    })
    await browser.close()

    response = HttpResponse(content_type='application/pdf')
    report_name = await get_report_name(pk)
    response['Content-Disposition'] = 'attachment; filename="{report_name}.pdf"'.format(
        report_name=escape_uri_path(report_name)
    )
    response.write(pdf_file)
    return response

uwsgi log after executing the code above

unhandled exception during loop shutdown
task: <Task finished coro=<WebSocketCommonProtocol.transfer_data() done, defined at /var/www/evaluation360/env/lib/python3.6/site-packages/websockets/protocol.py:818> exception=CancelledError()>
Traceback (most recent call last):
  File "/var/www/evaluation360/env/lib/python3.6/site-packages/websockets/protocol.py", line 700, in close
    loop=self.loop if sys.version_info[:2] < (3, 8) else None,
  File "/usr/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/var/www/evaluation360/env/lib/python3.6/site-packages/websockets/protocol.py", line 1169, in close_connection
    await self.transfer_data_task
concurrent.futures._base.CancelledError
[pid: 1788|app: 0|req: 7/17] 109.63.253.81 () {44 vars in 1002 bytes} [Thu Jul 29 20:00:12 2021] GET /api/v1/reports/617dd17b-217a-4489-aa5f-c7f5568a569b/pdf/test/ => generated 887 bytes in 7326 msecs (HTTP/1.1 200) 8 headers in 537 bytes (2 switches on core 0)
Ian
  • 43
  • 1
  • 7

0 Answers0