ai2html is a script for Adobe Illustrator that converts your Illustrator document into html and css.
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 Mac OS X running Adobe Illustrator CC 2014, the path would be:
Applications/Adobe Illustrator CC 2014/Presets/en_US/Scripts/ai2html.jsx
Document Color Mode is set to RGB.fonts array in the script.File > Scripts > ai2htmlai2html-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 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-settingsMost of the script’s options are set in the settings text block. 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 jpgpngfixed dynamicfixeddynamic 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-filesone-fileone-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.<blank><%=headline%>) notation. The ai2html partial can be inserted using the <%=ai2htmlPartial%> variable.2 to 256128png or png24 images.0 to 10060jpg images.local_preview_tempate 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:
<blank>max_width inserts a max-width css instruction on the div containing the ai2html partial. The width should be specified in pixels.true falsefalsetrue 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.ymlyes nonotrue falsetruetrue on this option causes the ai2html div to be centered within the container it is placed in.yes noyesyes 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.yes nonoyes 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.If you want to edit the script to change the default settings, you may find it helpful to make a copy of this Google spreadsheet which makes it easier to view and edit the settings.
ai2html-cssIf 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-jsIf 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-htmlIf 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-textYou 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.
Layer names
ai2html- appended to the front of the name.Object names
id to the div that corresponds to that text block. Note that ai2html- is not appended to the beginning of the id, so take care that you begin the id with letters and not numbers.Artboard names
id of the div corresponding to that artboard. It is not necessary to rename the artboards from the Illustrator defaults.Specifying artboards to ignore
-, as the first character of an artboard name to tell the script that you don’t want it to be included in the output.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 bottomtopvalign: 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.
Point text
valign discussion).Area text
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>.
Added to inline styles on the <div>
Added to CSS classes applied to <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.
Here are examples of how we’ve used the script at The New York Times.
Works well
Not so great
The Github repository for this site is available at newsdev/ai2html.
Many thanks to Jeremy Ashkenas, Matt Ericson, Tom Giratikanon, Alan McLean and my colleagues in The New York Times Graphics Department for their patient guidance in the development of this project.
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-2015 The New York Times Company