Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to copy a pdf to specific Directory and rename it
QtWS25 Last Chance

How to copy a pdf to specific Directory and rename it

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMJJ001
    wrote on 27 Feb 2018, 11:55 last edited by
    #1

    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

    R 1 Reply Last reply 27 Feb 2018, 12:38
    0
    • T TMJJ001
      27 Feb 2018, 11:55

      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

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 27 Feb 2018, 12:38 last edited by
      #2

      @TMJJ001

      QString 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?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • G Offline
        G Offline
        Gojir4
        wrote on 27 Feb 2018, 12:39 last edited by Gojir4
        #3

        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.

        1 Reply Last reply
        3
        • T Offline
          T Offline
          TMJJ001
          wrote on 27 Feb 2018, 15:07 last edited by
          #4

          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

          R 1 Reply Last reply 27 Feb 2018, 15:15
          0
          • T TMJJ001
            27 Feb 2018, 15:07

            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

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 27 Feb 2018, 15:15 last edited by
            #5

            @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(".")
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            T 1 Reply Last reply 27 Feb 2018, 15:26
            0
            • R raven-worx
              27 Feb 2018, 15:15

              @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(".")
              
              T Offline
              T Offline
              TMJJ001
              wrote on 27 Feb 2018, 15:26 last edited by
              #6

              @raven-worx

              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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bludger
                wrote on 27 Feb 2018, 15:36 last edited by
                #7

                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()

                T 1 Reply Last reply 27 Feb 2018, 15:47
                0
                • B bludger
                  27 Feb 2018, 15:36

                  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()

                  T Offline
                  T Offline
                  TMJJ001
                  wrote on 27 Feb 2018, 15:47 last edited by
                  #8

                  @bludger

                  The application has the correct right to create files. I also tried the QDir::toNativeSeperators() without success.
                  I get the same errors outputs.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bludger
                    wrote on 27 Feb 2018, 16:11 last edited by
                    #9

                    @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.

                    R T 2 Replies Last reply 27 Feb 2018, 16:34
                    3
                    • B bludger
                      27 Feb 2018, 16:11

                      @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.

                      R Offline
                      R Offline
                      raven-worx
                      Moderators
                      wrote on 27 Feb 2018, 16:34 last edited by raven-worx
                      #10

                      @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.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        bludger
                        wrote on 27 Feb 2018, 16:47 last edited by
                        #11

                        @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.

                        R 1 Reply Last reply 27 Feb 2018, 17:05
                        3
                        • B bludger
                          27 Feb 2018, 16:11

                          @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.

                          T Offline
                          T Offline
                          TMJJ001
                          wrote on 27 Feb 2018, 16:56 last edited by
                          #12

                          @bludger

                          Yes this was the problem!! Stupid !!

                          Thanks all for helping!
                          Dank u wel allemaal ;)

                          Kind regards

                          1 Reply Last reply
                          0
                          • B bludger
                            27 Feb 2018, 16:47

                            @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.

                            R Offline
                            R Offline
                            raven-worx
                            Moderators
                            wrote on 27 Feb 2018, 17:05 last edited by
                            #13

                            @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

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            1
                            • sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on 28 Feb 2018, 06:32 last edited by
                              #14

                              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 :-)

                              (Z(:^

                              1 Reply Last reply
                              1

                              3/14

                              27 Feb 2018, 12:39

                              11 unread
                              • Login

                              • Login or register to search.
                              3 out of 14
                              • First post
                                3/14
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved