Create a pdf with Python Flask from file - python

I need to write a python flask service which takes in input a file (.doc or .ppt) and returns the same file converted in pdf without saving it in a folder. This service has to run on a server.
I tried with pdfkit but I've it not easy to install it on a server.
What i need is something like this:
def post(self):
file = request.files['file_request']
pdf = pdfkit.from_file(file, False) #or any other library to convert the file
return send_file(
pdf,
mimetype='application/application/pdf',
attachment_filename="File.pdf",
as_attachment=True,
cache_timeout=0
)
Is it possible to do it? Could somebody help me?
Thanks

Related

Download GitLab file with gitlab-python

I am trying to download a file or folder from my gitlab repository, but they only way I have seen to do it is using CURL and command line. Is there any way to download files from the repository with just the python-gitlab API? I have read through the API and have not found anything, but other posts said it was possible, just gave no solution.
You can do like this:
import requests
response = requests.get('https://<your_path>/file.txt')
data = response.text
and then save the contents (data) as file...
Otherwise use the API:
f = project.files.get(path='<folder>/file.txt',ref='<branch or commit>')
and then decode using:
import base64
content = base64.b64decode(f.content)
and then save content as file...

Download generated excel file using xslxwriter on Python(Django)

I am working with Python using the Django framework, at this moment I am generating some reports in Excel, for that I use the xslxwriter library, the user tries to download the file from the platform, I do not get the download and instead it The answer is strange characters, I think that's the Excel file, anyway I don't know how to make that file download.
This snippet is the one that is supposed to download the file
workbook.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=test.xlsx"
output.close()
return response
This is the response I get from the frontend
Thank you very much in advance.

Download excel to open in browser instead of downloading directly? Python & Flask

Can I make an excel file open in the browser to be viewed instead of being downloaded, and have a download button, similar to how PDFs are?
I'm using Python Flask, for PDFs I do:
#blueprint_name.route("/download_some_pdf", methods=["GET"])
def download_pdf():
return send_file(file_path, cache_timeout=1)
This opens the PDF in a browser tab with the download PDF button
and for Excel files:
#blueprint_name.route("/download_some_xlsx", methods=["GET"])
def download_xlsx():
return send_from_directory(dir_path, filename, as_attachment=True, cache_timeout=1)
If for the Excel I remove the as_attachment parameter, or I use send_file instead of send_from_directory, it still downloads the file but with the name of the method ("download_xlsx") instead of the filename!!
I'm using Python 3.8.3 and Flask 1.1.2
So far as I know, whether to open the file depends on the client browser. I have encountered a browser who could not read the pdf and download it directly. So once you set the file to have the "GET" property it is upon the receiver to open it or simply save it in directory. Moreover, the "open pdf" only happens after the file is already fully downloaded in Temporary directory set by the browser.

Flask: force download pdf files to open in browser

I'm trying to download a PDF file using flask but I don't want the file to download as an attachment. I simply want it to appear in the user's browser as a separate webpage. I've tried passing the as_attachment=False option to the send_from_directory method but no luck.
Here is my function so far:
#app.route('/download_to_browser')
def download_to_browser(filename):
return send_from_directory(directory=some_directory,
filename=filename,
as_attachment=False)
The function works in the sense that the file is downloading to my computer but I'd much rather just display it in the browser (and let the user download the file if they want to).
I read here I need to change thecontent-disposition parameter but I'm not sure how that can be done efficiently (perhaps using a custom response?). Any help?
Note: I'm not using Flask-Uploads at the moment but I probably will down the line.
You can try to add the mimetype parameter to send_from_directory:
return send_from_directory(directory=some_directory,
filename=filename,
mimetype='application/pdf')
That works for me, at least with Firefox.
If you need more control over headers, you can use a custom response, but you will lose the advantages of send_file() (I think it does something clever to serve the file directly from the webserver.)
with open(filepath) as f:
file_content = f.read()
response = make_response(file_content, 200)
response.headers['Content-type'] = 'application/pdf'
response.headers['Content-disposition'] = ...
return response

How do I allow users to download a MIDI file with Flask without getting a 0 byte download?

I am writing an application that creates a midi file using the MIDIUtil library. When the user submits an HTML form, a midi file object is created with MIDIUtil. How do I allow the user to download this as a .mid file? I have tried the following code, but I end up downloading a file of 0 bytes.
return Response(myMIDIFile, mimetype='audio/midi')
I use a variant of the following code to allow my users to download images they generate. The below code should work for you. Please note that you will most likely need to specify the full server path to the file being downloaded.
from flask import send_file
download_filename = FULL_PATH_TO_YOUR_MIDI_FILE
return(send_file(filename_or_fp = download_filename,mimetype="audio/midi",as_attachment=True))
I ended up using this, and it worked.
new_file = open('test.mid', 'wb')
myMIDI.writeFile(new_file)
new_file.close()
new_file = open('test.mid', 'rb')
return send_file(new_file, mimetype='audio/midi')
Might want to just try using send_file
from flask import send_file
return send_file("yourmidifile.mid", as_attachement=True, mimetype="audio\midi")

Categories

Resources