Search code examples
pythonflaskpdfiisdocx

Python Flask UnderIIS - Docx to Pdf


I have an flask app under my windows server and i would like it to convert docx to pdf without changing the docx file. I have tried to do it with comptypes code below, but it doesn't work because windows doesnt allow it.

What are the other options?

Here is the error that i am getting

(-2147024891, 'Access is denied', None, None)

I am looking to find another package that will convert through requests that has been made to IIS but all of them requires a license.

Final Edit: To everyone who tries to do this by dispatching word.application, you have to change windows component services settings. You have to find Microsoft Word over there and apply security changes for IIS and DefaultApplication User. But i decided to change all my plan to libreoffice which doesnt require such thing but it also has disadvantages such as it will not convert to document 1:1 it will change some stuff.

I am closing the topic now.

def doc2pdf(doc_name, pdf_name, font_size=8):

    pythoncom.CoInitialize()
    word = client.DispatchEx("Word.Application")
    if os.path.exists(pdf_name):
        os.remove(pdf_name)
    worddoc = word.Documents.Open(doc_name, ReadOnly=1)
    worddoc.Content.Font.Size = font_size
    try:
        worddoc.SaveAs(pdf_name, FileFormat=17)
    except Exception as e:
        createText('wordToPdfException', f"{e}")
    worddoc.Close()
    # Quit the Word application
    word.Quit()
    pythoncom.CoUninitialize()
    return pdf_name

Solution

  • Doc2Pdf through IIS doesnt work because microsoft doesnt allow it, you have to go to component services of the windows and find Microsoft Word over there and change its security by adding IIS and DefaultAppPool, however, i decided to go with libreoffice which doesnt require these steps, but it has its own disadvantages for example it doesnt convert the document 1:1 it changes some stuff.