Copy link to clipboard
Copied
I'm trying to create a "Save As PDF" script for Illustrator that will allow me to keep my active document as an Illustrator (.AI) file open.
In my workflow, I need to create compressed PDF version of my Illustrator file for clients, WHILE I'm working on an Illustrator file. In other words, I need to keep the .AI file open and have the .PDF file saved in the background.
Currently this is how I do this:
1. Choose "Save A Copy..."
2. Choose PDF
3. Choose PDF Preset from the drop-down menu.
4. Use the keyboard to delete " copy" which is automatically appended.
5. Choose "Save PDF"
Of course I can easily just choose "Save As.." and choose pdf, but then my file is no longer an Illustrator (.ai) file.
I've been able to code a script which will automatically save my document as a pdf with a specific preset but it only works on the "Save As..." command. I need it to work on the "Save A Copy..." command. If I could just figure out a way to automatically remove the " copy" suffix that's automatically appended to the filename it would be a breeze.
Can you assist me in figuring this out?
Copy link to clipboard
Copied
var doc = app.activeDocument; // Name of the active document
var original_file = doc.fullName; // we need to store the original file and path
var pdfOption = 'Ansichts-PDF 150dpi'; // Name of PDF settings
var pdfSuff = '_lowres.pdf'; // Suffix for PDF
var arr = doc.name.split(".");
var extension = "";
if (arr.length>1) extension = "." + arr.pop();
var filename = arr.join("."); // Just generate a clean file name
var name_pdfpreview = filename + pdfSuff; // new name and suffix for PDF
var export_folder = "~/Desktop/"; // Define path where to save PDF
// save as PDF and tell function which options to use and how to name the PDF
function saveCopyAsPDF (setPDF, namePDF) {
var destFolder = null;
destFolder = export_folder;
var options = null;
options = new PDFSaveOptions ();
options.pDFPreset = setPDF;
options.viewAfterSaving = false; // just to be sure not to open Acrobat
var targetFile = null;
targetFile = new File (destFolder + namePDF);
doc.saveAs (targetFile, options); // here we save the PDF
doc.close (); // now we close it in Illustrator ...
app.open (File (original_file)); // ... and re-open the Illustrator file
}
saveCopyAsPDF (pdfOption, name_pdfpreview);
Hope this will help.
It's a workaround: just store the original file info in a variable, write the pdf, close it, and return to the orginal file.
Copy link to clipboard
Copied
Oh wow! Thanks for writing that script.
Question: I've changed the variable "pdfOption" to one of my custom settings but I'm getting an error when I run the script.
Error 1200:an Illustrator error occurred: 1129270854 ('FNOC') on Line 25
-> doc.saveAs (targetFile, options); // here we save the PDF
Any ideas what's causing the error?
Copy link to clipboard
Copied
Okay I found the issue. There was an uncapitalized letter ('p') on line 21. I changed it to this:
options.PDFPreset = setPDF;
The corrected code works flawlessly. Thank you again for taking the time to write this script!
Copy link to clipboard
Copied
Happy to help.
This function is part of a much larger Script I use for collecting all files needed for print houses. It not only generates 2 different PDF but also 2 different AI, one of them with outlined fonts. It also gathers the images and generates a .txt file with further information like used fonts and color swatches.
q3player
Copy link to clipboard
Copied
I'm trying this script (with the typo correction on line 21 and with my PDF preset name), and it's not working correctly for me. Instead of using the PDF using preset in the script, it saves it with Illustrator default settings—so all the embedded images are full resolution and the file ends up being the exact same size as the original AI file. I don't get an error or anything, just the file is saved with the wrong preset.
I've gone through it several times, tried different presets (updating the script and quiting and reopening Ilustrator) and nothing works, is there something in this script need to be updated for CC2019?
var doc = app.activeDocument; // Name of the active document
var original_file = doc.fullName; // we need to store the original file and path
var pdfOption = '200dpi'; // Name of PDF settings
var pdfSuff = '_PROOF.pdf'; // Suffix for PDF
var arr = doc.name.split(".");
var extension = "";
if (arr.length>1) extension = "." + arr.pop();
var filename = arr.join("."); // Just generate a clean file name
var name_pdfpreview = filename + pdfSuff; // new name and suffix for PDF
var export_folder = "~/Desktop/"; // Define path where to save PDF
// save as PDF and tell function which options to use and how to name the PDF
function saveCopyAsPDF (setPDF, namePDF) {
var destFolder = null;
destFolder = export_folder;
var options = null;
options = new PDFSaveOptions ();
options.PDFPreset = setPDF;
options.viewAfterSaving = false; // just to be sure not to open Acrobat
var targetFile = null;
targetFile = new File (destFolder + namePDF);
doc.saveAs (targetFile, options); // here we save the PDF
doc.close (); // now we close it in Illustrator ...
app.open (File (original_file)); // ... and re-open the Illustrator file
}
saveCopyAsPDF (pdfOption, name_pdfpreview);
Copy link to clipboard
Copied
Seems like the line was the issue in my case:
options.pDFPreset = setPDF;
needs to have the p lowercase actually, not sure if it's a CC2019 difference from previous years?
Copy link to clipboard
Copied
Thanks for this script. I've been using it for a few days and I really like it. However I remember now one of the reasons why I've been asking Adobe to add an Export PDF function to Illustrator as a new feature. Using your script (or one that I wrote myself a while back) causes the active document to lose it's UNDO history. Since we're saving the active document to PDF then reopening the original document from disk, all history is lost. It would be great if we could truly Export the document instead of Save As. If Adobe would just write modify their Export menu item to allow for this, all would be good.
I've been using your script, I just make sure I'm happy with the current state before running it, since I will lose the history.
Copy link to clipboard
Copied
Well, you have to do this via a dynamic action:
Creating a dynamic action to use with app.doScript() method.
Copy link to clipboard
Copied
You don't need to close and reopen the file, Just make sure you set the page range in the PDF options and it will retain the .ai filename i the document just as if you exported a file.
If you have a 1 page document just set the range to "1"
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Based on the replies above, here is a script that does the export in one step.
function exportPDF_IL(pdfPath, preset ) {
var docRef = app.activeDocument;
var options = new PDFSaveOptions();
if ((preset !== undefined) && (preset.length > 0))
options.pDFPreset = preset;
var numArtboards = docRef.artboards.length;
var range = "1";
if (numArtboards > 1) range += "-" + numArtboards;
// If you supply a range then the saveAs becomes a saveACopy.
pdfSaveOptions.artboardRange = range;
// Prevent open after export
pdfSaveOptions.viewAfterSaving = false;
var myFile = new File(pdfPath);
docRef.saveAs(myFile, pdfSaveOptions);
};
exportPDF_IL( "e:\\pdfs\\sample.pdf", "");
Copy link to clipboard
Copied
Grfeat script. Please, correct the "pdfSaveOptions" - has to be: "PDFSaveOptions"... May confuse...