Why is Tornado freezing when executing pdfkit.from_url() command?

307 Views Asked by At

I'm running a python tornado server and I'm trying to create pdfs of reports on my site. The function I've written to handle this is below:

def __generate_pdf(self, list_params):
    result = []

    try:
        try:
            print "Deleting old report PDF..."
            os.remove('report.pdf')
        except Exception as e:
            pass

        options = {
            'load-error-handling':  'ignore',
            'page-width':       str(list_params[8]) + 'in',
            'page-height':      str(list_params[9]) + 'in'
        }

        report_url = 'http://127.0.0.1/report.html'
        for i in range(1, 10):
            report_url += {True: '?', False: '&'}[i == 1]
            report_url += 'p' + str(i) + '=' + {True: '', False: str(list_params[i])}[list_params[i] is None]

        print "Generating PDF of: " + report_url
        print "Command: pdfkit.from_url(url='"+ report_url +"', output_path='report.pdf', options="+ str(options) +")"
        result.append(report_url)

        if pdfkit.from_url(url=report_url, output_path='report.pdf', options=options):
            return result
        else:
            self.errors = True
            print 'Failed to create PDF'
            self.message = 'Failed to create PDF'
            return result

    except Exception as e:
        self.errors = True
        print 'Failed to create PDF, ' + str(e)
        self.message = 'Failed to create PDF, ' + str(e)
        return result

The problem I'm having is that Tornado seems to lock up upon executing the pdfkit.from_url() command... I'm using an AJAX request to fire off this function and the browser never receives a response from the server once the AJAX request is made and the server won't respond to any other requests afterwards. I'm 99% certain that it isn't anything to do with the command itself as I can copy/paste the command that prints out to the console into a python terminal on the server and it executes just fine.

0

There are 0 best solutions below