iis 从flask函数调用时,docx到pdf的转换在本地工作,但在部署服务器上不工作

46scxncf  于 2023-03-08  发布在  其他
关注(0)|答案(1)|浏览(260)

我有一个 flask 应用程序,其中包含一个函数来创建一个docx文档,然后将其转换为pdf,当我在本地计算机上运行代码一切正常,并创建了pdf文档,但当我在部署计算机上的IIS上托管应用程序与fastcgi模块时,它工作正常,并创建docx文档,但不创建pdf文档,这里是创建docx文档并转换为pdf的函数:

def CreateApplication(file,replacements,serialnum):

    document=Document(file)
    for para in document.paragraphs:
        # Iterate through the runs in the paragraph
        for run in para.runs:
            # Check if the run text is a keyword in the replacements dictionary
            if run.text in replacements:
                # Replace the keyword with the replacement and maintain the style
                run.text = replacements[run.text]
    for table in document.tables:
        for row in table.rows:
            for cell in row.cells:
                for paragraph in cell.paragraphs:
                    for run in paragraph.runs:
                        for keyword, value in replacements.items():
                            if keyword in run.text:
                                # Replace the keyword with the value
                                run.text = run.text.replace(keyword, value)
    document.save(f'files/{serialnum}/MainFiles/Application{serialnum}.docx')
    docx_output_path=f'files/{serialnum}/MainFiles/Application{serialnum}.docx'
    pdf_input_path = docx_output_path
    
    # Specify the path of the output .pdf file
    pdf_output_path = os.path.splitext(pdf_input_path)[0] + '.pdf'
    
    # Convert the .docx file to .pdf
    convert(pdf_input_path, pdf_output_path)

下面是我如何调用它从 flask :

@app.route('/submitapplication/<serialnumber>',methods=['POST','GET'])
def submit(serialnumber):
    st = time.time()
    print('iam in')
    datacoming=request.get_json()
    print(datacoming)
    project_describtion=json.loads(datacoming)
    project_describtion['Statue_of_Project']='Application pending'
    current_date = datetime.now()
    formatted_date = current_date.strftime("%Y-%m-%d")
    project_describtion['DateofApplication'] = formatted_date
    print(project_describtion)
    pidata = Search_in_Company(session['user'], session['password'], session['serverip'], session['companyid'])
    project_describtion.update(pidata)
    '''
    write sql code to upload this dictionary to its specified rows
    '''
    project_describtion['Project_Serial_Number']=serialnumber
    #CreateApplication('LPG application General.docx',project_describtion,serialnumber)

    #trying threading to run conversion in another thread
    Application=threading.Thread(target=CreateApplication,args=('LPG application General.docx',project_describtion,serialnumber))
    Application.start()

    # get the execution time

    Update_case(session['user'],session['password'],session['serverip'],session['currentserial'],session['companyid'],project_describtion)
    et = time.time()
    elapsed_time = et - st
    print('Execution time:', elapsed_time, 'seconds')
    return jsonify({'url': url_for('home')})
oewdyzsn

oewdyzsn1#

IIS提供了很多托管Web应用的特性,Python Web应用可以通过Httpplatformhandler和FastCGI特性来托管,对于Python开发者来说,学习HttpPlatformHandler变得非常重要,微软不再推荐FastCGI,所以这不再是IIS上托管Python Web应用的正确方式,可以切换到HttpPlatformHandler。
使用Windows平台安装程序下载并在IIS上安装Httpplatformhandler,或从此link下载。
此外,您还可以引用此article来托管带有Httpplatformhandler的Flask Web应用程序。

相关问题