Skip to content

PDF Kit from_string not working #191

@mbrsagor

Description

@mbrsagor

def sendfile(filestring):
file_service = FileUploadService()
uploaded_file_path = file_service.upload_to_server(filestring, "invoice", "pdf")
if uploaded_file_path is not None:
return uploaded_file_path
return None

def generate_pdf(obj):
try:
data = obj
template = get_template('pdfkit.html')
html = template.render({'data': data})

    options = {
        'page-size': 'Letter',
        'encoding': "UTF-8",
    }
    config = pdfkit.configuration(wkhtmltopdf=bytes("/usr/local/bin/wkhtmltopdf", 'utf8'))
    pdf = pdfkit.from_string(html, False, options=options, configuration=config)
    print(f"PDF: {pdf}")
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment'
    response['Content-Disposition'] = 'filename="report.pdf"'

    bb = base64.b64encode(response.getvalue())
    file_path = sendfile(bb.decode('utf-8'))
    Log.general_log(f"file saved path-> {file_path}")
    return file_path
except Exception as ex:
    # logger
    print(f"EX: {ex}")
    Log.general_log(str(traceback.format_exc()))
    return None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions