简体   繁体   English

使用Python将网页另存为PDF吗?

[英]Saving a webpage as PDF using Python?

I'm trying to save/export a full web page result to PDF using python 我正在尝试使用python将整个网页结果保存/导出为PDF

I have generated a webpage which contains some calculation results and a comment section which is accompanied with an "Export to PDF" button. 我已经生成了一个网页,其中包含一些计算结果和一个带有“导出为PDF”按钮的注释部分。

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 我不是python专家:x

So what am I doing wrong here? 那我在做什么错呢?

Consider using the popular, open-source wkhtmltopdf which you can call externally in Python by simply passing in .html file and .pdf file names. 考虑使用流行的开源wkhtmltopdf ,您可以在Python中通过简单地传入.html文件和.pdf文件名来对其进行调用。 Simply, download its executable and run command lines with it. 只需下载其可执行文件并运行命令行即可。 Plus you can specify page size ( -s A4 )and orientation arguments ( -O landscape ). 另外,您可以指定页面大小( -s A4 )和方向参数( -O landscape )。 Also, wkhtmltopdf works great with page-rendering CSS like page-break-before: always , page-break-inside: avoid !important; 另外,wkhtmltopdf与页面渲染CSS一样非常适用,例如page-break-before: alwayspage-break-inside: avoid !important; , etc.

Below is an example with screenshots using posted html string. 以下是使用张贴的html字符串的屏幕截图示例。 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 HTML文件 网页HTML

PDF File PDF文件 网页PDF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM