Skip to content

Issue 423 Knowledge PDF Lost Formatting

Ed Mozley edited this page Nov 22, 2026 · 1 revision

πŸ“„ #423 Knowledge PDF export lost the formatting and the pictures

GitHub #423, reported by @proseriox Β· Knowledge Β· fixed in 5.9.1 (#3117)

What you saw

Export as PDF on a Knowledge article kept the words but lost most of what the editor shows:

  • a line break inside a paragraph (<br>) disappeared, so lines ran together - "This paragraph contains a line break.This text should appear on a new line";
  • bold, italic, underline, strikethrough, superscript, subscript and inline code all printed as plain text;
  • nested list items lost their bullets and numbers;
  • headings 5 and 6 printed as body text, and a block quote looked like any other paragraph;
  • links were not clickable;
  • pictures were missing entirely - both a picture linked from another site and one embedded in the article.

@proseriox sent a test article that uses every one of these, with its HTML, which made the whole thing reproducible in one go.

What was wrong

The export walked the article's top-level blocks and, for each one, printed its textContent - the plain text with every tag removed:

// assets/js/knowledge.js, buildArticlePdf() (before)
if (tag === 'p') {
    const text = node.textContent.trim();
    ...
    printLines(doc.splitTextToSize(text, contentW), margin, 5);

textContent is the right tool for searching text and the wrong one for drawing it: <strong>, <em>, <br> and <a> are exactly the things it throws away. Lists did the same per item, so a nested list's items came out as extra lines of the parent item's text, with no bullet. There was no case for <img> at all, and only h1-h4 had heading styles.

Why it was built like that

The PDF writer has a jsPDF-shaped interface (text(), setFont(), splitTextToSize()), and splitTextToSize() wraps one string in one font. Mixed formatting in one line - a bold word in the middle of a sentence - needs a line built from pieces in different fonts, which that call cannot do. Plain text was the shape that fitted the tool.

The fix

The export now has its own small layout engine, in three parts:

  1. collectRuns() walks a block's inline content and flattens it into runs: a piece of text plus its style (bold, italic, underline, strikethrough, superscript, subscript, code, link), a line break, or a picture. Styles come from the tags and from the inline styles the editor writes (<span style="text-decoration: underline">).
  2. flow() lays the runs out word by word. Each word is measured in its own font (setFont() then getTextWidth()), words are added to the line until the next would not fit, and then the line is drawn piece by piece - each in its font, with underline and strikethrough drawn as lines, superscript and subscript smaller and shifted, and links made clickable.
  3. renderBlocks() handles the blocks: headings h1-h6, paragraphs, lists (nested lists indent and change marker - β€’, β—¦, β–ͺ - and <ol start> is honoured), block quotes (a grey bar beside every line, so a quote that crosses a page keeps it), code blocks, tables and pictures. Anything else - div, section, figure - is treated as a box around more blocks.
// flow(): a word is measured in its own font, so a bold word takes a bold word's room
applyStyle(r.st);
let ww = doc.getTextWidth(word);
if (lineW + ww > w && line.length) emit(false);
line.push({ text: word, st: r.st, w: ww }); lineW += ww;

Pictures

Drawing is synchronous and loading a picture is not, so every picture is loaded first, before anything is drawn. Each goes through a canvas, which does two jobs: any format the browser can show becomes a PNG the PDF can hold, and a picture from a site that does not allow it fails there, early, rather than in the middle of drawing.

A picture is placed at the size the article gives it (its width attribute, in CSS pixels) or its own, never wider than the column or taller than a page, and moves to the next page if it does not fit.

A picture that cannot be read - a site that refuses, a broken address - prints a grey line, [Picture not available: Network diagram], using its alt text or file name. It used to vanish without a word; now the reader knows something was there.

Links

The PDF writer gained link(x, y, w, h, { url }), the same shape as jsPDF's. It links only http(s): and mailto: addresses, so a javascript: link in an article is never made clickable in a PDF. Relative links are turned into full addresses first.

πŸ“ Files

File Change
🟑 assets/js/knowledge.js buildArticlePdf(): the rich-text layout - collectRuns(), flow(), renderBlocks(), renderList(), drawPicture()
🟑 assets/js/pdf-writer.js link()
🌐 lang/*/knowledge.php share.pdf_picture_missing - 23 languages
🟣 knowledge/index.php knowledge.js?v=74
βšͺ 12 pages pdf-writer.js?v=3

How it was verified

The reporter's own HTML was put into a test install through the Knowledge REST API (POST /knowledge/articles) - which also checked the way in: the article was stored byte for byte as sent, 171,608 bytes, with its embedded picture, and read back identically. Then it was exported from the Knowledge page in a real browser, and the PDF read page by page:

Element Before After
Line breaks in a paragraph run together new line
Bold, italic, underline, strikethrough plain as styled
Superscript, subscript, inline code plain smaller and raised / lowered; monospace
Nested lists, three deep no markers β€’, β—¦, β–ͺ and 1. 2. at each level
Headings 5 and 6 body text bold headings
Block quote a plain paragraph indented, grey, with a bar
Link plain text blue, underlined, clickable
Linked picture missing drawn
Embedded picture (166 KB) missing drawn, on the next page when it did not fit

A second article checked the edges: a picture at an address that does not exist (the grey line), a list three levels deep mixing bullets and numbers, a list inside a quote (the bar continues), Hβ‚‚O and xΒ² inline, a relative link (made absolute and clickable) and a javascript: link (not linked - the PDF contains no javascript at all).

What is not covered

  • Tables are still drawn from each cell's plain text: a bold word in a cell prints plain. The table engine (autoTable) takes one style per cell.
  • Colours and font sizes set in the editor are not carried over; the PDF uses its own text colour and sizes.
  • A picture from another site appears only if that site allows it to be read by a web page (most picture hosts do). One that does not prints the grey line instead.

What this means for you

Upgrade to 5.9.1 and hard-refresh (Ctrl-F5). Export an article again, and Email (Link + PDF) benefits too - it attaches the same PDF.

Related

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally