1

I'm tried to solve the following problem using pdfkit.

when I add wkhtmltopdf configuration to my app then my app can't load into the browser even the index page cant load into the browser. but when I remove wkhtmltopdf configuration from my app then my all routes work fine only the HTML to pdf route gives me the following error.

OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

Configuration:

config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
pdfkit.from_url('http://127.0.0.1:5000/leave_worker_pdf', 'output.pdf', configuration=config)

route.py code:

@app.route('/leave_worker_pdf', methods=['POST'])
def leave_worker_pdf():
    leave_worker = LeaveWorker.query.all()
    html = render_template("leave_worker_pdf.html", leave_worker=leave_worker)
    pdf = pdfkit.from_string(html, False)
    response = make_response(pdf)
    response.headers["Content-Type"] = "application/pdf"
    response.headers["Content-Disposition"] = "inline; filename=output.pdf"
    return response

HTML markup:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
</head>
<body>
  <div class="container">

  
    <h1 class="text-center mt-4">Leave Workers Details</h1>
  
    <table class="table mt-4">
        <thead class="table-dark">
          <tr>
            <th scope="col">Id</th>
            <th scope="col">Name</th>
            <th scope="col">Address</th>
            <th scope="col">Contact</th>
            <th scope="col">LeaveDate</th>
            <th scope="col">Admin</th>
          </tr>
        </thead>
        {% for worker in leave_worker %}
        <tbody>
          <tr>
            <td> {{ worker.id }}</td>
            <td> {{ worker.leave_worker_name }} </td>
            <td> {{ worker.leave_worker_address }} </td>
            <td> {{ worker.leave_worker_contact }} </td>
            <td> {{ worker.leave_date.strftime('%Y-%m-%d') }} </td>
            <td> {{ worker.admin.username }} </td>
          </tr>
        </tbody>
        {% endfor %}
    </table>
  
  </div>
</body>
</html>

When I add wkhtmltopdf configuration the page can't load into the browser.

When I add wkhtmltopdf configuration the page can't load into the browser

Even the homepage cant load into the browser.

index page

when I remove wkhtmltopdf configuration from my app then my all routes work fine only when i click on the pdf then its give in error.

enter image description here

Error:

enter image description here

ijaz Bacha
  • 49
  • 8
  • Where have you put those "Configuration" lines? – Ali Tou Mar 01 '21 at 07:43
  • @AliTou I put into the top of the routes.py file – ijaz Bacha Mar 01 '21 at 08:01
  • I think there is some kind of deadlock here. Why would you want to call `pdfkit.from_url('http://127.0.0.1:5000/leave_worker_pdf', 'output.pdf', configuration=config)` in your code? Doesn't it mean to be the way clients can interact with your server to get their PDFs? – Ali Tou Mar 01 '21 at 08:05
  • @AliTou if I don't call to the config I get this error `OSError: No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf` – ijaz Bacha Mar 01 '21 at 09:07

1 Answers1

0

If you're running this flask application on a remote server, you might have difficulty setting up wkhtmltopdf. I was unable to do so and had to resort to using python library reportlab in order to create this solution.

Here's a git repo w/ code from someone who wrote a book on reportlab if you're interested in pursuing that as a solution.

https://github.com/driscollis/reportlabbookcode

Andrew Clark
  • 850
  • 7
  • 13