0

I'm trying to convert HTML to pdf using flask.

I can use the following packages.

  • pdfkit
  • wkhtmltopdf

When I run the server it cant load into the browser even the homepage cant load into the browser. I do not understand what's wrong with it.

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>

route.py code:

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)

@app.route('/leave_worker_pdf')
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

Browser screenshot

MacOS
  • 1,149
  • 1
  • 7
  • 14
ijaz Bacha
  • 49
  • 8
  • The browser can not reach your site becuase the server does not know the site, as you have not registered the route with flask. – MacOS Feb 28 '21 at 18:26
  • My first comment might be wrong. Maybe you just have forgotten to start flask? – MacOS Feb 28 '21 at 18:34
  • 1
    when I remove wkhtmltopdf configuration from my app then my app works fine.when I add wkhtmltopdf configuration to my app then the browser can't load the page. – ijaz Bacha Feb 28 '21 at 18:39
  • Well, if you app works fine without the config then you simply do not need it. Please post the solution as a post an mark it as such. – MacOS Feb 28 '21 at 18:45
  • 1
    @MacOS My app works fine but I can't convert HTML to pdf the app gives 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 Feb 28 '21 at 19:05
  • I think you do not have to specify the location of ```wkhtmltopdf```. According to the documentation, it searches for it automatically on linux and windows. On windows, it uses ```where wkhtmltopdf```. Please try it without the explicit config. – MacOS Mar 01 '21 at 08:29
  • @MacOS can you send me the documentation link? – ijaz Bacha Mar 01 '21 at 09:47
  • @Batcha coder: https://github.com/JazzCore/python-pdfkit#configuration – MacOS Mar 01 '21 at 13:42

1 Answers1

0

Change

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

to

@app.route('/leave_worker_pdf')
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

this assumes that you are working with app not not with blueprints.

MacOS
  • 1,149
  • 1
  • 7
  • 14