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.
Pdf_Export(saveFolder)
OS: Monterey OS 12.2.1
Application: Photoshop 2022 version 23.0.2
Thanks
Asuvath
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
...
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);
}
Copy link to clipboard
Copied
The JS reference indicates that the PDF presetFile overrides other options:
It also indicates that ZIP compression is the default PDFEncoding:
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)
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Object model code.
Thanks
Asuvath