Send PDF file path to client to download after covnersion in WeasyPrint - python

In my Django app, I'm using WeasyPrint to convert html report to pdf. I need to send the converted file back to client so they can download it. But I don't see any code on WeasyPrint site where we can get the path of saved file or know in any way where the file was saved.
If I hard code the path, like, D:/Python/Workspace/report.pdf and try to open it via javascript, it simply says that the address was not understood.
What is a better way to apporach this issue?
My code:
HTML(string=htmlContent).write_pdf(fileName,
stylesheets=[CSS(filename='css/bootstrap.min.css')])
This is all the code related to WeasyPrint that generated PDF file.

You didn't even bothered to post the relevant code, but anyway:
If you're using the Python API, you either specify the output file path when calling weasyprint.HTML().write_pdf() or get the PDF back as bytestring, as documented here - and then you can either manually save it to a file somewhere you can redirect your user to or just pass the bytestring to django's HttpResponse.
If you're using the commandline (which would be quite surprising from a Django app...), you have to specify the output path too...
IOW : I don't really understand your problem. FWIW, the whole documentation is here : http://weasyprint.readthedocs.io/en/latest/ - and there's a quite obvious link on the project's homepage (which is how I found it FWIW).
EDIT : now you posted your actual code: the answer is written in plain in the FineManual(tm):
Parameters: target – A filename, file-like object, or None
Returns:
The PDF as byte string if target is not provided or None, otherwise None
(the PDF is written to target.)
IOW, either you choose to pass the filename for the generated to be generated and serve this file to the user, or you can just pass your Django HttpResponse as target, cf this example in Django's doc.

Related

How to upload file with python mechanicalsoup to ASP.net site inside a form

I'm trying to automate the interaction of a website. The website is built with ASP.net so most of the interactions work as a form under the hood. One of the things I need to do is upload a file. In Chrome's inspect window I see this part of the form:
ctl00$ctl00$MainContent$RPBVContent$ucPriceUploader$FileUpload1: (binary)
Chrome's inspect doesn't show the form information when I actually submit the file. It only shows this when I try to upload without having selected the file.
I previously tried doing:
with open('pricestoy.csv', 'rb') as f:
pp=browser.submit_selected(files={'prices.csv': f})
but the website didn't seem to receive the file even though it returned a 200.
It seems like I need to do something like
with open('pricestoy.csv', 'rb') as f:
browser['ctl00$ctl00$MainContent$RPBVContent$ucPriceUploader$cmdUpload1']=f.read()
pp=browser.submit_selected()
but that's got the same issue where I get a 200 but the site doesn't seem to recognize having got a file.
if I do pp.request.headers I see that the Content-Length is 6158289 but when I submit the file in Chrome then it has Content_Length of 6158414 so there seems to be something Chrome is adding. I don't know if that matters since it's very close.
Another difference is that Chrome has
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryg4PYqQpHVnsxwtTh
whereas the python version has
Content-Type: multipart/form-data; boundary=d5416a61760fabc3ac8e6f99229df131
At this point I'm at a loss.
The very helpful thing in figuring this out was to use Firefox instead of Chrome. Firefox has the option to 'Copy POST data' in its Inspect Network whereas Chrome doesn't. By doing that I could see that I was trying to put the file into the wrong form id. I could also see that boundary was just an arbitrary string so focusing on that was time wasted. Also I found this deep in the documentation for mechanicalsoup:
Example: uploading a file through a
field (provide the path to the local file, and its content will be
uploaded):
form.set("tagname", path_to_local_file)
So it turned out to be as simple as doing
browser.form.set('ctl00$ctl00$MainContent$RPBVContent$ucPriceUploader$FileUpload1', 'pricestoy.csv')
browser.submit_selected()

python: determine file type by its content (not only by its magic)

So I'm trying to verify that files I'm getting (uploaded by the user) are indeed images, and valid.
I'm running ClamAV (using python's clamd package) but it doesn't give back the actual file content.
I'm using python's magic package in order to check the magic file prefix (as imghdr also does for images),
But my worry is for files with image magic prefix that contains JS code for example.
Any idea about how can I scan the uploaded file's content in order to determine its type ?
Thank you

save python visualization panel as html

I am working on Panel along with Holoviews. I would like to save it to the HTML file so that I can call it from my website.
Following this, I found that the panel is able to save the file in HTML format. However, it is static and does not reflect changes based on the other components.
panel.save()
I am not able to save it even to JSON as described in the same link through this command
panel.embed().save_path(default='./holoviews-examples')
Any solution.
I think you're misinterpreting the docs a little bit, save_path is not a method, it's an argument to the embed and save methods, e.g.:
panel.save('test.html', embed=True)
If you then want to export to JSON files you can also enable that with:
panel.save('test.html', embed=True, embed_json=True)
and optionally provide a save_path and load_path.

Django Upload file get full name and file path

I'm tinkering around with cloud storage api's and need help getting the full file name from an upload form on Django. I currently can let the user choose a file, "file.txt" from any directory, and can get the name through
for file in request.FILES.getlist('file'):
print file.name `
However I want the full file path, something more like 'home/user/documents/file.txt'
Is this possible and how would I get the full name? I'm not looking to actually upload the file, just get the full path so that I can utilize dropbox/google drive api.
For reference here is my form:
class UploadFileForm(forms.Form):
folder_name = forms.CharField(max_length = 300)
title = forms.CharField(max_length=50)
file = forms.FileField(label='Select a file')
Thanks in advance.
Browsers do not tell what directory the files come from.
They do not even give that information to the scripts on the page.
When a user uploads a file, you may know:
Its basename.
Its size.
Its type (usually guessed from extension).
Its modification time.
This is all you will get, regardless of whether you access the information straight from the browser's <input> element, or wait for it to POST it.
You may also turn this answer the other way around: if you really need to be able to post the full path of the file, you need to develop a client-side application that will send it. It could be a standalone executable, a browser addon/app, or a Java applet, whatever as long as it runs outside of the webpage sandbox.
I do not use dropbox, but I believe you need to download and install some additional software to use it. That's how it would access the full path of your files.

What is a good audio library for validating files in Python?

I'm already checking for content-type, size, and extension (Django (audio) File Validation), but I need a library to read the file and confirm that it is in fact what I hope it is (mp3 and mp4 mostly).
I've been here: http://wiki.python.org/moin/Audio/ but no luck. Been at this one for a while, am a bit lost in the woods. Relying on SO big time for this whole end of things...
Thanks in advance.
EDIT:
I'm already (in Django) using UploadedFile.content_type() :
"The content-type header uploaded with the file (e.g. text/plain or application/pdf). Like any data supplied by the user, you shouldn't trust that the uploaded file is actually this type. You'll still need to validate that the file contains the content that the content-type header claims -- "trust but verify."
So, I'm already reading the header. But how can I validate the actual content of the file?
If just checking the header isn't good enough, I'd recommend using mutagen to load the file. It should throw an exception if it's not correct.
FYI, I do not think your approach is very scalable. Is it really necessary to read every byte of the file? What is your reason for not trusting the file header?
You can call a unix sub-shell within python like this:
>>> filename = 'Giant Steps.mp3'
>>> import os
>>> type = os.system('file %s' % filename)
Giant Steps.mp3: ISO Media, MPEG v4 system, iTunes AAC-LC
** See man pages for more details on the 'file' command if you want to go this route.
See this post for other options
Use sndhdr
It does a little more than content-type. Reads the file and gets it's headers..of course this is still not foolproof..using ffmpeg is probably then the only option.

Categories

Resources