How to copy a pdf to specific Directory and rename it
-
Hi all,
So what I want to do:
I have reports in pdf format and I would like to store them all in one folder. After I would like to change the filename automatically.
When this all is done, I want to save the file URL into my database, so I'm able to open the file from within my application.Now I have no idea on how to start with this.
Can anybody give me some hints?
I looked already into QFile, QDesktopservice ... But I can't implement it.
Kind regards
Toon -
Hi all,
So what I want to do:
I have reports in pdf format and I would like to store them all in one folder. After I would like to change the filename automatically.
When this all is done, I want to save the file URL into my database, so I'm able to open the file from within my application.Now I have no idea on how to start with this.
Can anybody give me some hints?
I looked already into QFile, QDesktopservice ... But I can't implement it.
Kind regards
ToonQString fileName = "myReport.pdf"; QString filePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/" + fileName;
Copy an existing file:
bool success = QFile::copy(existingFilePath, filePath );
Or writing PDF contents directly to file:
QFiel f( filePath ); if( f.open(QFile::WriteOnly) ) { f.write( pdfData ); f.close(); }
Getting file url:
QUrl fileUrl = QUrl::fromLocalFIle( path );
Was this what you were looking for?
-
Hi,
To copy a file: QFile::copy().
To rename a file: QFile::rename().
To get URL from a file path: QUrl::fromLocalFile() static method.
To open a file: QDesktopServices::openUrl() static method.If you post your code it will be easier to help.
-
Hi all,
Thanks for the quick reply. It gave me already a good way to go.
Now It think I do something stupid. This is my code.maintenanceType = ui->maintenanceType_Combo->currentText(); reductionSN = ui->ReductionSN_lineEdit->text(); resp = ui->resp_lineEdit->text(); dateMydate = ui->dateEdit_maintenance->date(); QString Date = dateMydate.toString("dd/MM/yyyy"); QString pdfPath = QFileDialog::getOpenFileName(this,tr("Search report"), "C://", "PDF (*.pdf)"); QMessageBox::information(this,tr("Load report"),pdfPath); QString name = reductionSN + " -- " + Date + " -- " + maintenanceType + " -- " + resp + ".pdf"; qDebug()<<name; QString newPath = "//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/" + name ; qDebug()<<newPath; bool success = QFile::copy(pdfPath,newPath); qDebug()<<success;
When I run this I get the following output:
When I push the addMaintenance button I get the following output
mincore\com\oleaut32\dispatch\ups.cpp(2128)\OLEAUT32.dll!74BD5072: (caller: 74BDFE4F) ReturnHr(1) tid(cd8) 8002801D Library not registered. shell\comdlg32\fileopensave.cpp(14267)\comdlg32.dll!74647C7D: (caller: 746768FC) ReturnHr(1) tid(cd8) 80004005 Unspecified error CallContext:[\PickerModalLoop]
When I click OK on the messagebox I get the following output:
"72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf" "//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf" false
So the file doesn't get copied at all.
Now I was wondering is the in the first step or the second step?Anybody a clue?
Thanks in advance,
Kind regards -
Hi all,
Thanks for the quick reply. It gave me already a good way to go.
Now It think I do something stupid. This is my code.maintenanceType = ui->maintenanceType_Combo->currentText(); reductionSN = ui->ReductionSN_lineEdit->text(); resp = ui->resp_lineEdit->text(); dateMydate = ui->dateEdit_maintenance->date(); QString Date = dateMydate.toString("dd/MM/yyyy"); QString pdfPath = QFileDialog::getOpenFileName(this,tr("Search report"), "C://", "PDF (*.pdf)"); QMessageBox::information(this,tr("Load report"),pdfPath); QString name = reductionSN + " -- " + Date + " -- " + maintenanceType + " -- " + resp + ".pdf"; qDebug()<<name; QString newPath = "//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/" + name ; qDebug()<<newPath; bool success = QFile::copy(pdfPath,newPath); qDebug()<<success;
When I run this I get the following output:
When I push the addMaintenance button I get the following output
mincore\com\oleaut32\dispatch\ups.cpp(2128)\OLEAUT32.dll!74BD5072: (caller: 74BDFE4F) ReturnHr(1) tid(cd8) 8002801D Library not registered. shell\comdlg32\fileopensave.cpp(14267)\comdlg32.dll!74647C7D: (caller: 746768FC) ReturnHr(1) tid(cd8) 80004005 Unspecified error CallContext:[\PickerModalLoop]
When I click OK on the messagebox I get the following output:
"72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf" "//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf" false
So the file doesn't get copied at all.
Now I was wondering is the in the first step or the second step?Anybody a clue?
Thanks in advance,
Kind regards@TMJJ001
does the directory//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/
already exist?
I am not sure if QFile::copy() takes care of this.It may be that you need to ensure that the target path is already created:
QFileInfo(filePath).absoluteDir().mkpath(".")
-
@TMJJ001
does the directory//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/
already exist?
I am not sure if QFile::copy() takes care of this.It may be that you need to ensure that the target path is already created:
QFileInfo(filePath).absoluteDir().mkpath(".")
Yes the directory does already excists. This is the directory to my server.
I copied some files to this dir manually and I can access them with the following code:QDesktopServices::openUrl(QUrl::fromLocalFile(RP));
Where RP is the following:
\\SARWOLVM180\Technical Documentation\Kranen\Algemeen\10 Jaarlijkse inspectie\Database\Report\72802057740 -- 19012018 -- Rebuild -- BoschRexroth.pdf
So I don't think this is the issue
-
Does the application or the context wherein it lives have the correct rights to create files ?
Another thing what you could try is to convert the forward slashes to backward slashes. You can use the following function to do that:QDir::toNativeSeperators()
-
Does the application or the context wherein it lives have the correct rights to create files ?
Another thing what you could try is to convert the forward slashes to backward slashes. You can use the following function to do that:QDir::toNativeSeperators()
-
@TMJJ001 said in How to copy a pdf to specific Directory and rename it:
"//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf"
You're using slashes for the date notation, you should use dashes instead. That should fix it.
Graag gedaan. -
@TMJJ001 said in How to copy a pdf to specific Directory and rename it:
"//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf"
You're using slashes for the date notation, you should use dashes instead. That should fix it.
Graag gedaan.@bludger said in How to copy a pdf to specific Directory and rename it:
You're using slashes for the date notation, you should use dashes instead. That should fix it.
dashes are
-
, if you mean backslashes\
also no. Qt (QFile at least) supports//
for UNC paths. -
@raven-worx I'm aware of that but if you take a close look at his code you see the following:
QString Date = dateMydate.toString("dd/MM/yyyy");
The Date object is being used to compose the destination filename as seen here:
QString name = reductionSN + " -- " + Date + " -- " + maintenanceType + " -- " + resp + ".pdf";
As he showed in an earlier post the destination path becomes this:
"//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf"
Since he doesn't do
mkpath
before the file copy there is a big chance that the folder Report/ doesn't contain a directory named:72802057740 -- 27
which contains 02 as subdir and that one haves 2018 as subdir.So if he use dashes instead of slashes to get the Date object to copy will probably work. Another way is to create the complete directory structure using the
mkpath
function. -
@TMJJ001 said in How to copy a pdf to specific Directory and rename it:
"//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf"
You're using slashes for the date notation, you should use dashes instead. That should fix it.
Graag gedaan. -
@raven-worx I'm aware of that but if you take a close look at his code you see the following:
QString Date = dateMydate.toString("dd/MM/yyyy");
The Date object is being used to compose the destination filename as seen here:
QString name = reductionSN + " -- " + Date + " -- " + maintenanceType + " -- " + resp + ".pdf";
As he showed in an earlier post the destination path becomes this:
"//SARWOLVM180/Technical Documentation/Kranen/Algemeen/10 Jaarlijkse inspectie/Database/Report/72802057740 -- 27/02/2018 -- Oil change -- MMM.pdf"
Since he doesn't do
mkpath
before the file copy there is a big chance that the folder Report/ doesn't contain a directory named:72802057740 -- 27
which contains 02 as subdir and that one haves 2018 as subdir.So if he use dashes instead of slashes to get the Date object to copy will probably work. Another way is to create the complete directory structure using the
mkpath
function.@bludger said in How to copy a pdf to specific Directory and rename it:
I'm aware of that but if you take a close look at his code you see the following:
QString Date = dateMydate.toString("dd/MM/yyyy");ah yes, didnt get that you actually meant the date in the path
-
Sorry to chime in, I just want to verify: it works? If yes then it means Qt does now support copying to samba shares, cool :-)