8 Replies Latest reply on May 15, 2016 8:49 PM by karth80472045

    Script to open SVGs in a folder and save them to one multipage PDF

    ifarago

      Hi.

       

      I'm using Illustrator for work and I love it, I never needed scripts yet.

      Now I have couple houndred svgs coming in per day,

      I would like to have a script that opens every svg one by one in the selected directory and add them to a pdf

      as pages of the same size and than save it.

       

      Is this possible? How many files I could put in 1 pdf this way?

       

      I'm using Illustrator CS5 on Win7 x64.

       

      Thanks in advance.

       

      Istvan

        • 1. Re: Script to open SVGs in a folder and save them to one multipage PDF
          Muppet Mark Level 5

          SVG is not a format I use my self… ( I will check with the scripting docs) Do you open SVG or can you place… Either way yes this can be scripted I would think… If open is the required option the you duplicate to another document ( multi-artboard ) if place is OK then its a bit quicker. Illustrator artboards export to PDF as pages…

          • 2. Re: Script to open SVGs in a folder and save them to one multipage PDF
            ifarago Level 1

            If by place you mean: I have a5 sized svgs and copy them to artboards in an AI file and then export as pdf, it would be fine by me, how many a5 artboars an AI file can have?

             

            Anyways I don't mind either way if it happends, I do not like to preform this mindless task anymore if I dont have to.

            Any help would be appriciated with the script.

             

            Thank you for your kind help.

            • 3. Re: Script to open SVGs in a folder and save them to one multipage PDF
              Muppet Mark Level 5

              I think there is a 100 file limit on artboards ( not tested ) but the size would also affect that… You can't straight place *.svg but you can make group from them would that do you?

              #target Illustrator
              
              function filesToArtboards() {
              
                        // My function variables…
                        var doc, fileList, i, inFolder, mm, svgFile;
              
                        // Get the user to select a folder
                        inFolder = Folder.selectDialog( 'Please choose your Folder of files to place…' );
              
                        // Check they ain't cancelled
                        if ( inFolder != null ) {
              
                                  // Gets just the SVG files…
                                  fileList = inFolder.getFiles( /\.svg$/i );
              
                                  // Make sure it has AI files in it…
                                  if ( fileList.length > 0 ) {
              
                                            mm = 2.83464567 // Metric MM converter…
              
                                            // Set the script to work with artboard rulers
                                            app.coordinateSystem =
                                            CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
              
                                            // Add new multi-page document
                                            doc = app.documents.add(
                                                       DocumentColorSpace.RGB,
                                                       width = 148.5*mm,
                                                       height = 210*mm,
                                                       numArtboards = fileList.length, // The number of SVG files
                                                       DocumentArtboardLayout.GridByCol,
                                                       artboardSpacing = 20*mm,
                                                       artboardRowsOrCols = Math.round( Math.sqrt( fileList.length ) )
                                            );
              
                                            // Loop thru the counter
                                            for ( i = 0; i < fileList.length; i++ ) {
              
                                                      // Set the active artboard rulers based on this  
                                                      doc.artboards.setActiveArtboardIndex( i );
              
                                                      // Create group from SVG
                                                      svgFile = doc.groupItems.createFromFile( fileList[i] );
              
                                                      // Move relative to this artboards rulers
                                                      // Top Left
                                                      sgvFile.position = [ 0, 0 ];
              
                                            };
                                  };
              
                        };
              
              };
              
              filesToArtboards();
              
              
              1 person found this helpful
              • 4. Re: Script to open SVGs in a folder and save them to one multipage PDF
                ifarago Level 1

                Thank You Mark!

                 

                I corrected the typo "sgvfile" and switched the dimensions:

                It works!!!, could you modify this script to group the items and align to center,center?

                The other thing I would like to save the pdf automatically.

                And if it is possible save one pdf for every 100 svgs if that is the limit (seems like it), so if the folder got more than a hundred it would save more pdfs automatically. Page order by filename.

                 

                I just switched to

                DocumentColorSpace.CYMK

                is that ok?

                 

                Thanks for the help, Its a great feeling to see this problem go away, and that I can get professional help with illustrator this quick  if i need to.

                 

                Thank you!

                • 5. Re: Script to open SVGs in a folder and save them to one multipage PDF
                  Muppet Mark Level 5

                  I changed my usual CMYK for RGB thinking thats what may have wanted… You have said CYMK I don't think thats going to be valid… Well spotted on the typo… Sure once the group is made it can be centred on the artboard easy enough and yep you can save straight to PDF… What settings do you use? Using a preset makes things easier…

                  • 6. Re: Script to open SVGs in a folder and save them to one multipage PDF
                    ifarago Level 1

                    CMYK typo , I'm sorry.

                    What do I have to add to the script to group and center?

                    So you say like [illustrator default] can be defined in the script? That sounds useful, I save a [svgs2pdf] pdf profile to use by the script.

                     

                    As I understand we have to add in the FOR loop to check if "i mod 100 = 0" then save pdf, close, open new file.

                    And at the and (after the loop) save PDF, close.

                    Oh we have to modify the part where setting number of artboards  (before, not in loop) if "number of files > 100" then 100 artboards else "num of files" artboards.

                    And in the loop as in the IF defined above when creating new document:

                    number of artboards : if num of files>100 then 100 else num of files - i .

                     

                    or at least i think something like this.

                    I just don't know how.

                     

                    oh and edit : doc.artboards.setActiveArtboardIndex( i mod 100 ); should be like this just probably not the right syntax.

                     

                    Can you help?

                    • 7. Re: Script to open SVGs in a folder and save them to one multipage PDF
                      Muppet Mark Level 5

                      This should center the group… If its got stroked elements then its better to work off visible bounds…

                       

                      svgFile.position = [ (doc.width - svgFile.width) / 2, -(doc.height - svgFile.height) / 2 ];
                      
                      • 8. Re: Script to open SVGs in a folder and save them to one multipage PDF
                        karth80472045 Level 1

                        why it is not saving as pdf ? it is possible to change the script to save as pdf ? in my case i have 100 svg files i open each and every file and press shift+o artboard selection as landscape and page is A4 format can anybody modify the code for me