Search Docs
Loading...
Skip to content

Folded Cards

Build a bi-fold greeting card and a tri-fold leaflet on a server, and clip the panels at the folds. Then change the paper format of the card and write the flat sheets as PDFs for print.

7 mins
estimated time
Download
StackBlitz
GitHub

We build an A5 greeting card and an A4 tri-fold leaflet, each with an outside and an inside sheet. We clip the outside panels and place one photo across the inside fold of the card. Then we change the card to half US Letter and write the four sheets to the output folder. To run the code, use npm install and then npm run dev in its folder.

This guide covers:

  • How a page with page slices makes a folded card
  • Building a bi-fold card and a tri-fold leaflet
  • Clipping the panels at the folds
  • Changing the paper format of a card
  • Exporting the flat sheets for a print shop

How a Folded Card Is Built#

A folded card is one flat sheet of paper with folds. CE.SDK builds each printed side of the sheet as one page. Each panel of that side is one page slice of the page, and the folds lie between the page slices. This guide calls a page a sheet and a page slice a panel, except next to an API name.

  • A bi-fold card has two panels on each side, so it needs two sheets: the outside and the inside.
  • A tri-fold leaflet has three panels on each side. One panel folds inside, so it is 3 mm narrower than the other two.
  • The other panels have the size of the folded card. Along the fold axis, a sheet is as long as all its panels together.
  • The print shop sets the bleed. This guide uses 3 mm on each side.
  • A tent card folds from top to bottom. For it, set page/pageSliceAxis to Vertical before you append the page slices.

Folded Products and Spreads explains the model, the size rules and the ownership rules of page slices.

Set Up the Engine#

Start the headless engine. The license line is commented out, so the engine runs in trial mode. To remove the watermark, set CESDK_LICENSE in .env and uncomment the license line. IMGLY_LOCAL_ASSETS_URL points to self-hosted assets. Leave it unset to load the assets from the IMG.LY CDN.

const engine = await CreativeEngine.init({
baseURL: process.env.IMGLY_LOCAL_ASSETS_URL
// license: process.env.CESDK_LICENSE,
});

Create the Scene#

Create a design scene and set its design unit to millimeters. All sizes in this guide are then in millimeters.

engine.scene.create('VerticalStack');
engine.scene.setDesignUnit('Millimeter');
const [stack] = engine.block.findByType('stack');

Create a Sheet#

A sheet is a page that holds one page slice per panel. To build one:

  1. Create a page and add it to the stack.
  2. Turn on its margin, which CE.SDK uses as the bleed, and set 3 mm on each side.
  3. Add one page slice per panel, each with its own width, height and name.

The helper below puts these steps together:

type Panel = { name: string; width: number; height: number };
const createSheet = (panels: Panel[]): DesignBlockId => {
const page = engine.block.create('page');
engine.block.setName(page, panels.map((panel) => panel.name).join(' | '));
engine.block.appendChild(stack, page);
// A 3 mm bleed on every side of the sheet
engine.block.setBool(page, 'page/marginEnabled', true);
for (const side of ['top', 'bottom', 'left', 'right']) {
engine.block.setFloat(page, `page/margin/${side}`, 3);
}
for (const { name, width, height } of panels) {
const pageSlice = engine.block.create('pageSlice');
// Size the page slice before it joins the page.
engine.block.setWidth(pageSlice, width);
engine.block.setHeight(pageSlice, height);
engine.block.setName(pageSlice, name);
engine.block.appendChild(page, pageSlice);
}
return page;
};

The sheet takes its size from its panels, so we never size the page.

Build a Bi-Fold Card#

The outside of a bi-fold card reads from left to right: the back, then the front. The front of the folded card is the right half of the outside sheet.

For an A5 card that folds to A6, each panel is 105 × 148 mm. Each sheet is then A5 in landscape.

// An A5 card folds to A6: two panels of 105 × 148 mm on each side.
const outside = createSheet([
{ name: 'Back', width: 105, height: 148 },
{ name: 'Front', width: 105, height: 148 }
]);
const inside = createSheet([
{ name: 'Inside Left', width: 105, height: 148 },
{ name: 'Inside Right', width: 105, height: 148 }
]);
const [, front] = engine.block.getPageSlices(outside);

Build a Tri-Fold Leaflet#

In a tri-fold leaflet, the inside flap folds in first. It is 3 mm narrower than the other two panels, so it fits inside without buckling. For an A4 leaflet of 297 × 210 mm in landscape:

  • The outside holds the inside flap, the back and the cover: 97, 100 and 100 mm.
  • The print shop turns the sheet over. So the inside holds the same widths in the opposite order: 100, 100 and 97 mm.
// The inside flap is 3 mm narrower, so it fits inside without buckling.
// The inside sheet holds the same widths in the opposite order.
const leafletOutside = createSheet([
{ name: 'Inside Flap', width: 97, height: 210 },
{ name: 'Back', width: 100, height: 210 },
{ name: 'Cover', width: 100, height: 210 }
]);
const leafletInside = createSheet([
{ name: 'Inside Left', width: 100, height: 210 },
{ name: 'Inside Middle', width: 100, height: 210 },
{ name: 'Inside Right', width: 97, height: 210 }
]);

Update the Layout#

The engine lays out a sheet on its next update. Call engine.update() before you read a size. Until then, getWidth on the page returns its earlier width, such as the default 100 mm. getOwningPageSlice, resizeContentAware and export lay out the sheet themselves.

engine.update();
console.log('Leaflet width (mm):', engine.block.getWidth(leafletOutside));

Add Content to the Panels#

We give the front a color fill and a title. We place one photo across the inside of the card, and a blue band on the leaflet cover. setFill on a page slice also turns its fill on.

const frontFill = engine.block.createFill('color');
engine.block.setColor(frontFill, 'fill/color/value', {
r: 0.55,
g: 0.12,
b: 0.2,
a: 1
});
engine.block.setFill(front, frontFill);
// Content is a child of the page, in the coordinates of the sheet.
const title = engine.block.create('text');
engine.block.appendChild(outside, title);
engine.block.replaceText(title, 'Happy Birthday');
engine.block.setTextFontSize(title, 24, { unit: 'Point' });
engine.block.setTextColor(title, { r: 1, g: 1, b: 1, a: 1 });
engine.block.setWidth(title, 85);
engine.block.setHeightMode(title, 'Auto');
engine.block.setPositionX(title, 115);
engine.block.setPositionY(title, 60);
// One photo across the inside, bleed included, centered on the fold
const photo = engine.block.create('graphic');
engine.block.setShape(photo, engine.block.createShape('rect'));
const photoFill = engine.block.createFill('image');
engine.block.setString(
photoFill,
'fill/image/imageFileURI',
'https://img.ly/static/ubq_samples/sample_1.jpg'
);
engine.block.setFill(photo, photoFill);
engine.block.appendChild(inside, photo);
engine.block.setPositionX(photo, -3);
engine.block.setPositionY(photo, -3);
engine.block.setWidth(photo, 216);
engine.block.setHeight(photo, 154);
// A band on the leaflet cover that reaches 20 mm onto the back and 3 mm
// into the bleed
const band = engine.block.create('graphic');
engine.block.setShape(band, engine.block.createShape('rect'));
const bandFill = engine.block.createFill('color');
engine.block.setColor(bandFill, 'fill/color/value', {
r: 0.13,
g: 0.36,
b: 0.6,
a: 1
});
engine.block.setFill(band, bandFill);
engine.block.appendChild(leafletOutside, band);
engine.block.setPositionX(band, 177);
engine.block.setPositionY(band, 20);
engine.block.setWidth(band, 123);
engine.block.setHeight(band, 40);

Place each block in the coordinates of the whole sheet. The origin is the top-left corner of the trim. The bleed starts at x = -3 mm and y = -3 mm.

  • The front starts at x = 105 mm. The title starts 10 mm inside it, at x = 115 mm.
  • The photo of the inside starts at x = -3 mm and y = -3 mm. It is 6 mm larger than the sheet, so it covers both panels and the bleed.
  • The center of the photo is on the fold, so Inside Left owns it. A clip on Inside Left would cut the photo at the fold.
  • The blue band of the leaflet has its center on the cover. It reaches 20 mm onto the back and 3 mm into the bleed.

Clip the Panels at the Folds#

setClipped on a panel cuts the blocks that the panel owns at its folds and at the edges of the sheet with their bleed. The panel under the center of a block owns the block. On a fold, the earlier panel owns it.

  • The cover of the leaflet owns the blue band. The clipped cover cuts the band at the fold to the back.
  • At an outer edge of the sheet, a clipped panel cuts at the edge of the bleed. The band reaches 3 mm into the bleed and stops there.
  • An unclipped panel lets a block cross the fold, such as the photo across the inside of the card.
  • Each panel has its own clip setting.
// A clipped panel cuts the blocks that it owns at its folds, so the band
// stops at the fold between the back and the cover of the leaflet.
for (const sheet of [outside, leafletOutside]) {
for (const panel of engine.block.getPageSlices(sheet)) {
engine.block.setClipped(panel, true);
}
}
// The inside panels stay open, so the photo crosses the fold.
const photoOwner = engine.block.getOwningPageSlice(photo);
if (photoOwner !== null) {
console.log('The photo belongs to:', engine.block.getName(photoOwner));
}

