Рендеринг pdf локально

У меня нет доступа к S3 project bucket и я хотел бы сохранить html файл или pdf (будет лучше) на моей локальной машине, вместо S3 хранилища. Когда я генерирую pdf, я ловлю ValidationError. У меня есть эта функция для рендеринга pdf файла, есть идеи, как я могу отредактировать ее и сохранить локально?

def _render_pdf(storage, context, language_code, filename, template_path):
    tmp_html_file = tmp_pdf_file = None
    try:
        with tempfile.NamedTemporaryFile(
            delete=False, suffix='.html'
        ) as tmp_html_file:
            with translation.override(language_code):
                tmp_html_file.write(
                    str(
                        render_to_string(
                            template_path,
                            context
                        )
                    ).encode('utf-8')
                )
        tmp_pdf_file = tempfile.NamedTemporaryFile(
            delete=False, suffix='.pdf'
        )

        i = 0
        while i < settings.WKHTMLTOPDF_BINARY_RETRIES and os.path.getsize(tmp_pdf_file.name) == 0:
            i += 1
            generate_pdf_file(tmp_html_file.name, tmp_pdf_file.name)

        if os.path.getsize(tmp_pdf_file.name) == 0:
            raise ValidationError(u'Pdf file is empty: {}'.format(tmp_pdf_file), u'empty_pdf_file')

        storage.save(filename, tmp_pdf_file)
    finally:
        if tmp_html_file is not None:
            os.unlink(tmp_html_file.name)
        if tmp_pdf_file is not None:
            tmp_pdf_file.close()
            os.unlink(tmp_pdf_file.name)
Вернуться на верх