引导程序未呈现

问题描述

问题分为不同的部分:

  • 小小的序言和尝试的分享
  • 用于重现该问题的文件夹结构和代码
  • 显示并解释问题
  • 问题

序言

我在Raspbian上使用Python 3.8创建了一个简单的Flask 1.1.1应用程序。

我正在尝试使用PdfKit实现PDF文件的生成,但是我在引导渲染方面遇到了一些问题。

我已经尝试在Google上搜索,但发现了两篇文章:

如回购README.md中所述,我已经安装了wkhtmltopdf的静态二进制文件:

警告! debian / ubuntu仓库中的版本功能减少 (因为它编译时没有wkhtmltopdf QT补丁),例如 添加轮廓,页眉,页脚,目录等。要使用此选项,您需要 应该从wkhtmltopdf网站安装静态二进制文件,或者您可以使用此文件 脚本。

有一个名为--user-style-sheet的选项可以用作某些解决方法:

警告这是wkhtmltopdf中此错误的解决方法。你应该 首先尝试--user-style-sheet选项。

问题是我不知道是哪个错误,因为参考页面处于脱机状态。


代码和文件夹

项目文件夹结构如下:

WebServer/
├── Rembe
│   ├── __init__.py
│   ├── static
│   │   ├── content
│   │   │   ├── bootstrap.css
│   │   │   ├── bootstrap.min.css
│   │   ├── fonts
│   │   │   ├── glyphicons-halflings-regular.eot
│   │   │   ├── glyphicons-halflings-regular.svg
│   │   │   ├── glyphicons-halflings-regular.ttf
│   │   │   ├── glyphicons-halflings-regular.woff
│   │   │   ├── glyphicons-halflings-regular.woff2
│   │   ├── scripts
│   │   │   ├── bootstrap.js
│   │   │   ├── bootstrap.min.js
│   ├── templates
│   │   ├── test_page.html
│   │   └── test_pdf_report.html
│   └── views.py
└── runserver.py

重现该问题的代码(避免使用自举程序,您可以从官方网站下载它,我从中获得了代码):

初始化 .py

from flask import Flask

app = Flask(__name__)

db = SQLAlchemy(app)

import Rembe.views

runserver.py

from os import environ,path,walk
from Rembe import app

if __name__ == '__main__':
    # Folder to monitor and check if the server needs to be reloaded
    extra_dirs = ['/home/pi/WebServer/','/home/pi/WebServer/Rembe/','/home/pi/WebServer/Rembe/templates/']
    extra_files = extra_dirs[:]
    for extra_dir in extra_dirs:
        for dirname,dirs,files in walk(extra_dir):
            for filename in files:
                filename = path.join(dirname,filename)
                if path.isfile(filename):
                    extra_files.append(filename)

    app.run('0.0.0.0',80,False,extra_files=extra_files)

views.py

from flask import render_template,url_for,send_from_directory
from Rembe import app
import pdfkit

@app.route('/')
@app.route('/test_page')
def test_page():
    return render_template(
        'test_page.html',title='test'
    )

@app.route('/test_view',methods=['GET','POST'])
def test_view():
    return render_template('test_pdf_report.html',my_custom_value='TEST PARAMETER')

@app.route('/test_download','POST'])
def test_download():
    filename = 'report.pdf'

    string_page = render_template('test_pdf_report.html',my_custom_value='TEST PARAMETER')
    options = {'enable-local-file-access': ""}
    pdfkit.from_string(string_page,f'/tmp/{filename}')

    return send_from_directory(directory='/tmp/',filename=filename)

test_page.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>{{ title }} - TEST</title>
    <link rel="stylesheet" type="text/css" href="/static/content/bootstrap.min.css" />
</head>

<body>

    <div class="container body-content">
        <a href="{{url_for('test_view')}}">test</a>
        <label> | </label>
        <a href="{{url_for('test_download')}}">Download</a>
    </div>

</body>
</html>

test_pdf_report.html

<html>
<head>
    <link href="{{ url_for('static',filename='content/bootstrap.min.css',_external=True) }}" rel="stylesheet">
</head>
<body>

    <div class="row">
        <div class="col-sm-12">
            <h2 style="text-align: center">REPORT TITLE - {{my_custom_value}}</h2>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-6">
            <p>ROW 1 - COLUMN 1</p>
        </div>

        <div class="col-sm-6">
            <p>ROW 1 - COLUMN 2</p>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-6">
            <p>ROW 2 - COLUMN 1</p>
        </div>

        <div class="col-sm-6">
            <p>ROW 2 - COLUMN 2</p>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-6">
            <p>ROW 3 - COLUMN 1</p>
        </div>

        <div class="col-sm-6">
            <p>ROW 3 - COLUMN 2</p>
        </div>
    </div>

</body>
</html>

问题

该项目将生成一个包含两个可点击文本的白页: test download

ClickableText

单击文本 test 呈现的页面正确,并且引导程序正确运行,行/列在正确的位置。

testImage

点击文本下载,打开pdf文件,但不会呈现引导程序,并且行/列与页面左侧对齐。

downloadImage

服务器的输出告诉我一切都很好:

Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done

我还尝试过使用wkhtmltopdf库直接生成PDF,如下所示:

wkhtmltopdf --enable-local-file-access test_pdf_report.html /tmp/mypdf.pdf

该命令无需生成引导程序即可执行并生成PDF,控制台输出为:

Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done

问题

我有两个问题:

  • PdfKit模块是否与引导程序兼容?

  • 有什么方法可以解决我的问题,并下载显示为HTML页面且行/列格式正确的PDF文件?

谢谢您的时间。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...