• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Photoshop script JPG to PDF file size issue

Participant ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

Hi,


My objective is to create the PDF from JPG through javascript, for that I am using the following code. I can able to create the pdf but the file size is vary (huge [11.5 MB]) when compare to manual PDF (2.3 MB) creation. Can you please check and advise to get the same file size as manual PDF creation.

 

function Pdf_Export(saveFolder){
     var fileName = String(app.activeDocument.name).replace(".psd","");
     var pdfOptions = new PDFSaveOptions();
     pdfOptions.layers = false;
     pdfOptions.embedColorProfile = true;
     pdfOptions.presetFile = "Smallest File Size";
     pdfOptions.optimizeForWeb = true;
     var PdfFileName = File(saveFolder+"/"+fileName);
     app.activeDocument.saveAs(PdfFileName, pdfOptions, false, Extension.LOWERCASE);
}

Pdf_Export(saveFolder)


OS: Monterey OS 12.2.1
Application: Photoshop 2022 version 23.0.2

Thanks
Asuvath

TOPICS
Actions and scripting , macOS

Views

478

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 04, 2022 Jul 04, 2022

Why don't you try to use specific PDF save settings rather than a preset (recreate the preset without using the preset)?

 

function Pdf_Export(saveFolder) {
    var fileName = String(app.activeDocument.name).replace(".psd", "");
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.layers = false;
    pdfOptions.embedColorProfile = true;
    pdfOptions.encoding = PDFEncoding.JPEG;
    pdfOptions.jpegQuality = 4; // 0-12
    // Other options such as downSample, downSampleSize, downSamplesSize
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

Why don't you try to use specific PDF save settings rather than a preset (recreate the preset without using the preset)?

 

function Pdf_Export(saveFolder) {
    var fileName = String(app.activeDocument.name).replace(".psd", "");
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.layers = false;
    pdfOptions.embedColorProfile = true;
    pdfOptions.encoding = PDFEncoding.JPEG;
    pdfOptions.jpegQuality = 4; // 0-12
    // Other options such as downSample, downSampleSize, downSamplesSizeLimit etc...
    pdfOptions.optimizeForWeb = true;
    var PdfFileName = File(saveFolder + "/" + fileName);
    app.activeDocument.saveAs(PdfFileName, pdfOptions, false, Extension.LOWERCASE);
}
Pdf_Export(saveFolder)

 

Another option is to use Scripting Listener recorded code and swap out the static save path and filename for the variable.

 

Pdf_Export("Smallest File Size", 10, new File(saveFolder + "/" + fileName), true, false);

function Pdf_Export(pdfPresetFilename, pdfCompressionType, in2, lowerCase, layers) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	descriptor2.putString( s2t( "pdfPresetFilename" ), pdfPresetFilename );
	descriptor2.putInteger( s2t( "pdfCompressionType" ), pdfCompressionType );
	descriptor.putObject( s2t( "as" ), s2t( "photoshopPDFFormat" ), descriptor2 );
	descriptor.putPath( s2t( "in" ), in2 );
	descriptor.putInteger( s2t( "documentID" ), 883 );
	descriptor.putBoolean( s2t( "lowerCase" ), lowerCase );
	descriptor.putBoolean( s2t( "layers" ), layers );
	descriptor.putEnumerated( s2t( "saveStage" ), s2t( "saveStageType" ), s2t( "saveSucceeded" ));
	executeAction(s2t("save"), descriptor, DialogModes.NO);
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

The JS reference indicates that the PDF presetFile overrides other options:

 

presetFile.png

 

It also indicates that ZIP compression is the default PDFEncoding:

 

encoding.png

 

Reading that the preset overrides all other options, one may think that it is saving with JPEG encoding, however, looking at the recorded Action Manager code, despite a preset being used, there is also an encoding method used as well (10). I am guessing that 10 = JPEG and that at the bare minimum this needs to be included as well. Perhaps something like:

 

function Pdf_Export(saveFolder) {
    var fileName = String(app.activeDocument.name).replace(".psd", "");
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.presetFile = "Smallest File Size";
    pdfOptions.encoding = PDFEncoding.JPEG;
    // Not sure...
    //pdfOptions.jpegQuality = 12;
    var PdfFileName = File(saveFolder + "/" + fileName);
    app.activeDocument.saveAs(PdfFileName, pdfOptions, false, Extension.LOWERCASE);
}
Pdf_Export(saveFolder)

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

Thanks @Stephen_A_Marsh . It is working fine.

 

Thanks
Asuvath

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

So which suggestion was the answer?

 

The addition of standard object model code into your existing code? 

Or the second option of replacing the entire function with the action manager recorded code?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

LATEST

Object model code.

 

Thanks

Asuvath

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines