简单来说
一家德国窗户修缮作坊的两页报价信。示例演示怎样按德国标准摆放地址等各部分,让信纸正好装进开窗信封。
成品一览
Fensterwerkstatt Tessin是柏林一家虚构的作坊,专门修复箱式窗。它给一栋公寓楼的物业管理方寄去一份报价单。报价单用德文写成,共两张A4纸,按DIN 5008的B式排版。顶部一条柏林蓝色带印着作坊的标志和名称。色带下方,每个部分都放在标准规定的位置:回寄地址行和收信地址位于85 × 45 mm的地址栏内,这正是DL信封窗口露出的区域;信息栏距左边缘125 mm;粗体事由在地址栏下方空两行;折叠标记和打孔标记位于105、148.5和210 mm处。两页的页脚都用三栏排出公司信息。第二页在“Seite 2 von 2”旁重复编号,最后是一个用代码画出的签名。
这道食谱解答
- 怎样把徽标或贴纸固定在页面上的某个位置?
- 怎样给每一页加水印、背景底色或装饰图片?
- 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?
- 怎样排题词、献词、署名,或带大号引号的引文框?
简短回答
const at = (x, y, size) => ({ anchor: { to: 'page', edge: 'top-left' }, // mm from the corner
offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const ZONE = DIN.field.y + DIN.notes; // 62.7 mm: where the six address lines start
const SUBJECT = DIN.field.y + DIN.field.h + 2 * LEAD * PT; // two blank lines under the field
const firstPage = () => ({ enabled: true, slot: { elements: [ // in paint order
...letterhead(), // band, logo and name, and the corners of the window
text('from', FROM, FACE.from, at(DIN.left, ZONE - 3.6)), // the return line, underlined
rule('from-rule', 'muted', { anchor: { to: '#from', edge: 'below' }, offset: { y: mm(0.6) },
size: { width: mm(WINDOW) } }),
// One attribute holds the whole address: each '\n' in it starts a new line.
text('to', '{attr.to}', FACE.address, at(DIN.left, ZONE, { width: mm(WINDOW) })),
...infoBlock(), // labels, and values from {attr.*}
text('subject', '{titleText}', FACE.subject, at(DIN.left, SUBJECT, { width: mm(TEXT) })),
] } });
// The H1 reserves the sheet down to its lowest element (gotcha: opener-reserves-anchored).
// fontFamily: the design paints the H1, but the build measures it (default: Open Sans).
const headings = () => ({ fontFamily: 'Familjen Grotesk', levels: [{ level: 1,
breakBefore: { enabled: true, parity: 'any' }, // a letter starts on the next sheet
marginBottom: pt(2 * LEAD), // two blank lines between the subject and the salutation
advancedDesign: firstPage() }] });
用料
做法
#1 · 以毫米为单位把第一页固定在纸上
const at = (x, y, size) => ({ anchor: { to: 'page', edge: 'top-left' }, // mm from the corner
offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const ZONE = DIN.field.y + DIN.notes; // 62.7 mm: where the six address lines start
const SUBJECT = DIN.field.y + DIN.field.h + 2 * LEAD * PT; // two blank lines under the field
const firstPage = () => ({ enabled: true, slot: { elements: [ // in paint order
...letterhead(), // band, logo and name, and the corners of the window
text('from', FROM, FACE.from, at(DIN.left, ZONE - 3.6)), // the return line, underlined
rule('from-rule', 'muted', { anchor: { to: '#from', edge: 'below' }, offset: { y: mm(0.6) },
size: { width: mm(WINDOW) } }),
// One attribute holds the whole address: each '\n' in it starts a new line.
text('to', '{attr.to}', FACE.address, at(DIN.left, ZONE, { width: mm(WINDOW) })),
...infoBlock(), // labels, and values from {attr.*}
text('subject', '{titleText}', FACE.subject, at(DIN.left, SUBJECT, { width: mm(TEXT) })),
] } });
// The H1 reserves the sheet down to its lowest element (gotcha: opener-reserves-anchored).
// fontFamily: the design paints the H1, but the build measures it (default: Open Sans).
const headings = () => ({ fontFamily: 'Familjen Grotesk', levels: [{ level: 1,
breakBefore: { enabled: true, parity: 'any' }, // a letter starts on the next sheet
marginBottom: pt(2 * LEAD), // two blank lines between the subject and the salutation
advancedDesign: firstPage() }] });
at()用元素的左上角把它锚定到'page',也就是成品尺寸框(元素定位),所以每个x和y都是到纸张角点的距离,标准里的数字可以原样填入:收信地址横向25 mm、纵向62.7 mm,信息栏的第一个标签在125和50处。这些元素构成H1的版面设计。标题的文字是事由({titleText}),标题属性保存收信地址和各项编号,于是配置管信纸,Markdown管信的内容。事由位于100.6 mm处,是最低的元素,所以标题把纸面一直预留到事由的底部。marginBottom加上两个空行,称呼从下一个网格行开始,位于117 mm处。
收信地址是一个属性,各行之间用\n分隔,每个\n都在设计文本里另起一行。标题留在正文栏内,它的版面设计照样把色带、标志和名称画在27 mm的上页边距之上,因为每个元素都固定在页面上。
#2 · 从一个固定的标签串起整个信息栏
const INFO = [['Ihr Zeichen', 'ihr-zeichen'], ['Ihre Nachricht vom', 'ihre-nachricht'],
['Unser Zeichen', 'unser-zeichen'], ['Name', 'name'], ['Telefon', 'telefon'],
['E-Mail', 'email'], ['Datum', 'datum']]; // [label, heading attribute]
const VALUE = 29; // mm from the labels to the values
const infoBlock = () => INFO.flatMap(([label, key], i) => [
text(`label-${i}`, label, FACE.label, i === 0 ? at(DIN.info.x, DIN.info.y) : {
anchor: { to: `#label-${i - 1}`, edge: 'below' }, // each label under the last
offset: { y: mm(key === 'datum' ? ROW * PT : 0) } }), // a blank row before the date
text(`value-${i}`, `{attr.${key}}`, FACE.value, { // a 12 pt row as well, on the label's baseline
anchor: { to: `#label-${i}`, edge: 'align-top' }, offset: { x: mm(VALUE) },
size: { width: mm(DIN.info.w - VALUE) } }),
]);
只有第一个标签固定在纸上。其余每个标签都用'below'锚定在前一个标签下方,每个值用'align-top'和29 mm的偏移锚定在自己的标签旁,所以在INFO里添加一行不需要任何坐标。设计文本把基线放在行框的80%处。7 pt的标签和9 pt的值都设lineHeight: pt(ROW),即12 pt的行,所以在每个4.23 mm的行上共用一条基线。
#3 · 把信头画进同一个版面设计
const [BAND, TICK] = [32, 3]; // mm: the band's depth, each arm of the window's corner angles
const LOGO = { y: 7, h: 18 }; // mm: the mark, centred in the band
const CRAFTS = 3 * 7.5 * 1.45 * PT; // mm: three 7.5 pt lines at 1.45
const letterhead = () => [
{ kind: 'box', id: 'band', style: { backgroundColor: col('brand') },
placement: at(0, 0, { width: 'fill', height: mm(BAND) }) },
{ kind: 'image', id: 'logo', resourceId: 'logo',
placement: at(DIN.left, LOGO.y, { height: mm(LOGO.h) }) },
text('name', 'Tessin', face('Familjen Grotesk', 30, 700, 'paper', { lineHeight: 1 }),
{ anchor: { to: '#logo', edge: 'right-of' }, offset: { x: mm(5), y: mm(1.2) } }),
text('trade', 'FENSTERWERKSTATT · BERLIN', face('Reddit Mono', 7.5, 500, 'sky',
{ letterSpacing: pt(1.2) }), { anchor: { to: '#name', edge: 'below' },
offset: { x: mm(0.4), y: mm(1.5) } }),
text('crafts', 'Kastenfenster\nHolzfenster\nDenkmalpflege', face('Reddit Mono', 7.5,
400, 'sky', { lineHeight: 1.45, align: 'right' }),
at(210 - DIN.right - 45, LOGO.y + (LOGO.h - CRAFTS) / 2, { width: mm(45) })),
...[[0, 0], [1, 0], [0, 1], [1, 1]].flatMap(([right, low], i) => { // turned inwards
const [x, y] = [DIN.field.x + right * DIN.field.w, DIN.field.y + low * DIN.field.h];
return [rule(`across-${i}`, 'marks', at(x - right * TICK, y, { width: mm(TICK) })),
rule(`down-${i}`, 'marks', at(x, y - low * TICK, { height: mm(TICK) }), 'vertical')];
}),
];
色带是一个固定在纸张角上的框。width: 'fill'把它从一边拉到另一边,32 mm的高度使它越过27 mm的上页边距5 mm。标志是一个图像元素,把一个资源画成18 mm高,宽度由图形本身决定(图像元素)。名称用'right-of'挂在标志右侧,行业说明用'below'挂在名称下方。八条3 mm长的线在85 × 45 mm地址栏的每个角上画出一个角标,这个区域就是信封窗口露出的部分。
#4 · 把每页共有的内容放进页眉和页脚
const header = { elements: [
// In the H1's design they would stretch its reserve (gotcha: opener-reserves-anchored).
...[[DIN.folds[0], 5], [DIN.punch, 8], [DIN.folds[1], 5]].map(([y, length], i) =>
rule(`mark-${i}`, 'marks', at(0, y, { width: mm(length) }))), // fold, punch, fold
// Page 1 opens with the H1, which makes it an 'opener'; later sheets are 'body' pages.
{ kind: 'box', id: 'strip', pages: 'body', style: { backgroundColor: col('brand') },
placement: at(0, 0, { width: 'fill', height: mm(4) }) },
text('ref', 'Fensterwerkstatt Tessin · Unser Zeichen {attr.unser-zeichen} · {attr.datum}',
FACE.small, at(DIN.left, 13.2), { pages: 'body' }),
text('page', 'Seite {pageNumber} von {totalPages}', FACE.small,
at(210 - DIN.right - 40, 13.2, { width: mm(40) }), { pages: 'body', align: 'right' }),
] };
const FOOT = 297 - DIN.bottom + 5; // mm: the hairline over the company data
const COMPANY = [ // three columns, 57 mm apart, inside the bottom margin
'Fensterwerkstatt Tessin GmbH\nRennbahnstraße 48\n13086 Berlin\nTelefon 030 23125-400',
'Geschäftsführer Martin Tessin\nAmtsgericht Charlottenburg\nHRB 000000 B\nUSt-IdNr. DE000000000',
'Musterbank Berlin\nIBAN DE00 0000 0000 0000 0000 00\nBIC MUSTDEBBXXX',
];
const footer = { elements: [
rule('foot-rule', 'rule', at(DIN.left, FOOT, { width: mm(TEXT) })),
...COMPANY.map((lines, i) => text(`company-${i}`, lines, FACE.small,
at(DIN.left + 57 * i, FOOT + 2.5, { width: mm(52) }))),
] };
H1版面设计中固定在事由下方的元素会增加标题预留的高度。如果把210 mm处的折叠标记放进版面设计,称呼就会降到222.8 mm,信会排成三页。页眉在每一页上绘制,不预留空间,所以三个标记放在那里(页眉与页脚)。第1页以H1开始,因此是'opener'页,pages: 'body'把蓝条、编号行和Seite {pageNumber} von {totalPages}限制在后面的页上。编号行通过{attr.unser-zeichen}和{attr.datum}读取H1的属性。
#5 · 把签名排进正文流
const resourceTypes = [{ id: 'drawing', name: 'Zeichnung', shortLabel: 'Zeichnung',
numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal', captionPrefix: '' }];
const drawing = (id, width, height, altText, placement) => ({ id, typeId: 'drawing',
kind: 'svg', svg: { fileId: `${id}.svg`, width, height }, altText, placement,
createdAt: 0, updatedAt: 0 });
const resources = [ // an image element draws the logo; nothing cites it
drawing('logo', 200, 280, 'Zeichen der Fensterwerkstatt Tessin, ein Kastenfenster'),
drawing('signature', 760, 328, 'Unterschrift von Martin Tessin',
{ position: 'here', width: 0.297 }), // 49 × 21.1 mm: under 4 lines, no grid gap below
];
::resource{id="signature"}把图形放在“Mit freundlichen Grüßen”下方,width: 0.297使它宽49 mm、高21.1 mm,略低于四个15 pt的行。正文流中资源之后的文字会回到正文的网格。设为0.25时,图形高17.8 mm,网格在它下方补上的3.4 mm把姓名推离了签名。这个资源类型的captionPrefix为空,而当前缀和题注都为空时,Postext不排题注,所以签名下方不会出现标签或编号(资源类型)。标志也是同一类型的资源,只由图像元素绘制。没有任何:ref或::resource引用它,所以它永远不会进入正文流。
完整食谱
// ═══ Postext Cookbook · Nº 058 · Business letter to DIN 5008 ═════════════════════
// https://postext.dev/en/cookbook/din-business-letter
// Code: MIT · Text: original, in German (CC BY 4.0) · Logo, signature: drawn in code (CC BY 4.0)
// Fonts: Nunito Sans, Familjen Grotesk, Reddit Mono (SIL OFL 1.1) · Needs postext ≥ 1.34.0
// A German quotation on A4 whose H1 design pins the letterhead and the address to DIN 5008.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'din-business-letter';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = {
ink: '#1f2429', brand: '#1b4a73', // text, a blue-grey near-black; Berlin blue, for the band
sky: '#b4cbe0', paper: '#ffffff', // the trade and the crafts on the band; the logo and name
muted: '#5b6670', pen: '#26408f', // labels, return line, company data; the signature's ink
marks: '#98a3ad', rule: '#c9d1d8', // fold marks, window corners; the hairline in the footer
};
// col(id): a colour linked to its palette entry, with the entry's hex beside the id.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// main-color is the engine's default accent: the dashes of the list take it.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.brand })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// DIN 5008, form B: every position in mm from the sheet's top-left corner.
const DIN = {
left: 25, right: 20, top: 27, bottom: 30, // the margins of the text
field: { x: 20, y: 45, w: 85, h: 45 }, // the address field, behind the envelope window
notes: 17.7, // the field's top zone, for the return line; the address zone takes the rest
info: { x: 125, y: 50, w: 75 }, // the information block
folds: [105, 210], punch: 148.5, // fold marks for a DL envelope, and the punch mark
};
const TEXT = 210 - DIN.left - DIN.right; // 165 mm of text width
const WINDOW = DIN.field.w - 2 * (DIN.left - DIN.field.x); // 75 mm: text inset 5 mm each side
const PT = 25.4 / 72; // mm per point
const [LEAD, ROW] = [15, 12]; // pt: the body's leading, a row of the information block
// Design text is centred by default, and a header or footer line ends in '…': left, wrapped.
const text = (id, content, face, placement, more) => ({ kind: 'text', id, content,
align: 'left', overflow: 'wrap', ...face, placement, ...more }); // more: pages, align
const rule = (id, colour, placement, direction = 'horizontal') => ({ kind: 'rule', id,
direction, thickness: pt(0.5), color: col(colour), placement });
const face = (fontFamily, size, fontWeight, colour, more) => ({ fontFamily, fontSize: pt(size),
fontWeight, color: col(colour), ...more });
const FACE = { // lineHeight: a dimension is the line's depth, a number multiplies the size
label: face('Reddit Mono', 7, 400, 'muted', { lineHeight: pt(ROW) }),
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
常见问题
易错点
章首页预留的高度一直到最低的页面锚定元素
高级设计的章首页会预留到其最低元素为止的高度,标题下方锚定在页面或出血上的元素也算在内,所以页脚处的装饰会把正文推到下一页。给这样的元素设reserve: false(从postext 1.5起):它绘制在锚定的位置,不预留任何空间。跟随区段本身的元素(锚定在区段底边,或高度为'fill')从不决定区段的高度。 设计过的章首页 →
{totalPages}统计的是整个文档的页数,所以如果一个Markdown文件里有两封信,第一封信的第二页会印成“Seite 2 von 4”。每封信要单独排。
致谢
- 文本
- 原创文字, CC BY 4.0
- 字体
- Nunito Sans (SIL OFL 1.1) · Familjen Grotesk (SIL OFL 1.1) · Reddit Mono (SIL OFL 1.1)


