unlayer-export
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseExport Content
导出内容
Overview
概述
Unlayer supports multiple export formats. Some are client-side (free), others use the Cloud API (paid).
Unlayer支持多种导出格式,部分为客户端免费功能,其余则需使用付费的Cloud API。
Which Export Method?
选择哪种导出方式?
| Method | Output | Paid? | Use When |
|---|---|---|---|
| HTML + design JSON | No | Email sending, web publishing, saving designs |
| Plain text + design | No | SMS, accessibility fallback |
| PNG URL + design | Yes | Thumbnails, previews, social sharing |
| PDF URL + design | Yes | Print-ready documents |
| ZIP URL + design | Yes | Offline download packages |
Critical: Always save the design JSON alongside any export. All export methods return— save it so users can edit later.data.design
| 方式 | 输出格式 | 是否付费 | 适用场景 |
|---|---|---|---|
| HTML + design JSON | 否 | 邮件发送、网页发布、保存设计 |
| 纯文本 + 设计数据 | 否 | SMS短信、无障碍内容备选 |
| PNG链接 + 设计数据 | 是 | 缩略图、预览图、社交分享 |
| PDF链接 + 设计数据 | 是 | 可打印文档 |
| ZIP链接 + 设计数据 | 是 | 离线下载包 |
重要提示: 导出任何内容时,请务必同时保存design JSON。所有导出方法都会返回——请保存该数据,以便用户后续编辑。data.design
Save & Load Designs
保存与加载设计
javascript
// SAVE — use exportHtml to get both design JSON and HTML
unlayer.exportHtml(async (data) => {
await fetch('/api/templates', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
design: data.design, // Save this — needed to edit later
html: data.html, // The rendered HTML output
}),
});
});
// LOAD — restore a saved design (must wait for editor:ready)
unlayer.addEventListener('editor:ready', async () => {
const response = await fetch('/api/templates/123');
const saved = await response.json();
unlayer.loadDesign(saved.design); // Pass the saved JSON object
});
// LOAD BLANK
unlayer.loadBlank({ backgroundColor: '#ffffff', contentWidth: '600px' });
// LOAD AN UNLAYER TEMPLATE
unlayer.loadTemplate(templateId); // ID from Unlayer dashboardjavascript
// SAVE — use exportHtml to get both design JSON and HTML
unlayer.exportHtml(async (data) => {
await fetch('/api/templates', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
design: data.design, // Save this — needed to edit later
html: data.html, // The rendered HTML output
}),
});
});
// LOAD — restore a saved design (must wait for editor:ready)
unlayer.addEventListener('editor:ready', async () => {
const response = await fetch('/api/templates/123');
const saved = await response.json();
unlayer.loadDesign(saved.design); // Pass the saved JSON object
});
// LOAD BLANK
unlayer.loadBlank({ backgroundColor: '#ffffff', contentWidth: '600px' });
// LOAD AN UNLAYER TEMPLATE
unlayer.loadTemplate(templateId); // ID from Unlayer dashboardExport HTML
导出HTML
javascript
unlayer.exportHtml((data) => {
const { html, design, chunks } = data;
// html — Full HTML document (string)
// design — Design JSON (always save this!)
// chunks — { css, js, body, fonts, tags }
// body — Just the content inside <body> (no wrapper)
// css — Extracted CSS styles
// fonts — Web fonts used in the design
// Save both to your backend
await fetch('/api/templates', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ design, html }),
});
}, {
// All options are optional
cleanup: true, // Remove editor markup (default: true)
minify: false, // Minify HTML output
inlineStyles: false, // Move CSS inline (for email clients)
mergeTags: {}, // Replace merge tags with real values
title: 'My Email', // Set HTML <title>
});Using chunks — when you need just the body content (no wrapper):
<!DOCTYPE>javascript
unlayer.exportHtml((data) => {
const { body, css, fonts } = data.chunks;
const myHtml = `<style>${css}</style>${fonts}${body}`;
});javascript
unlayer.exportHtml((data) => {
const { html, design, chunks } = data;
// html — Full HTML document (string)
// design — Design JSON (always save this!)
// chunks — { css, js, body, fonts, tags }
// body — Just the content inside <body> (no wrapper)
// css — Extracted CSS styles
// fonts — Web fonts used in the design
// Save both to your backend
await fetch('/api/templates', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ design, html }),
});
}, {
// All options are optional
cleanup: true, // Remove editor markup (default: true)
minify: false, // Minify HTML output
inlineStyles: false, // Move CSS inline (for email clients)
mergeTags: {}, // Replace merge tags with real values
title: 'My Email', // Set HTML <title>
});使用chunks — 当你仅需要主体内容(不含包裹)时:
<!DOCTYPE>javascript
unlayer.exportHtml((data) => {
const { body, css, fonts } = data.chunks;
const myHtml = `<style>${css}</style>${fonts}${body}`;
});Export Plain Text
导出纯文本
javascript
unlayer.exportPlainText((data) => {
const { text, design } = data;
// Use as email plain-text fallback
}, {
ignorePreheader: false,
ignoreLinks: false,
ignoreImages: false,
mergeTags: {},
});javascript
unlayer.exportPlainText((data) => {
const { text, design } = data;
// Use as email plain-text fallback
}, {
ignorePreheader: false,
ignoreLinks: false,
ignoreImages: false,
mergeTags: {},
});Export Image (Paid — Cloud API)
导出图片(付费——Cloud API)
Generates a PNG screenshot of the design. The image uploads to your connected File Storage.
Client-side:
javascript
unlayer.exportImage((data) => {
// data.url — PNG URL
// data.design — Design JSON (always save this!)
console.log('Image URL:', data.url);
}, {
fullPage: false, // true = entire page, false = viewport
mergeTags: {},
});Server-side via Cloud API (get API key from Dashboard > Project > Settings > API Keys):
javascript
const response = await fetch('https://api.unlayer.com/v2/export/image', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + Buffer.from('YOUR_API_KEY:').toString('base64'),
},
body: JSON.stringify({
displayMode: 'email',
design: designJSON, // The saved design JSON object
mergeTags: {},
}),
});
const data = await response.json();
// data.url — image URL生成设计的PNG截图,图片会上传至你关联的文件存储服务。
客户端实现:
javascript
unlayer.exportImage((data) => {
// data.url — PNG URL
// data.design — Design JSON (always save this!)
console.log('Image URL:', data.url);
}, {
fullPage: false, // true = entire page, false = viewport
mergeTags: {},
});通过Cloud API服务端实现(从控制台>项目>设置>API密钥获取API密钥):
javascript
const response = await fetch('https://api.unlayer.com/v2/export/image', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + Buffer.from('YOUR_API_KEY:').toString('base64'),
},
body: JSON.stringify({
displayMode: 'email',
design: designJSON, // The saved design JSON object
mergeTags: {},
}),
});
const data = await response.json();
// data.url — image URLExport PDF / ZIP (Paid — Cloud API)
导出PDF / ZIP(付费——Cloud API)
javascript
// PDF
unlayer.exportPdf((data) => {
// data.url — PDF URL
// data.design — Design JSON
}, { mergeTags: {} });
// ZIP
unlayer.exportZip((data) => {
// data.url — ZIP URL
// data.design — Design JSON
}, { mergeTags: {} });javascript
// PDF
unlayer.exportPdf((data) => {
// data.url — PDF URL
// data.design — Design JSON
}, { mergeTags: {} });
// ZIP
unlayer.exportZip((data) => {
// data.url — ZIP URL
// data.design — Design JSON
}, { mergeTags: {} });Auto-Save Pattern
自动保存模式
Design + HTML (recommended):
设计数据 + HTML(推荐):
javascript
let saveTimeout;
unlayer.addEventListener('design:updated', () => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
unlayer.exportHtml(async (data) => {
await fetch('/api/templates/123', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ design: data.design, html: data.html }),
});
});
}, 1000);
});javascript
let saveTimeout;
unlayer.addEventListener('design:updated', () => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
unlayer.exportHtml(async (data) => {
await fetch('/api/templates/123', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ design: data.design, html: data.html }),
});
});
}, 1000);
});Design + HTML + Thumbnail (full):
设计数据 + HTML + 缩略图(完整方案):
javascript
let saveTimeout;
unlayer.addEventListener('design:updated', () => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
// Save design + HTML immediately
unlayer.exportHtml(async (data) => {
await fetch('/api/templates/123', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ design: data.design, html: data.html }),
});
});
// Generate thumbnail (slower, paid — debounce longer or do on manual save)
unlayer.exportImage(async (data) => {
if (!data.url) return;
await fetch('/api/templates/123/thumbnail', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ thumbnailUrl: data.url }),
});
}, { fullPage: false });
}, 3000); // Longer debounce for image generation
});javascript
let saveTimeout;
unlayer.addEventListener('design:updated', () => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
// Save design + HTML immediately
unlayer.exportHtml(async (data) => {
await fetch('/api/templates/123', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ design: data.design, html: data.html }),
});
});
// Generate thumbnail (slower, paid — debounce longer or do on manual save)
unlayer.exportImage(async (data) => {
if (!data.url) return;
await fetch('/api/templates/123/thumbnail', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ thumbnailUrl: data.url }),
});
}, { fullPage: false });
}, 3000); // Longer debounce for image generation
});Design JSON Quick Reference
设计JSON快速参考
The design JSON has this structure (see references/design-json.md for full TypeScript types):
JSONTemplate
├── counters — Internal counters
├── schemaVersion — Currently 16
└── body
├── rows[] — Each row contains columns
│ ├── cells[] — Column ratios: [1,1] = 50/50
│ └── columns[]
│ └── contents[] — Content items (text, image, button...)
├── headers[] — Same as rows (with headersAndFooters feature)
├── footers[] — Same as rows
└── values — Body-level styles (backgroundColor, contentWidth, fontFamily)Content types: , , , , , , , , , .
textheadingbuttonimagedividersocialhtmlvideomenutimer设计JSON的结构如下(完整TypeScript类型请参考references/design-json.md):
JSONTemplate
├── counters — Internal counters
├── schemaVersion — Currently 16
└── body
├── rows[] — Each row contains columns
│ ├── cells[] — Column ratios: [1,1] = 50/50
│ └── columns[]
│ └── contents[] — Content items (text, image, button...)
├── headers[] — Same as rows (with headersAndFooters feature)
├── footers[] — Same as rows
└── values — Body-level styles (backgroundColor, contentWidth, fontFamily)内容类型包括:、、、、、、、、、。
textheadingbuttonimagedividersocialhtmlvideomenutimerCommon Mistakes
常见错误
| Mistake | Fix |
|---|---|
| Only saving HTML, not design JSON | Always save both — all export methods return |
Calling export before | Wait for the event first |
| Not configuring File Storage for image/PDF export | Image and PDF uploads go to your connected File Storage |
| Not debouncing auto-save | |
Ignoring | Use |
| Missing API key for image/PDF/ZIP | Cloud API key required — get from Dashboard > Project > Settings > API Keys |
| 错误 | 修复方案 |
|---|---|
| 仅保存HTML,未保存设计JSON | 务必同时保存两者——所有导出方法都会返回 |
在 | 先等待该事件触发 |
| 未为图片/PDF导出配置文件存储服务 | 图片和PDF会上传至你关联的文件存储服务,请先完成配置 |
| 未对自动保存做防抖处理 | |
忽略 | 当你需要不含 |
| 图片/PDF/ZIP导出时缺少API密钥 | 需使用Cloud API密钥——从控制台>项目>设置>API密钥获取 |
Troubleshooting
故障排查
| Problem | Fix |
|---|---|
| Check API key, check Cloud API plan, verify design isn't empty |
| Exported HTML looks different from editor | Use |
| Always debounce — it fires on every property change |
| Loaded design shows blank | Check |
| 问题 | 修复方案 |
|---|---|
| 检查API密钥、Cloud API套餐,确认设计内容非空 |
| 导出的HTML与编辑器预览效果不一致 | 使用默认的 |
| 务必添加防抖处理——该事件会在每次属性变更时触发 |
| 加载的设计显示空白 | 检查 |