Generate custom download link for download from Qt
I have a website where people can enter some values in a form, color, email, etc ... and when validated it generates a dynamic pdf file.
I would like to send a download link with this pdf file directly from Qt (not in PhP) on the customer's email.
Is that possible? Without database?
A website form is some HTML client side.
Are you running some Qt app also client side or do you mean on the server? Is your HTTP server Qt based or something else (e.g. PHP), and if so how do you communicate the two? What a database has to do with them?
My website is a wordpress website.
Once the form is validated, I will trigger the execution of my qt app (the trigger is in PHP) by passing the parameters of the form to my qt app, but then, I would like the Qt app to do everything else, which means, create the file, create a custom download link, and send it by email.
Ok, so you can create the PDF using "QPrinter":http://doc.qt.io/qt-5/qprinter.html and "QPainter":http://doc.qt.io/qt-5/qpainter.html.
Use the setOutputFormat and setOutputFileName methods of QPrinter to create a file. Place it in some directory your server opens for read access. This depends on what server software you're using. Generating a link is straightforward - it's the exposed outside directory url, e.g. "www.mysite.org/downloads/" + the file name. I suggest a file name generated at random.
To send an email you need a running mail server and a recipient address.
Once you've got a server running Qt doesn't have a built-in mail client class, but there are libraries ready to do that, e.g. QxtSmtp. But since you've got PHP running on the server I think it would be easier to just use that, as it has great SMTP support.
As for the recipient address it's up to you how you get it in the Qt app. Either pass it from PHP along with other params or get from some file or database. It depends on where you store it and how easy it is to access that information.
Thanks for your answer Chris.
In your example www.mysite.org/downloads/” + the file , can Qt create the custom random directory with the file inside and automatically allow the read access on it? I would imagine something like www.mysite.org/downloads/custom_random_dir/file”
As you understood, I don't want people to access other's person pdfs files by going to the url www.mysite.org/downloads/.
No, it's something you have to do server side. It's your web application that knows who can access what.
This is mostly a job of your web server and OS. Qt can of course create a directory (QDir) and a file (QFile) and run an external script, but that's about it.
Imagine you want to create a file /somedir/myservedstuff/351625361582.pdf and then send it to a user as a download link http://mypage.org/stuff/351625361582
I assume it's the PHP script that runs your Qt app?
PHP is run by a web server (I'll use Apache for an example). The Apache server needs to run with write permissions to the /somedir/myservedstuff/ directory. It then runs the PHP script which runs your Qt app using the same access privileges.
Then, when your app creates a file, the web server, e.g. Apache, needs to have read access to that directory to serve it. It also needs to have a rule configured to serve stuff from the /somedir/myservedstuff/ under the http://mypage.org/stuff/ address.
All this is mostly web server configuration and setting directory permissions in your server OS. Qt app has little to do with it.
As for the "privacy" stuff. The file needs to be accessible from the web and in the simplest form if it's accessible it's accessible to anyone who knows the address. One thing to do is to just expose the files, not the directory. That is again a role of your web server and Qt has nothing to do with it. Then you can use some long random file names (e.g. fb324ygf9263g23gvf729v3f972v3f67239.pdf) to minimize a risk of sniffing the files with consecutive number names. But the only way to make them private and accessible only to a selected user is by creating a user account and exposing the file only to that user. That is yet again a role of your web server and possibly WordPress, depending on how you want to administer it.
Thank you very much Chris and SGaist its much clearer now.
I wanted to avoid to look into PHP; it looks like I won't have choice :(