Change the Paper Format#

resizeContentAware on a page gives the whole sheet a new size. Each block stays on its panel.

  • Pass the size of the whole sheet, not the size of one panel. Half US Letter in landscape is 215.9 × 139.7 mm.
  • The panels keep the ratio of their widths, not the difference. So the flap of a leaflet is no longer exactly 3 mm narrower.
  • To keep the flap 3 mm narrower on a sheet of width W, set each other panel to (W + 3) / 3 mm. Set the flap to 3 mm less. Each block moves with its panel.
  • The resize also scales and moves the blocks, so a font size can change.
  • A page slice refuses the call with BLOCK.PAGE_SLICE_RESIZE_CONTENT_AWARE. Resize its page instead.
  • To offer paper formats, keep a list of sheet sizes in your app and pass the size that the user chooses.
// Pass the size of the whole sheet: half US Letter, landscape.
engine.block.resizeContentAware([outside, inside], 215.9, 139.7);
const titleOwner = engine.block.getOwningPageSlice(title);
console.log('The title stays on the front:', titleOwner === front);

Export the Sheets for Print#

A print shop wants each side of a card as one flat sheet, with the bleed, crop marks and fold marks. Export each sheet as a PDF with exportPdfWithCropMarks and exportPdfWithFoldMarks. A pair of fold marks shows each fold.

  • printMarkOffset is the distance from the trim to the marks, in design units. The default is 6 pt, so the marks reach into a 3 mm bleed. Set the offset to the bleed, here 3 mm.
  • A scene export gives one PDF page per page. To send both sides of a card in one PDF, keep each product in its own scene. Then export the scene.
  • Without a license, each export carries a watermark.

Export to PDF describes the marks, the TrimBox and the BleedBox, and the option exportPdfPageSlices that gives one PDF page per panel.

const outputDir = './output';
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
// One flat sheet per printed side, with crop and fold marks
for (const [fileName, sheet] of [
['card-outside.pdf', outside],
['card-inside.pdf', inside],
['leaflet-outside.pdf', leafletOutside],
['leaflet-inside.pdf', leafletInside]
] as const) {
const pdf = await engine.block.export(sheet, {
mimeType: 'application/pdf',
exportPdfWithCropMarks: true,
exportPdfWithFoldMarks: true,
// The marks start at the edge of the 3 mm bleed.
printMarkOffset: 3
});
writeFileSync(
`${outputDir}/${fileName}`,
Buffer.from(await pdf.arrayBuffer())
);
}

Clean Up#

Dispose of the engine in a finally block, so the engine also stops when an export fails.

engine.dispose();

Troubleshooting#

  • A photo shows on the next panel. The panel that owns the photo is not clipped. Call setClipped on that page slice.
  • A photo is cut off at the fold. The panel that owns the photo is clipped. Turn the clip off, or move the center of the photo onto the panel where it belongs.
  • The sheet has the wrong size after a format change. resizeContentAware got the size of one panel. Pass the size of the whole sheet.
  • A format change fails with BLOCK.PAGE_SLICE_RESIZE_CONTENT_AWARE. The call got a page slice. Pass its page.
  • A tent card has panels with width and height swapped. The axis changed after the page slices were appended. Set page/pageSliceAxis before you append them.
  • The marks print over the bleed. The default mark offset is 6 pt. Set printMarkOffset to the bleed.

API Reference#

API Purpose
engine.update() Lay out the sheets before you read a size.
engine.block.setBool(page, 'page/marginEnabled', true) Turn on the bleed of a sheet.
engine.block.setFloat(page, 'page/margin/top', 3) Set the bleed on one side of a sheet.
engine.block.create('pageSlice') Create a panel. Size it, then append it to a page.
engine.block.getPageSlices(page) Get the panels of a sheet in reading order.
engine.block.setClipped(pageSlice, clipped) Cut the blocks of a panel at its folds and at the page edges with their bleed.
engine.block.getOwningPageSlice(block) Get the panel that owns a block, or null.
engine.block.setEnum(page, 'page/pageSliceAxis', axis) Put the panels in a row or a column.
engine.block.resizeContentAware(pages, width, height) Give the sheets a new size, and keep each block on its panel.
engine.block.export(page, { mimeType, exportPdfWithCropMarks, exportPdfWithFoldMarks, printMarkOffset }) Export a sheet as a PDF with crop and fold marks.

Next Steps#