0

I am trying to create a flask site, which can take a dataframe with data, insert them into an HTML file, which then gets converted into a PDF file as an output.

The solution works locally on my machine, but when deploying to azure app service i receive following error message:

OSError: No wkhtmltopdf executable found: ".\emazys_service\PDF_generation\wkhtmltopdf\bin\wkhtmltopdf.exe"

I have tried to change my service plan based on following recommendation, but without luck: Azure websites and wkhtmltopdf

source code:

PDF = Blueprint('PDF', __name__)


### Generate Dynamic PDF from Data
path_wkhtmltopdf = r'.\emazys_service\PDF_generation\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)

@PDF.route('/PDF_form', methods = ['POST', 'GET'])
@login_required
def PDF_form():

    if request.method == 'GET':
        files = glob.glob(r".\emazys_service\test_csv\*.csv")

        return render_template('/PDF_form.html', files = files)

    if request.method == 'POST':

        ## Load the value, and output as HTML
        file_requested = request.form.get('file')
        csv_data = pd.read_csv(file_requested, sep="\t")

        ## Load the HTML Template
        templateLoader = jinja2.FileSystemLoader(searchpath="./")
        templateEnv = jinja2.Environment(loader=templateLoader)
        TEMPLATE_FILE = "emazys_service/templates/pdf_template.html"
        template = templateEnv.get_template(TEMPLATE_FILE)
        

        outputText = template.render(df = csv_data)

        html_file = open("test.html", 'w')
        html_file.write(outputText)
        html_file.close()

        ## Take the HTML file, and create to PDF
        pdf = pdfkit.from_file("test.html", False, configuration=config)
        response = make_response(pdf)
        response.headers["Content-Type"] = "application/pdf"
        response.headers["Content-Disposition"] = "inline; filename=output.pdf"

        ### DELETE THE HTML AFTERWARDS
        if os.path.exists("test.html"):
            os.remove("test.html")
        else:
            print("The file does not exist")

        return response

PDF_form.html

{% extends "base.html" %}
{% block content %}
<h1>Create PDF by Clicking Submit! </h1>
<form action="/PDF_form" class="content" method = "POST" target="_blank">
  <select name="file" method="GET" action="/">
      {% for file in files %}
        <option value="{{file}}" SELECTED>{{file}}</option>"
      {% endfor %}
    </select>
    <p><input type = "submit" value = "Create PDF" /></p>
</form>
{% endblock %}

pdf_template.html

<head>
<style>
  body {
    font-family: 'Asap';
  }

  h2 {
    font-size: 26px;
    margin: 20px 0;
    text-align: center;
    small {
      font-size: 0.5em;
    }
  }

  td {
  text-align: center;
  }

  #Tdesign {
    border-collapse: collapse;
    width: 100%;;
  }

  #Tdesign td, #Tdesign th {
  border: 1px solid #ddd;
  padding: 8px;
  }

  #Tdesign tr:nth-child(even){background-color: #f2f2f2;}

  #Tdesign tr:hover {background-color: #ddd;}

  #Tdesign th {
    padding-top: 12px;
    padding-bottom: 12px;
    background-color: #353E45;
    color: #E74011;
    text-align: center;
  }
</style>
</head>
<body>
    <h1>String Test Data</h1>
    <table id="Tdesign">
        <tr>
            {% for column in df.columns %}
            <th>{{ column }}</th>
            {% endfor %}
        </tr>
        {% for idx, row in df.iterrows() %}
        <tr>
            {% for colname in df.columns %}
            <td>{{ row[colname] }}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </table>

</body>

forms.py:

class PDF_form(FlaskForm):
    file = SelectField(u'Choose file', choices = [])
    submit = SubmitField('Create PDF')

Directory overview

James Z
  • 12,209
  • 10
  • 24
  • 44
Søren T
  • 15
  • 1
  • 5
  • Thanks. It worked by changing the route. I however receive a new fault: ´´´OSError: [Errno 8] Exec format error: './emazys_service/PDF_generation/wkhtmltopdf/bin/wkhtmltopdf.exe'´´´ Is that somethingthat says you anything? – Søren T Mar 07 '22 at 12:20
  • I think I have located the problem to be that when I create the Wep App in App Services the only operating system available is Linux. It is not possible to choose Windows. I have tried to use the corresponding debian file for WKhtmltopdf, but I receive the same error. Do u know how to come around this? – Søren T Mar 08 '22 at 09:08

0 Answers0