Copy link to clipboard
Copied
Hi everyone! I'm a graphic designer trying to create a JavaScript script for Adobe Illustrator to automatically embed all linked images.
I often work with files that contain a large number of linked images. When I need to send a PDF with everything embedded, I have to manually embed each image—sometimes clicking hundreds of times. It's extremely tedious.
I know I can Shift-click all the linked items and choose Links panel menu > Embed Image(s), but Illustrator prompts a confirmation for each individual image, which defeats the purpose.
There must be a more efficient and less painful way to do this.
With the help of ChatGPT, I've tried several versions of a script that loop through all placedItems and call .embed() on them. However, they all produce the same issue:
Only some images (like every other one) get embedded.
Some images change size or shift position after being embedded.
I’ve attached screenshots showing the inconsistency. Any ideas why this is happening or how to fix it?
Thanks in advance!
if (app.documents.length > 0) {
var doc = app.activeDocument;
var linkedItems = doc.placedItems;
var count = 0;
for (var i = 0; i < linkedItems.length; i++) {
var item = linkedItems[i];
if (!item.embedded) {
try {
item.embed();
count++;
} catch (e) {
alert("No se pudo incrustar el elemento: " + item.file.name + "\nError: " + e);
}
}
}
alert("Se incrustaron " + count + " elementos enlazados.");
} else {
alert("No hay documentos abiertos.");
}
An additional note: If you save your Illustrator document as .ai you can turn on the "Include Linked Files" checkbox in the Illustrator Options dialog to embed all linked files in one go.
You then just have to close and reopen the document to get the version with the embedded files.
Copy link to clipboard
Copied
you need to start embedding from the bottom up. After each iteration the next item gets the previous items index, ie second item (index 1) will become index 0 after the first item (index 0) gets embedded.
this line reverses the process order
for (var i = linkedItems.length-1; i>=0; i--) {
full script
if (app.documents.length > 0) {
var doc = app.activeDocument;
var linkedItems = doc.placedItems;
var count = 0;
// for (var i = 0; i < linkedItems.length; i++) {
for (var i = linkedItems.length-1; i>=0; i--) {
var item = linkedItems[i];
if (!item.embedded) {
try {
item.embed();
count++;
} catch (e) {
alert("No se pudo incrustar el elemento: " + item.file.name + "\nError: " + e);
}
}
}
alert("Se incrustaron " + count + " elementos enlazados.");
} else {
alert("No hay documentos abiertos.");
}
Copy link to clipboard
Copied
In addition to my other answer, I would like to recommend ChatGPT. The documentation is so spread out and confusing that it can be hard to advance. ChatGPT has helped me enormously in the last few months in spotting bugs, explaining what is possible or not, and how all the different parts function together.
Copy link to clipboard
Copied
Thanks for your reply.
I've tried your script. All embedded but changed sizes/positions.
Copy link to clipboard
Copied
All embedded but changed sizes/positions.
That sounds really weird. Embedding a placed image should not affect its position. If it does, that’s an AI bug.
If you embed the images manually by clicking the Embed button, does that make any difference?
I ran Carlos’s script on a mockup PDF containing linked bitmaps (clipped, scaled, and rotated) and the embedded bitmaps appear correctly when the PDF reopened in Illustrator and when opened in macOS’s Preview app.
Can you provide more information, plus an example PDF that reproduces the problem you see? Before and after screenshots showing the problem will also be useful.
Copy link to clipboard
Copied
Perhaps I'm missing something, but why would you write a script to do this?
Illustrator already embeds all the images when you save as a PDF.
If the problem is that you want the original resolution/format, you can change the PDF options when you export so that the images are not resampled.
Copy link to clipboard
Copied
That's what I thought, but I've heard from a couple of clients they're having the same issue of missing linked images in the final pdf.
I have not seen this problem personally.
Copy link to clipboard
Copied
If this is the case, then I would look for clipping mask problems or image format problems.
It would be useful to have a problem PDF to analyze.
One could also save as a PDF with no compression, open the result to verify that all is well and re-save as a new PDF without compression.
This would still be much easier than writing a script.
Copy link to clipboard
Copied
Andy’s correct: an Ai-generated PDF file has ALL bitmap images embedded upon saving, so that PDF will open and display correctly in any PDF viewer app.
The “missing linked images” problem occurs when you reopen that Ai-generated PDF file in Illustrator.
If that PDF was saved with the “Preserve Illustrator Editing Capabilities” option checked, Illustrator automatically attempts to relink those placed items to the original bitmap files. If it cannot find files with those names in the local filesystem, it throws up those annoying “error” dialogs that it cannot find a linked file, asking you to manually relink or ignore. You can click “Ignore”, of course, in which case the embedded bitmap is shown—but that page item still appears in Ai as a PlacedItem with missing file link, not as a RasterItem as you’d expect, making it problematic to work with.
This behavior is both logical and stupid. For instance, what is the right behavior if the PDF file’s author linked a file named “foo.png”, and there is a completely different file also named “foo.png” on the user’s system? Ai is pretty dumb: it has no way to know if the “foo.png” file it finds locally is identical to the file that was originally used [2], so your PDF’s contents could end up looking drastically different (and wrong!) when reopened in Ai.
On opening an Ai-generated PDF that contains linked bitmaps, Ai should always ask its user:
1. Do you want it to use the PDF’s embedded bitmaps OR relink to the original bitmap files?
And, if you choose to relink but a placed item cannot be relinked because no bitmap file was found:
2. Do you want the item automatically converted to a raster item (using the embedded bitmap), OR do you want the embedded bitmap extracted to a file and the item re-linked to that, OR leave the item with a missing link for the user to fix manually?
See also (ugh): https://illustrator.uservoice.com/search?filter=merged&query=pdf%20image%20relink
--
[1] An option to export all embedded bitmaps from a PDF as individual .png files would also be nice to have. But there are already 3rd-party tools which do that, so best KISS.
[2] Similarly, it’d be nice if Ai’s automatic relinking detected if a found “foo.png” file appears different to the original linked “foo.png” file—a simple checksum comparison—and asks how you want to proceeed.
Copy link to clipboard
Copied
[1] An option to export all embedded bitmaps from a PDF as individual .png files would also be nice to have. But there are already 3rd-party tools which do that, so best KISS.
I’ve just had a quick rummage on Exchange to see if there’s an existing plugin to export all the embedded bitmaps to file and auto-relink to them, and didn’t find anything (although Exchange is a neglected dumpster with a lot of spammy junk and its search feature is lousy so YMMV). A quick web search didn’t find anything either (but ditto).
Illustrator’s scripting API is famously useless with PlacedItems that have missing links; however, the C++ SDK has a deeper reach so may be more useful. Unfortunately the Ai SDK only includes functions for interrogating a document’s Ai data, so (AFAIK) extracting the embedded raster data to individual PNG/TIFF/whatever files will still have to be done using a separate PDF library. But I’m sure there are suitable open-source C/C++ PDF libraries (that aren‘t GPL/LGPL) that can do this.
One challenge: how to match up automatically the PDF’s embedded bitmap data with the embedded Ai data, so that each PlacedItem (AIArtHandle*) is relinked to the correct bitmap file. I expect it can be done as both PDF library and Ai SDK can read each objects’ position and rotation (although their coordinate systems will be different).
An “Open PDF File relinking to exported bitmaps” would be trivial for Ai to provide as a built-in feature (since it can access ALL of the PDF file’s data), but we all know Adobe’s not super-responsive (as the mountain of open tickets on UserVoice can attest). If someone were to create a third-party C++ plugin (and enjoys the challenge) it sounds like there might be some market interest for such a product amongst prepress/graphics professionals.
Copy link to clipboard
Copied
> "An option to export all embedded bitmaps from a PDF as individual .png files would also be nice to have. But there are already 3rd-party tools which do that, so best KISS."
Are you using a recent version of Illustrator?
If so, you may want to have a look at the Unembed command in the Links panel flyout menu to extract and (re-)link all or individual embedded raster images in one go (as .psd or .tif files). It's pretty close to your suggestion, as far as I understand it.
Copy link to clipboard
Copied
AFAIK that only works if a bitmap image was explicitly embedded by the PDF’s author before saving the document as an Illustrator-editable PDF file.
When the Illustrator-editable PDF is reopened in Illustrator, Ai reconstructs the editable document by preferentially using that file’s embedded Ai data†. If a linked file isn’t found, the PlacedItem displays the PDF’s embedded raster image but AFAIK there’s no menu option to unembed that raster data as a bitmap file: the Links palette shows a “broken link” icon for the selected item and the Embed/Unembed menu option remains grayed out. (Or have I missed something?)
--
† Like PSD, PDF is really a container file format, able to store any arbitrary app-specific data in addition to standard print-only data. This is why (e.g.) a block of text remains editable in its original TextFrame when Ai reopens an Ai-editable PDF file it created, instead of the fragmented “point text” characters (the raw print-only data) you get with non-Ai-editable PDFs: Ai stores the text in both formats in separate sections of the file.
(Ideally Ai would embed all the original text in Accessibility format too, so that screen readers and other tools can easily obtain it intact. But I think Adobe’s PDF data format predates ADA compliancy, and most (all?) major apps and tools that generate PDFs—including Adobe’s and Microsoft’s—don’t do this as standard, if at all. Tsk.)
Copy link to clipboard
Copied
I can see that we are now amidst a turnaround of the initial request and that is indeed really challenging.
Will have to think about your valid objections. Probably it's even more complicated as expected.
Copy link to clipboard
Copied
Yeah, was responding to Andy and Carlos, as the issue they describe is a known (and very irritating!) problem when re-opening an Ai-generated PDF in Ai. The ability to click a “Re-export missing images” option in the Links palette and automatically recreate all the linked files from the embedded bitmap data would be the ideal solution to OP’s problem. That would eliminate the need to manually “embed” all the linked files before saving as Ai-editable PDF. I’m sure there’s a lot of prepress users who’d like it too: very handy when altering or correcting an Ai-generated PDF beyond Acrobat’s capabilities.
(The exported bitmap files wouldn’t be identical to the originals—if the PDF is saved with image compression options then the exported files may be lower resolution/quality—but the goal is to fix the “broken links” message that make those items impossible to work with when the PDF is reopened in Ai, while preserving the accuracy of that particular PDF file by not relinking to different bitmap files by accident.)
For now, OP’s only other choice is to save the PDF with “Preserve Illustrator editing” turned off, in which case all bitmaps will appear embedded when reopened in Ai. Unfortunately, that also gets discards Ai text frames and layer data, making everything else in the PDF mostly uneditable in Ai, so I’m guessing that’s not a practical option.
[Rant: It is frustrating that Adobe don’t pay a little more attention to what users are saying about its products: they spend millions of developer hours adding whizzy new geegaws, yet won’t spare a few thousand to eliminate users’ daily pain points. We can add our bug and feature requests to illustrator.uservoice.com but I do wonder how often they action those as there is a lot of open tickets on there, often several years old.]
.
As for the issue OP reports where the linked images resize/reposition after being embedded, that sounds like an Ai bug (another thing Adobe don’t spend nearly enough time cleaning up!). Or possibly an issue with how the linked images themselves were authored.
I wasn’t able to reproduce this issue using a mockup PDF so OP will have to supply an example Ai PDF and linked image files that demonstrates the problem for us to help any further.
Copy link to clipboard
Copied
oh I see, it makes sense, reopening the pdf in ai will open the Ai portion of the file that doesn't have linked files anymore.
thanks for that and the other good pointers
Copy link to clipboard
Copied
An additional note: If you save your Illustrator document as .ai you can turn on the "Include Linked Files" checkbox in the Illustrator Options dialog to embed all linked files in one go.
You then just have to close and reopen the document to get the version with the embedded files.
Copy link to clipboard
Copied
awesome tip, I learned something today.
Copy link to clipboard
Copied
Amazing. This just works.
Thank you very much!
Copy link to clipboard
Copied
When you make your PDF the way Kurt describes—save as .ai with “Include Linked Files” on, then reopen that .ai and resave it as .pdf—do the embedded images keep their correct size and position?
If so, there’s almost certainly a serious bug in the Links palette and/or ExtendScript’s “Embed” command. I was unable to recreate the problem you describe using a mockup PDF, but you’re welcome to send us a problematic PDF plus its linked images if you want us to test that.
You can also submit a bug report to https://illustrator.uservoice.com/, including a sample PDF and instructions/script to reproduce the problem using it. I don’t know how often Adobe reads those reports, but artwork content unexpectedly resizing/repositioning is a serious bug so definitely worth reporting!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now