0

I'm having an issue very similar to this post: pdfkit - python : 'str' object has no attribute decode

I'm running a python script via a web app.

import pdfkit after installing it with pip3, python version 3.6.

import pdfkit

def pdfkit(source, method):

if method == "string":

    try:
        options = {
            'page-size': 'A4',
            'margin-top': '0.75in',
            'margin-right': '0.75in',
            'margin-bottom': '0.75in',
            'margin-left': '0.75in',
        }
        config = pdfkit.configuration(wkhtmltopdf=bytes("/usr/local/bin/wkhtmltopdf", 'utf8'))
        pdf = pdfkit.from_string(source, False,options=options,configuration=config)
        return pdf

    except Exception as e:
        return str(e)
else:
    return "Error:  Not yet Supported"

I installed wkhtmltopdf following these instructions for UBUNTU 20.04. It says that these are "headless" and can be executed from the command line. In fact, it does when using the pdfkit wrapper, but when I try to run via the python script itself it doesn't work.

One of the errors that I am getting is:

{
   "pdf": "'function' object has no attribute 'configuration'"
}

among other, like the same for from_string if I remove the configuration.

Just wondering if I need to import some other modules or if I need a different version of wkhtmltopdf on the system.

Do I need to get a different binary, or follow the directions here. It is confusing because there are multiple ways to install that, the CLI, the .deb package and using the info on GitHub. Thanks.

wkhtmltopdf/packaging

wkhtmltopdf for UBUNTU

SScotti
  • 2,158
  • 4
  • 23
  • 41

1 Answers1

0

Thanks. Posting an Answer because I want to follow up and elaborate a little bit.

That was one problem, the function name (Duh !):

Still playing around with it a bit, but mostly working. I just started using Python. Pretty nice really. Wish I had discovered that sooner.

def getpdf(source, method):

if method == "string":

    try:
        options = {
            'page-size': 'A4',
            'margin-top': '0.75in',
            'margin-right': '0.75in',
            'margin-bottom': '0.75in',
            'margin-left': '0.75in',
        }
        config = pdfkit.configuration(wkhtmltopdf=bytes("/usr/local/bin/wkhtmltopdf", 'utf8'))
        pdf = pdfkit.from_string(source, False,options=options)
        return pdf

    except Exception as e:
        return str(e)
#       pdf = pdfkit.from_string(html, False)
#       return pdf;
else:
    return "Error:  Not yet Supported"

def HTMLTOPDF(output, uri, **request):

if request['method'] != 'POST':
    output.SendMethodNotAllowed('POST')
else:
    query = json.loads(request['body'])
    pdf = getpdf(query['html'],query['method'])
    print (pdf)
    encoded = base64.b64encode(pdf)
    response = dict();
    response['pdf'] = pdf
    output.AnswerBuffer(encoded, 'text/html')

That is mostly working now, and as it is, I've just returning the encoded results as base64, or so it seems since the reponse I get via the request looks like (JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/KQovQ3JlYXRvciAo), PDF I think. I am a little interested in returning the result as JSON instead because I'd like to pass something else back like:

{"base64":encoded","status":"","error":""}, something like that.

I tried something like this:

    encoded = base64.b64encode(pdf)
    response = dict();
    response['pdf'] = encoded
    response['status'] = "status"
    output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')

and I get an error:

Object of type 'bytes' is not JSON serializable

Thanks though. At least I can get raw base64 back, which might be easier really.

SScotti
  • 2,158
  • 4
  • 23
  • 41