Files
wekan/models/server/ExporterCardPDF.js
T
Lauri Ojansivu eb14168893 Fix #6586: the PDF export writes umlauts, not question marks.
"When using Board settings » Export board » export / PDF we get a pdf file but the
umlauts (german, ä, ü, ö,...) are corrupt", "all the text in this PDF file is
markdown formatted - this doesn't make sense in a pdf file, does it?", and "I can't
see in which swimlane a card is in that export, no tags".

The umlauts were removed on purpose, one line before anything was written:

  .replace(/[^\x20-\x7E\n]/g, '?')

so "Grüße" left the server as "Gr??e". That line was a workaround for the real
problem rather than a choice: text in a PDF is BYTES PLUS AN ENCODING, and the
exporter declared neither. A `(...)` string in a content stream is read through the
font's encoding; no /Encoding was given, so a viewer used the font's built-in one,
where the bytes above 127 are not the Latin-1 letters - and the file was assembled
as a UTF-8 string, so one "ü" would have arrived as two bytes and drawn as two
wrong glyphs. With no encoding to write into, erasing the letters was the only
thing left that did not corrupt the file.

So the font declares /WinAnsiEncoding - Windows-1252, which every viewer has for
the base-14 fonts - the text is encoded to those single bytes, and the file is
assembled as binary. The xref offsets are measured in bytes for the same reason:
measured as UTF-8, every offset after the first accented character is wrong, and
some viewers refuse the file outright.

What Windows-1252 does not have is transliterated rather than erased: a Latin
letter loses its mark (ā -> a, ź -> z, Ł -> L) and keeps its word. Only a script
the base-14 fonts cannot draw at all - Cyrillic, Greek, Hebrew, Arabic, CJK, emoji
- still falls back to '?', because the fix for those is an embedded Unicode font
and that is a font binary this repository does not carry. It is in TODO Later.

Markdown is flattened instead of printed. `**bold**` in a PDF is four asterisks,
and the board export was itself emitting markdown - "## List (3)", "- Card" - into
a file that has no markdown in it. The structure is drawn now: headings and titles
in Courier-Bold, cards as bullets, and a card's own markdown reduced to its words
(links keep their text and their URL). Emphasis underscores only count at a word
boundary, so `snake_case` and `file_name_here` are left alone - markdown treats
them the same way.

And the board export says what it was missing: swimlanes (walked swimlane by
swimlane when a board has more than the one every board is created with, and not
mentioned at all when it does not), labels, members, assignees and the dates.

The PDF itself - encoding, flattening, wrapping, pagination - moves to
models/lib/pdfDocument.js, which is pure, so tests/pdfExport.test.cjs reads the
BYTES that come out: that ü is 0xFC and ß is 0xDF, that the euro sign is 0x80,
which Latin-1 does not have at all, that every xref row points at the object it
claims, and that the bold font is selected only where a heading asks for it. Its
negative half is the part worth keeping: junk never throws, an empty board still
produces a file that opens, `2 * 3 * 4` and `a_b_c` are not emphasis, and a
Japanese title degrades to '?' without ever emitting a byte above 0xFF into the
stream.

Verified by running the suite and the whole node suite (378 suites, 0 failed). Not
verified against a PDF viewer: this sandbox has none, so the bytes are checked
against the PDF specification's own rules instead.

Thanks to Heart1010 and xet7 !

Fixes #6586.
2026-08-12 14:53:06 +03:00

313 lines
11 KiB
JavaScript

