ai2html
ai2html is an open-source script for Adobe Illustrator that converts your Illustrator documents into html and css.
Here are examples of how we’ve used the script at The New York Times and examples of how others have used it. Share your ai2html projects on Twitter, Delicious, etc. using #ai2html.
Download the latest version of the script here by clicking the link and saving the file to your computer.
Move the ai2html.js
file into the Illustrator folder where scripts are located. For example, on a Mac running Adobe Illustrator CC 2015, the path would be:
/Applications/Adobe Illustrator CC 2015/Presets/en_US/Scripts/ai2html.js
Document Color Mode
is set to RGB
.fonts
array in the script.File > Scripts > ai2html
ai2html-output
. Open the html files in your browser to preview your output.There are several ways of customizing the output of the script:
The script will look for files named ai2html-config.json
in two locations: the Illustrator scripts folder where the ai2html.js
file is located and the folder containing your .ai
file. Any settings contained in these files will override the default program settings, and will be overridden by any settings contained in the .ai
file (see “Special text blocks” below). Configuration data should match the format of the defaultSettings
object near the top of the ai2html.js
file.
New If the config file includes "create_text_block": false
, then ai2html will not create the settings text block described below.
The script recognizes five special types of text blocks. The first line of the text block should begin with ai2html-
followed by one of the following keywords: settings
, css
, js
, html
or text
. This should be the only thing on the first line of the text block. The special text blocks can be placed anywhere in your Illustrator document, but note that if you place them on an artboard, their contents will be rendered in your output.
ai2html-settings
When you run ai2html for the first time in your ai file, the script will place a settings text block to the upper left of all your artboards. Here is a description of the settings:
Options that are included in the settings text block by default:
png
png24
jpg
svg
auto
auto
auto
option uses png
by default, or jpg
if the graphic includes a raster image, which is likely to be continuous-tone.fixed
dynamic
fixed
dynamic
responsiveness means that the ai2html divs will scale to fill the container they are placed in.fixed
responsiveness means that the ai2html div will appear only at the size it was created in Illustrator and will not change sizes if the container it is placed in changes size.one-file
multiple-files
one-file
one-file
output means that all artboards will be written into one html file.multiple-files
output means that each artboard will be written to separate html files./ai2html-output/
.html
<blank>
html_output_path
.image_output_path
will be usedhtml_source_path
to specify the location from where the images are loaded in the <img>
tags. This does not change where image files are stored.<blank>
<%=headline%>
) notation. The ai2html partial can be inserted using the <%=ai2htmlPartial%>
variable.2
to 256
128
png
or png24
images.0
to 100
60
jpg
images.local_preview_template
and can be written to a “config” text file along with the html. The config file is written in yml can be used to pass parameters to a cms.Other options that can be added to the settings text block (or an ai2html-config.json file):
fixed
dynamic
dynamic
<blank>
max_width
inserts a max-width css instruction on the div containing the ai2html partial. The width should be specified in pixels.true
false
false
true
if you want the script to also output a text file in yml format containing the parameters you want to pass to your cms.<blank>
/ai2html-output/config.yml
yes
no
no
image_format: png24
can help prevent this.true
false
true
true
on this option causes the ai2html div to be centered within the container it is placed in.yes
no
yes
yes
on this setting tells the script to output images at double the resolution if possible. The script uses limits specified in this document to determine if the size of the image is too large to make it double resolution.<blank>
<a>
tag pointing to the given URL.yes
no
no
yes
on this setting causes the text in the file to be rendered both on the image and in the html. This is useful for testing whether the placement of html text is consistent with the Illustrator file.html
image
html
image
on this setting causes rotated and sheared text in the file to be rendered on the image instead of the html. See example.[ai file name]
project_name
to overwrite this. The project name will be used to name the exported HTML and image files.ai2html-css
If you want to add some css that is always inserted into your html partial, include it in a text block somewhere in your Illustrator document, but not on an artboard. Make the first line of the text block read ai2html-css
. The css will be added inside <style>
tags so don’t include <style>
tags in the text block.
ai2html-js
If you want to add some javascript that is always inserted into your html partial, include it in a text block somewhere in your Illustrator document, but not on an artboard. Make the first line of the text block read ai2html-js
. The js will be added at the end of all the html for each artboard. You will need to include the enclosing <script>
tags in the text block.
ai2html-html
If you want to add some html that is always inserted into your html partial, include it in a text block somewhere in your Illustrator document, but not on an artboard. Make the first line of the text block read ai2html-html
. The html will be added at the end of all the divs for each artboard.
ai2html-text
You can store text into variables and insert them into your document using basic mustache or erb/ejs notation. Any variables in the ai2html-settings
or in ai2html-text
blocks can be used.
To assign text to a variable, create a text block somewhere in your Illustrator document, but not on an artboard. Make the first line of that text block read ai2html-text
. Each subsequent paragraph should be in the format of variable_name: Lorem ipsum dolor.
. Variable names should only be letters, numbers and underscores.
Now you can insert that text anywhere in your document by placing {{variable_name}} or <%=variable_name%>
where you want that text to appear.
Note that you can pass mustache or erb/ejs notation untouched to your html partial as long as the variable names don’t match the variable names in the ai2html-settings
or ai2html-text
blocks.
g-
appended to the front of the name.id
to the div
that corresponds to that text block. Note that g-
is not appended to the beginning of the id
, so take care that you begin the id
with letters and not numbers.id
of the div
corresponding to that artboard. They should have unique names.-
, as the first character of an artboard name to tell the script that you don’t want it to be included in the output.:
followed by the minimum width up to which the artboard can be scaled down to. See example.Parameters can be attached to a text object and passed to the script using the notes field of the Attributes palette. The variables in the notes field should be in the format of key: value
. There is currently only one text-object parameter that can be specified here:
top
bottom
middle
middle
for Illustrator point text, top
for area text.valign: bottom
.responsiveness: dynamic
in the settings text block, but can also make a difference for area-text objects because text often wraps differently in different browsers so that a text block may be four lines in one browser and five lines in another. With the default valign: top
, the fifth line will be added to the bottom of the text block. With valign: bottom
the fifth line will cause the entire text block to be shifted up one line.The script handles point text and area text slightly differently which has ramifications on how text wraps on your web page. Fonts never appear identically in Illustrator and in web browsers. For example, the versions of Arial in Illustrator, in Chrome on a Mac and in Internet Explorer on Windows are not exactly the same — so text that fits in a box in Illustrator may be longer on IE or shorter in Chrome.
valign
discussion).The script processes each text object in your Illustrator file and translates the object and text attributes into inline and css styles. Each point- or area-text object is converted into a <div>
. Each paragraph is converted into <p>
tags within the <div>
.
<div>
<p>
tags
fonts
array to add your custom web fonts.The script renders text as absolutely positioned html elements. The remaining art is exported as an image that is placed underneath the text in the html. Artboards can be rendered as separate divs in a single file, or as separate files. The exported files are html partials, that is, everything is enclosed in a div that can be inserted into a page template. It is also possible to specify an html page template into which the script will insert the html partial so you can preview your artwork in the context of your site architecture and css.
Text styles are applied at the paragraph level. Each paragraph is given the character and paragraph attributes of the middle character of the paragraph. Other character styles within a paragraph are ignored — though we’re hoping to add this as a feature in the future. A work-around for this limitation is to enclose text in classed <span>
tags and define styles for those classes in an ai2html-css
text block (described below).
Paragraphs are styled using css classes that are consolidated across each artboard. This means that all paragraphs with the same style attributes are styled with a single css class. Text blocks in the output are ordered top-to-bottom, left-to-right so that the document is somewhat readable.
If you want to use fonts other than Arial and Georgia, you can add them to a fonts
array in an ai2html-config.json
config file (see above). You will need to know how Illustrator refers to the font. Enter this name as the aifont
property.
You can find the names that Illustrator uses for all the fonts used in your document by choosing Find Font…
from the Type
menu. You can also use the aifontname script, which will tell you the fonts used for selected text blocks.
You will need to assign each Illustrator font correspond to a specific font-family, weight and style, which will be used by the script to write the css that will be applied to your text. For example, the Illustrator font name Arial-BoldItalicMT
corresponds to this css:
font-family: arial,helvetica,sans-serif;
font-weight: bold;
font-style: italic;
The Github repository for this site is available at newsdev/ai2html.
Many thanks to Gregor Aisch, Matthew Bloch, Derek Watkins, Josh Katz, K.K. Rebecca Lai, Tom Giratikanon, Matt Ericson, Jeremy Ashkenas and Alan McLean for their incredible contributions to this project, as well as to my colleagues in The New York Times Graphics Department for their patient guidance.
If you’re learning to write Javascript for Adobe Illustrator, John Wundes, has many wonderful scripts. explore.js is particularly helpful for understanding what attributes are attached to Illustrator objects.
Created by Archie Tse / The New York Times
Copyright (c) 2011-2017 The New York Times Company