Copy link to clipboard
Copied
I started learning JSX and made this basic save as PDF Script:
function saveAsPDF() {
var f = new Folder(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0]);
if ( ! f.exists ) {
f.create()
}
var pathBeforeSaving = app.activeDocument.path + "/" + app.activeDocument.name;
var pdfFile = new File(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0] + "/" + app.activeDocument.name.split('.')[0] + '.pdf');
var pdfOptions = new PDFSaveOptions();
pdfOptions.viewAfterSaving = false;
pdfOptions.pDFPreset = "[Impressão de alta qualidade]";
app.activeDocument.saveAs(pdfFile, pdfOptions, true);
alert("Saved");
app.activeDocument.close();
open(File(pathBeforeSaving));
}
saveAsPDF();
It saves a file in a folder with the same name of the life.
The Problems is that after saving the file, it replaces the current app.activeDocument to the new PDF file, but i still need to make chages into the original .ai file.
There is a way to save a new PDF file without opnening it after saving?
Hi @Luís Henrique de Almeida I have my own "saveDocumentAsPDF" function. There are some small differences. When I use my function, I don't have to open or close anything—it just exports the pdf and leaves my open document untouched.
Here is the function:
/**
* Saves document as PDF.
* @author m1b
* @version 2022-09-18
* @param {Object} options
* @param {Document} options.doc - an Illustrator Document.
* @param {String} options.path - export path including filename.
* @param {String} optio...
I got it, it is artboardRange, I never noticed that it will not keep pdf document open.
Thank you @m1b 🙂
Copy link to clipboard
Copied
Hi @Luís Henrique de Almeida I have my own "saveDocumentAsPDF" function. There are some small differences. When I use my function, I don't have to open or close anything—it just exports the pdf and leaves my open document untouched.
Here is the function:
/**
* Saves document as PDF.
* @author m1b
* @version 2022-09-18
* @param {Object} options
* @param {Document} options.doc - an Illustrator Document.
* @param {String} options.path - export path including filename.
* @param {String} options.pdfPresetName - the PDF preset name.
* @returns {File} - the exported pdf file.
*/
function saveDocumentAsPDF(options) {
if (
options.doc == undefined
|| options.path == undefined
|| options.pdfPresetName == undefined
)
return;
var saveOptions = new PDFSaveOptions();
var doc = options.doc,
exportPath = options.path;
saveOptions.saveMultipleArtboards = true;
saveOptions.pDFPreset = options.pdfPresetName;
saveOptions.viewAfterSaving = false;
// must set the range, or will rename the document, too
saveOptions.artboardRange = '1-' + doc.artboards.length;
var f = File(exportPath);
doc.saveAs(f, saveOptions);
if (f)
return f;
};
To use it, you must specify a document, a pdfPresetName, and the path to the final pdf.
var exportedPDFFile = saveDocumentAsPDF(
{
doc: app.activeDocument,
pdfPresetName: '[High Quality Print]',
path: '~/Desktop/test.pdf'
}
);
I hope that helps. - Mark
Copy link to clipboard
Copied
I got it, it is artboardRange, I never noticed that it will not keep pdf document open.
Thank you @m1b 🙂
Copy link to clipboard
Copied
Yes it seems to be the only thing that tells Illustrator I want to "Save a copy", vs "Save as". - Mark
Copy link to clipboard
Copied
Thanks, this also solved another problem i would have when some times i need to save specific panels like "1,2,5".
My code ended like that:
function saveAsPDF() {
var allFiles = new Folder(app.activeDocument.path).getFiles();
var folder;
for (var i = 0; i < allFiles.length; i++) {
thisFile = Folder(allFiles[i]);
if (thisFile instanceof Folder) {
var folderName = decodeURI(thisFile.name)
if((app.activeDocument.name + "").indexOf(folderName) != -1){
alert("existe");
folder = thisFile;
}
}
}
if(folder == undefined) {
folder = new Folder(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0]);
folder.create(SaveOptions.DONOTSAVECHANGES);
}
var pdfFile = new File(folder + "/" + app.activeDocument.name.split('.')[0] + '.pdf');
var pdfOptions = new PDFSaveOptions();
pdfOptions.viewAfterSaving = false;
var artoboardRange = prompt("Definir intervalo de pranhcetas:", "1-4");
if(artoboardRange == null) return;
try {
pdfOptions.artboardRange = artoboardRange;
app.activeDocument.saveAs(pdfFile, pdfOptions, false);
alert("Salvo como PDF");
} catch (err) {
pdfOptions.artboardRange = '1-' + app.activeDocument.artboards.length;
app.activeDocument.saveAs(pdfFile, pdfOptions, false);
alert("Intervalo ilegal, salvando com intervalo padrão.\n\nSalvo como PDF");
}
pdfOptions.pDFPreset = "[Impressão de alta qualidade]";
}
saveAsPDF();
Copy link to clipboard
Copied
Nice! And well done on your scripting!
Copy link to clipboard
Copied
Hi @Luís Henrique de Almeida ,
You can try ExportForScreen option, but you have less cabibility to set the PDF options.
function saveAsPDF() {
var f = new Folder(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0]);
if (!f.exists) {
f.create()
}
var itemToExport = new ExportForScreensItemToExport();
itemToExport.artboards = '';
itemToExport.document = true;
var pdfOptions = new ExportForScreensPDFOptions();
pdfOptions.viewAfterSaving = false;
pdfOptions.pDFPreset = "[Impressão de alta qualidade]";
app.activeDocument.exportForScreens(f, ExportForScreensType.SE_PDF, pdfOptions, itemToExport);
}
saveAsPDF();
Copy link to clipboard
Copied
Hi @m1b ,
I am curious why your script does not keep PDF document open. I mean you and @Luís Henrique de Almeida, both are uisng the saveAs method. Which property keep the document open as ai?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more