vana
vana

Reputation: 11

Saving a webpage as PDF using Python?

I'm trying to save/export a full web page result to PDF using python

I have generated a webpage which contains some calculation results and a comment section which is accompanied with an "Export to PDF" button.

What I did so far is as follows:

from flask import Flask, session, redirect, url_for, escape, request, Response
app = Flask(__name__)




@app.route('/result', methods=['GET', 'POST'])
def viewResult():
    if request.method == 'POST':

            if 'export' in request.form:

                    x = "XMLXMLXMLXML"

                    request.headers['Content-Type: application/pdf']
                    request.headers["Content-Disposition: attachment; filename='x.pdf'"]

                    return x


            return ''
else:
            global result
    result = 'xml'
            html = ''
            html += '<html>'
            html += '<body>'
            html += '<p>Result</p>'
            html += '<a  href="http://127.0.0.1:5000/">Back</a>'
            html += '<div>'
            html += '<b>Result:</b>'
            html += '<textarea name="result" readonly>' + result + '</textarea>'
            html += '<form method="POST">'
            html += '<br>'
            html += '<textarea name="content" placeholder="Enter a comment"></textarea>'
            html += '<p></p>'
            html += '<input type="submit" name="export" value="Save">'
            html += '</form>'
            html += '</div>'
            html += '</body>'
            html += '</html>'
            return html
if __name__ == "__main__":
    app.run()

I've tested/researched a couple of methods so far but none seem to make it work in my case. Please excuse my coding logic. I'm not a python expert :x

So what am I doing wrong here?

Upvotes: 0

Views: 2452

Answers (1)

Parfait
Parfait

Reputation: 107687

Consider using the popular, open-source wkhtmltopdf which you can call externally in Python by simply passing in .html file and .pdf file names. Simply, download its executable and run command lines with it. Plus you can specify page size (-s A4)and orientation arguments (-O landscape). Also, wkhtmltopdf works great with page-rendering CSS like page-break-before: always, page-break-inside: avoid !important;, etc.

Below is an example with screenshots using posted html string. Integrate the method into your app's code base.

import os
...

result = 'xml'

html = ''
html += '<html>'
html += '<body>'
html += '<p>Result</p>'
html += '<a  href="http://127.0.0.1:5000/">Back</a>'
html += '<div>'
html += '<b>Result:</b>'
html += '<textarea name="result" readonly>' + result + '</textarea>'
html += '<form method="POST">'
html += '<br>'
html += '<textarea name="content" placeholder="Enter a comment"></textarea>'
html += '<p></p>'
html += '<input type="submit" name="export" value="Save">'
html += '</form>'
html += '</div>'
html += '</body>'
html += '</html>'

# OUTPUT HTML PAGE
file = open('WebPage.html', 'w')
file.write(html)
file.close()

# OUTPUT PDF PAGE
os.system('/path/to/wkhtmltopdf /path/to/WebPage.html /path/to/WebPage.pdf')

HTML File Web Page HTML

PDF File Web Page PDF

Upvotes: 1

Related Questions