9 Replies Latest reply on Aug 2, 2016 1:04 PM by Kukurykus

    Photoshop Script PDF to PNG

    Zeppelin F

      Hi

      The script below opens photohop asks for a folder location takes all jpegs and then makes copies of them in the same folder as PNG's.

      But I would like it to turn PDF's into PNGs with the same name.  Currently the script names them like this. 

      "Example Image.jpeg   to   Example-Image.jpeg.png"

       

      I could use the script even if the naming convention was fixed to have the name change fixed.

       

       

      // Exports a folder full of images to PNG

      // images in the same folder.

      #target Adobe Photoshop CC 2015.5

       

      // Open folder selection dialog (for user to chose folder):

      alert("You will be prompted for the folder containing your images.\n" +

            "Files will be export with a '.png' on the end in the same folder.");

       

      var folder = Folder.selectDialog();

      if (!folder) {exit;}

       

      // Export currently open image as PNG.

      function exportPng(img, savePath){

        var opts = new ExportOptionsSaveForWeb();

        if (img.bitsPerChannel != BitsPerChannelType.EIGHT)

          img.bitsPerChannel = BitsPerChannelType.EIGHT;

        opts.PNG8 = false;

        opts.transparency = true;

        opts.interlaced = false;

        opts.quality = 100;

        opts.includeProfile = false;

        opts.format = SaveDocumentType.PNG;

        img.exportDocument(savePath, ExportType.SAVEFORWEB, opts);

      }

       

      // For each image: open, then save as JPG:

      var files = folder.getFiles(/\.(jpg|jpeg|tif|tiff|bmp|png|eps)$/i);

      for (var i = 0; i < files.length; i++) {

        var img = app.open(File(files[i]));

        var saveFilePath = File(files[i] + ".png");

        exportPng(img, saveFilePath);

        img.close(SaveOptions.DONOTSAVECHANGES);

      };

        • 2. Re: Photoshop Script PDF to PNG
          JJMack Most Valuable Participant

          PDF can contain many pages and they can contain images.   If you open more the one  image or page or 3d objects in a PDF they will open as seperate document each document will contain  one page, image or 3d object.  For example the Photoshop CC Reference PDF has 859 Pages and 1,778 images, no 3d objects.     The Images would also be in the Page documents so you could get by just opening the 859 Pages and save out not 1 but 859 jpeg file one for each page.

          • 3. Re: Photoshop Script PDF to PNG
            Zeppelin F Level 1

            Every PDF file i use at work are single pages, and we keep low res images of each one. I need a script that can save a PNG of each one.

            • 4. Re: Photoshop Script PDF to PNG
              JJMack Most Valuable Participant

              I do not know of any image processor type of scripts that opens PDF files.  Paul's Picture Processors could filter image file types in a source folder however, PDF are not considered Image File types.   PDF do not open like an Image file.   You may be able to modify that script to only process PDF file in a source folder adding scriptlistener code that would open a single page in a PDF.

               

              The script is written for a windows system to open all jpg, jpeg, tif, tiff, bmp, png, and eps images for Mac support you would add file extensions like jpe often used on Mac for jpeg files.  You do not want to process any of those and the current file open code "var img = app.open(File(files[i]));" I feel may not work for file type PDF.

               

              http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop-cc-javascr ipt-ref-2015.pdf

              • 5. Re: Photoshop Script PDF to PNG
                Kukurykus Adobe Community Professional

                Replace last part of your code with bellow modification:

                 

                var files = folder.getFiles(/\.(pdf)$/i);
                for(var i = 0; i < files.length; i++) {
                  var opn = new PDFOpenOptions(); opn.page = 1
                  var img = app.open(File(files[i]), opn);
                  var saveFilePath = File(files[i].parent + '/' + activeDocument.name + '.png');
                  exportPng(img, saveFilePath);
                  img.close(SaveOptions.DONOTSAVECHANGES);
                };
                


                I hope I understood you, but if not write what else should be modified...

                • 6. Re: Photoshop Script PDF to PNG
                  Zeppelin F Level 1

                  Thank you!

                   

                  Now it opens photoshop asks for a folder location takes the pdfs from it and makes pngs of them. The naming is still off somehow. It changed the name from "Ladies Skechers Nike.pdf" to "Ladies-Skechers-Nike.png

                   

                  Any idea on how to make the name of the exported png to match the pdf.

                  • 7. Re: Photoshop Script PDF to PNG
                    Kukurykus Adobe Community Professional

                    Instead of whole that code you came with on here use new shorter one:

                     

                    #target Adobe Photoshop CC 2015.5
                    bringToFront(); var folder = Folder.selectDialog()
                    
                    var files = folder.getFiles(/\.(pdf)$/i)
                    for(var i = 0; i < files.length; i++) {
                        var opn = new PDFOpenOptions(); opn.page = 1
                        var img = open(File(files[i]), opn)
                        img.saveAs(File(files[i].parent + '/' + img.name.replace(/-\d+/, '') + '.png'), new PNGSaveOptions())
                        img.close(SaveOptions.DONOTSAVECHANGES); 
                    }
                    

                     

                    When I modified first time that code for you I didn't noticed there is '-' between words, I thought you are saying about additional extension, ie. jpeg.png.

                    Anyway that '-' character was created because that had to be used for web where spaces in https adresses are not allowed.

                    • 8. Re: Photoshop Script PDF to PNG
                      JJMack Most Valuable Participant

                      Yes of course we do.

                       

                      If you want a custom script you should learn how to program them. That is why I poster what needed to be done to the script and told you what the original script did.  And posted the Adobe Photoshop JavaScript reference. I wrote that the way the image file were opened would not work for PDF files. They do not open like image files do.

                       

                      Kukurykus  handed the code to you which IMO did not help you learn anything about scripting Photoshop.

                       

                      To change the file names save you would need to parse the open document name and use parts of it to create the output file name you want then change the save logic.   It can be done.

                      • 9. Re: Photoshop Script PDF to PNG
                        Kukurykus Adobe Community Professional

                        Zeppelin I wrote new version which probably you won't need, but if, let me indicate deifferences.

                         

                        Previous version:

                        - it named original files with their original name but with new extenstion (PDF to PNG)

                        - it would fail for multi-pages files as it would add -1 at end of file name

                         

                        Current version:

                        - you may use it to for saving 1st page with original name (just changed extension) of multi-pages PDF. In previous version it would've added -1 at end of file name (indicating number of page). Additionally it won't remove -(no.)  from original name.

                        - if you use current version for one page PDF which name originally was with -1 (or any number) at the end, it won't remove it like in case of multi-pages document.

                         

                        #target Adobe Photoshop CC 2015.5
                        bringToFront(); var folder = Folder.selectDialog()
                        
                        var files = folder.getFiles(/\.(pdf)$/i)
                        for(var i = 0; i < files.length; i++) {
                            var opn = new PDFOpenOptions(); opn.page = 1, img = open(File(files[i]), opn), reg = /-\d+$/
                            img.saveAs(File(files[i].parent + '/' + eval("iN" + ((iN = img.name) !== decodeURI(files[i].name.slice(0, -4)) ? ".replace(reg, '')" : '')) + '.png'), new PNGSaveOptions())
                            img.close(SaveOptions.DONOTSAVECHANGES); 
                        }