FLASK uWSGI NGINX PyPDF2

问题描述

我在 ubuntu 18 上的应用程序有问题。

有来自 uWsgiNginx 的 Flask

下面的错误生成函数

def print_report(year,month):
import pdfkit,os
import app.DB.employee_data as EMP
from PyPDF2 import PdfFileMerger,PdfFileReader
from flask import send_file
emp_list = EMP.emp_list()
output_pdf=PdfFileMerger()
options = {'enable-local-file-access': None}

for emp in emp_list:
    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(dir_path)
    rendered=pracownicy_detail(emp[0],emp[1],year,month)
    pdf = pdfkit.from_string(rendered,'pdfs/{imie}-{nazwisko}.pdf'.format(imie=emp[0].strip(),nazwisko=emp[1].strip()),options=options)
    output_pdf.append(PdfFileReader('pdfs/{imie}-{nazwisko}.pdf'.format(imie=emp[0].strip(),'rb'),import_bookmarks=False)
    #usunac pozostale pliki
    os.remove('pdfs/{imie}-{nazwisko}.pdf'.format(imie=emp[0].strip(),nazwisko=emp[1].strip()))
output_pdf.write('pdfs/raport.pdf')

return send_file('../pdfs/raport.pdf',as_attachment=True)

使用 uswgi app.ini 命令手动启动的应用程序 100% 正确运行

作为 app.service 运行会产生“内部服务器错误

应用服务

[Unit]
Description=A simple Flask uWsgi application
After=network.target

[Service]
User=ubuntu-app
Group=www-data
WorkingDirectory=/home/ubuntu-app/app
Environment="PATH=/home/ubuntu-app/app/env/bin"
ExecStart=/home/ubuntu-app/app/env/bin/uwsgi --ini app.ini

[Install]
WantedBy=multi-user.target

Nginx 设置

server {
    listen 80;
    server_name 192.168.2.120;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/ubuntu-app/app/app.sock;
    }
}

app.ini 下面

[uwsgi]
; Production .ini file
module = run:app
master = true

; There is no magic rule for setting the number of processes or threads to use.
; It is very much application and system dependent so you'll need to experiment.
processes = 2
threads = 2



socket = app.sock
chmod-socket = 666
vacuum = true
die-on-term = true

解决方法

解决方案是添加:

config = pdfkit.configuration(wkhtmltopdf=bytes('/usr/local/bin/wkhtmltopdf','utf-8'))