-1

I have a flask app that contains a function to create a docx document then converts it to pdf, when i run the code on my local machine everything works fine and the pdf document is created but when i host the app on IIS on deployment machine with fastcgi module, it works fine and creates the docx document but doesn't create the pdf document, here is the function that creates docx document and converts to 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)

And here is how I call it from 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')})
borchvm
  • 3,533
  • 16
  • 44
  • 45
baraatemp
  • 5
  • 4
  • 1
    You need to log your errors and try catch where it's failing. You also haven't shown what's in the `convert` function. Also check you've installed all the dependencies required for PDF creation on your server. – PGHE Feb 24 '23 at 04:03
  • @PGHE I Tried logging but it doesnt log what is in the console in txt file, for the convert function its a function imported from docx2pdf library – baraatemp Feb 24 '23 at 11:40
  • `docx2pdf` requires Microsoft Word to be installed. My guess is your local machine has Word, but your Server doesn't. – PGHE Feb 24 '23 at 21:50
  • @PGHE I suspected this initially but word is installed on my server and to put this possibility away i tried running the program in the server as ordinary python program and it converts successfully but when i run it on iis it just doesnt work – baraatemp Feb 24 '23 at 22:32

1 Answers1

0

IIS provides many features for hosting Web applications. Python web applications can be hosted by the Httpplatformhandler and FastCGI features. For Python developers, learning HttpPlatformHandler becomes very important, Microsoft no longer recommends FastCGI, so this is no longer the right way to host Python web applications on IIS, you can switch to HttpPlatformHandler.

Download and Install Httpplatformhandler on IIS using Windows Platform Installer, or download from this link.

Also, you can refer to this article to host a Flask web application with Httpplatformhandler.

YurongDai
  • 1,362
  • 1
  • 2
  • 7