import { ReactiveCache } from '/imports/reactiveCache';
import {
wrapTextBlock,
line,
buildPdfBuffer,
} from '/models/lib/pdfDocument';
// #6586: the PDF itself - encoding, markdown flattening, wrapping, pagination -
// lives in models/lib/pdfDocument.js, where it can be tested against the bytes a
// reader's viewer sees. This file decides WHAT a card and a board export say.
function sanitizeFilename(value) {
return String(value || 'export-card')
.replace(/[^a-z0-9._-]+/gi, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '')
.slice(0, 80) || 'export-card';
}
function formatDateValue(value) {
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
return '-';
}
return value.toISOString().replace('T', ' ').slice(0, 16);
}
function formatUser(user) {
if (!user) {
return 'Unknown';
}
return user.profile?.fullname || user.username || user.profile?.initials || user._id;
}
class ExporterCardPDF {
constructor(boardId, listId, cardId) {
this._boardId = boardId;
this._listId = listId;
this._cardId = cardId;
}
async _getCardData() {
const board = await ReactiveCache.getBoard(this._boardId);
const list = await ReactiveCache.getList({
_id: this._listId,
boardId: this._boardId,
});
const card = await ReactiveCache.getCard({
_id: this._cardId,
boardId: this._boardId,
listId: this._listId,
});
if (!board || !list || !card) {
return null;
}
const swimlane = card.swimlaneId
? await ReactiveCache.getSwimlane({ _id: card.swimlaneId })
: null;
const checklists = await ReactiveCache.getChecklists(
{ cardId: this._cardId },
{ sort: { sort: 1 } },
);
const comments = await ReactiveCache.getCardComments(
{ cardId: this._cardId },
{ sort: { createdAt: 1 } },
);
const checklistItemsByChecklistId = {};
for (const checklist of checklists) {
checklistItemsByChecklistId[checklist._id] = await ReactiveCache.getChecklistItems(
{ checklistId: checklist._id },
{ sort: { sort: 1 } },
);
}
const userIds = new Set([
card.userId,
...(card.members || []),
...(card.assignees || []),
...comments.map(comment => comment.userId),
]);
const usersById = {};
await Promise.all(
[...userIds]
.filter(Boolean)
.map(async userId => {
usersById[userId] = await ReactiveCache.getUser({ _id: userId });
}),
);
return {
board,
list,
card,
swimlane,
checklists,
checklistItemsByChecklistId,
comments,
usersById,
};
}
async build(res) {
const data = await this._getCardData();
if (!data) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Card not found');
return;
}
const { board, list, card, swimlane, checklists, checklistItemsByChecklistId, comments, usersById } = data;
const labelsById = Object.fromEntries(
(board.labels || [])
.filter(label => label && label._id)
.map(label => [label._id, label.name || label.color || label._id]),
);
// Every value goes through the document's own encoder as it is written
// (models/lib/pdfDocument.js), so a title, a name or a comment with umlauts in
// it arrives as those letters rather than as question marks (#6586).
const lines = [
line('Wekan Card Export', true),
'',
`Title: ${card.title || '-'}`,
`Board: ${board.title || '-'}`,
`List: ${list.title || '-'}`,
`Swimlane: ${swimlane?.title || '-'}`,
`Created by: ${formatUser(usersById[card.userId])}`,
`Members: ${(card.members || []).map(userId => formatUser(usersById[userId])).join(', ') || '-'}`,
`Assignees: ${(card.assignees || []).map(userId => formatUser(usersById[userId])).join(', ') || '-'}`,
`Labels: ${(card.labelIds || []).map(labelId => labelsById[labelId] || labelId).join(', ') || '-'}`,
`Created: ${formatDateValue(card.createdAt)}`,
`Last activity: ${formatDateValue(card.dateLastActivity)}`,
`Received: ${formatDateValue(card.receivedAt)}`,
`Start: ${formatDateValue(card.startAt)}`,
`Due: ${formatDateValue(card.dueAt)}`,
`End: ${formatDateValue(card.endAt)}`,
`Spent time: ${card.spentTime ?? '-'}`,
'',
line('Description:', true),
...wrapTextBlock(card.description || '-'),
];
lines.push('', line('Checklists:', true));
if (checklists.length === 0) {
lines.push('-');
} else {
for (const checklist of checklists) {
lines.push(...wrapTextBlock(`${checklist.title || 'Checklist'}`, '- '));
const items = checklistItemsByChecklistId[checklist._id] || [];
if (items.length === 0) {
lines.push(' (no items)');
continue;
}
for (const item of items) {
lines.push(...wrapTextBlock(`${item.isFinished ? '[x]' : '[ ]'} ${item.title || ''}`, ' '));
}
}
}
lines.push('', line('Comments:', true));
if (comments.length === 0) {
lines.push('-');
} else {
for (const comment of comments) {
lines.push(
...wrapTextBlock(
`${formatDateValue(comment.createdAt)} ${formatUser(usersById[comment.userId])}: ${comment.text || ''}`,
'- ',
),
);
}
}
const filename = `${sanitizeFilename(card.title)}.pdf`;
const pdf = buildPdfBuffer(lines);
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${filename}"`,
'Content-Length': pdf.length,
});
res.end(pdf);
}
async canExport(user) {
const board = await ReactiveCache.getBoard(this._boardId);
return board && board.isVisibleBy(user);
}
}
// #395: board-level PDF export, written with the same document builder as the card
// export (models/lib/pdfDocument.js).
//
// #6586: it used to print MARKDOWN - "## List (3)" and "- Card title" - into a PDF,
// where a "##" is just two hash marks: "all the text in this PDF file is markdown
// formatted - this doesn't make sense in a pdf file, does it?" It does not. The
// structure is drawn instead: headings in the bold font, cards as bullets, and the
// card's own markdown flattened rather than reproduced.
//
// The same report asked for what was missing: "I can't see in which swimlane a card
// is in that export, no tags". So the board is walked SWIMLANE by swimlane (when it
// has more than the one every board is created with), and each card carries its
// labels, members, assignees and dates.
class ExporterBoardPDF {
constructor(boardId) {
this._boardId = boardId;
}
async build(res) {
const board = await ReactiveCache.getBoard(this._boardId);
if (!board) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Board not found');
return;
}
const labelsById = Object.fromEntries(
(board.labels || [])
.filter(label => label && label._id)
.map(label => [label._id, label.name || label.color || label._id]),
);
const lists = await ReactiveCache.getLists(
{ boardId: this._boardId, archived: false },
{ sort: { sort: 1 } },
);
const swimlanes = await ReactiveCache.getSwimlanes(
{ boardId: this._boardId, archived: false },
{ sort: { sort: 1 } },
);
const cards = await ReactiveCache.getCards(
{ boardId: this._boardId, archived: false },
{ sort: { sort: 1 } },
);
// The names behind the ids on the cards. One pass, so a board with hundreds of
// cards does not do a lookup per member.
const userIds = new Set();
for (const card of cards) {
if (card.userId) userIds.add(card.userId);
(card.members || []).forEach(id => userIds.add(id));
(card.assignees || []).forEach(id => userIds.add(id));
}
const usersById = {};
await Promise.all([...userIds].filter(Boolean).map(async userId => {
usersById[userId] = await ReactiveCache.getUser({ _id: userId });
}));
const lines = [line(board.title || 'Board', true), ''];
// A board created normally has exactly one swimlane and nobody thinks in terms
// of it; only name the swimlanes when there is a choice to be made.
const named = swimlanes.filter(swimlane => swimlane && swimlane.type !== 'template-swimlane');
const groups = named.length > 1
? named.map(swimlane => ({ swimlane, title: swimlane.title || 'Swimlane' }))
: [{ swimlane: null, title: null }];
for (const group of groups) {
if (group.title) {
lines.push(line(`Swimlane: ${group.title}`, true));
}
for (const list of lists) {
const listCards = cards.filter(card =>
String(card.listId) === String(list._id)
&& (!group.swimlane || String(card.swimlaneId) === String(group.swimlane._id)));
if (group.title && listCards.length === 0) continue;
lines.push(line(`${list.title || 'List'} (${listCards.length})`, true));
for (const card of listCards) {
lines.push(...wrapTextBlock(card.title || '', '\u2022 '));
const labels = (card.labelIds || [])
.map(labelId => labelsById[labelId] || labelId)
.filter(Boolean);
if (labels.length) lines.push(...wrapTextBlock(`Labels: ${labels.join(', ')}`, ' '));
const members = (card.members || []).map(id => formatUser(usersById[id])).filter(Boolean);
if (members.length) lines.push(...wrapTextBlock(`Members: ${members.join(', ')}`, ' '));
const assignees = (card.assignees || []).map(id => formatUser(usersById[id])).filter(Boolean);
if (assignees.length) lines.push(...wrapTextBlock(`Assignees: ${assignees.join(', ')}`, ' '));
const dates = [];
if (card.receivedAt) dates.push(`received ${formatDateValue(card.receivedAt)}`);
if (card.startAt) dates.push(`start ${formatDateValue(card.startAt)}`);
if (card.dueAt) dates.push(`due ${formatDateValue(card.dueAt)}`);
if (card.endAt) dates.push(`end ${formatDateValue(card.endAt)}`);
if (dates.length) lines.push(...wrapTextBlock(dates.join(', '), ' '));
if (card.description) {
lines.push(...wrapTextBlock(card.description, ' '));
}
}
lines.push('');
}
}
const pdf = buildPdfBuffer(lines);
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${sanitizeFilename(board.title)}.pdf"`,
'Content-Length': pdf.length,
});
res.end(pdf);
}
async canExport(user) {
const board = await ReactiveCache.getBoard(this._boardId);
return board && board.isVisibleBy(user);
}
}
export { ExporterCardPDF, ExporterBoardPDF };