8 Replies Latest reply on Sep 24, 2009 6:57 PM by Adam Cameron.

    Merging Pdf files in directories

    Ultimatorx

      I am completely new to this. I have tons of directories with pdf files which i want to merge and i need to do it programatically, The contents of each directory (i.e pdf) should be merged and the name of the final pdf for each directory (i.e. 1900) should be the name of the directory itself.(i.e. 1900.pdf)

       

      My folder structure looks like this

       

      c:\ 

      |

      |--------- Batch 1

      |              |___ 1900   (each directory here contains the pdf files that are to be merged)      

      |              |___ 1901

      |              |___ 1902

      |               etc

      |

      |---------- Batch 2

      |              |____1990

       

      |              |____1991

      |               etc

       

      I need to get the code to go through each folder/directory and generate a list of the files, then merge the files in each directory using each direcory name as the name the file for each directory.

      My research shows that i need to generate a list of the directory and files then use <cfpdf> but i'm stuck. Please HELP

        • 1. Re: Merging Pdf files in directories
          Ultimatorx Level 1

          I hope i will get an answer this time

           

          So far i have been able to pass the directory and file information to a <cfpdf> tag which will merge the files in each directory using the directory name as the final file output for each directory.

           

          However, the tag throws up an error: 
          Here is the code:
          <cfdirectory action="list" directory="D:\Dox" name="filelist">

          <cfset tempPath=""> <cfloop query="filelist">

          <cfset fPath="#directory#\#filelist.name#">

          <cfpdf action="merge" directory="#fpath#" destination="D:\dox\#filelist.name#\#filelist.name#.pdf" overwrite="yes"> </cfloop>

           

          and i get the error below, i have tried to, store and retrieve the directory and filename in a database, still the same problem. Can anyone tell me what i am missing

           

          The error is below:

           

          The system has attempted to use an undefined value, which usually indicates a  programming error, either in your code or some system code.  Null Pointers are another name for undefined values.     

           

          The error occurred in C:\wamp\www\codex\tesk.cfm: line 14

          12 : <cfloop query="filelist">

          13 : <cfset fPath="#directory#\#filelist.name#">

          14 : <cfpdf action="merge" directory="#fpath#"  destination="D:\pdfgen\#filelist.name#\#filelist.name#.pdf" overwrite="yes">

          15 : </cfloop>

          • 2. Re: Merging Pdf files in directories
            Adam Cameron. Level 5

            Just for the sake of debugging, in your <cfloop> can you output (or log) all the variables that you're passing to <cfpdf> and check they hold what you expect them to hold.  At the same time, it'll give you an idea if the process is failing outright, or whether, say, the PDFs in some specific subdir are giving you a problem.

             

            What does this code do if you try to merge PDFs in an empty subdir, btw?

             

            --

            Adam

            • 3. Re: Merging Pdf files in directories
              Ultimatorx Level 1

              >Just for the sake of debugging, in your <cfloop> can you output (or log) all the variables that you're passing to <cfpdf> and check they hold what you > >expect them to hold.

              They certainly hold what i expected, I used the code below to output the variables

               

              <cfdirectory action="list" directory="D:\dox" name="filelist">

              <cfset tempPath="">

              <cfloop query="filelist">

              <cfset fPath="#directory#\#filelist.name#">

              <cfoutput>#directory#\#filelist.name#\<br></cfoutput>

              </cfloop>

               

               

              and i got the directories and subdirectories which is now passed to <cfpdf>

              D:\dox\1662\
              D:\dox\1846\
              D:\dox\1901\
              D:\dox\1902\
              D:\dox\3498\
              D:\dox\69\
              D:\dox\8730\

               

               

              > What does this code do if you try to merge PDFs in an empty subdir, btw?

              It simply goes through without generating any error at all, i tried it on an empty directory.

              • 4. Re: Merging Pdf files in directories
                Adam Cameron. Level 5

                Cool.  So, back to the other question I asked: is it halting on a particular subdir, or is it just faceplanting on the very first one?

                 

                --

                Adam

                • 5. Re: Merging Pdf files in directories
                  Ultimatorx Level 1

                  it merges the files in the first directory and outputs them to the required file, then it stops.

                  • 6. Re: Merging Pdf files in directories
                    Adam Cameron. Level 5

                    OK.  Can you try to run the process just on that second dir?  Why I ask is perhaps <cfpdf> might not clear up after itself properly, and be failing as a matter of course on the second call.  Or it could be something in that second dir <cfpdf> doesn't like.

                     

                    If - when you just run on the second dir - it still crashes, check for any obvious differences between the two dirs?  How many PDFs are we talking?  Is it viable to omit each one, one at a time from the merge (renaming a file to .dud or something should omit it... or to be on the safe side move it right out of the dir)?  I've of some PDFs not working so well with CF's PDF ops.

                     

                    If none of that crashes, can you re-try the entire process, but move that second dir out of the way first (so the third dir becomes the second dir, if you see what I mean).

                     

                    In the mean time, I'll set up a similar rig here and see what I get.

                     

                    --

                    Adam

                    • 7. Re: Merging Pdf files in directories
                      Ultimatorx Level 1

                      What i did was to start moving out each directory and running the code, I ended up moving out three directories out, the code worked successfully through the remaining 4 directories without any problem.

                       

                      Initially I thought that that the issue had to do with the compatibility of the pdf files. But again I copied the contents of the directories that i moved out into some of the directories and ran the code and it worked perfectly, which left me confused as to what the problem could really be. I'm glad though that the code is working in some directories. Many thanks for your time, please let me know your findings with the one you set up. thanks

                      • 8. Re: Merging Pdf files in directories
                        Adam Cameron. Level 5

                        I didn't manage to get any errors like yours, but I found it pretty easy for my server instance to run out of heap space when merging a few non-very-big PDF files.  Which doesn't impress me too much.

                         

                        --

                        Adam