Editor.js to PDF and Word

Product requirements from Editor.js blocks, as PDF or Word

Generate PDF and Word documents from your Editor.js app

Product wikis, knowledge bases and CMS that embed the Editor.js block editor store each page as JSON: the output of editor.save(), a blocks array with one object per heading, paragraph, list, table or image. When a user clicks Export, your app sends this JSON to the Carbone API with the page properties it stores (title, owner, status), and one DOCX template returns the page as a PDF or a Word file. In this example, Ferrow, a fictional spend management company, exports PRD-142 from its product wiki: a 3-page product requirements document, followed by the usability report attached to the page.

In this example, you will:

  • Loop over the Editor.js blocks and keep one paragraph or table per block type
  • Render the bold, italic, inline code and links of Editor.js with :html
  • Number ordered lists, tick checklists and repeat the columns of tables
  • Append the PDF files attached to the page

Editor.js blocks and their template parts

The data sample follows the output format of the official Editor.js tools: Header, Paragraph, List (unordered, ordered and checklist styles), Quote, Warning, Code, Table, Image, Delimiter and Attaches.

Block type Data used How the template renders it
header data.text, data.level Levels 1 and 2 in the Heading 2 style, levels 3 to 6 in Heading 3
paragraph data.text Body text, with the bold, italic, inline code, links and line breaks of the editor
list data.style, data.items Bullets, numbers that restart in each list, or checkboxes
quote data.text, data.caption A rule in the brand color, and a caption dropped when it is empty
warning data.title, data.message A shaded callout
code data.code A monospace panel that keeps the line breaks
table data.content, data.withHeadings A heading row when withHeadings is true, one column per cell of the editor
image data.file.url, data.caption A framed picture at the full text width, with its caption
delimiter none A hairline rule
attaches data.file, data.title The file type, name and size, and PDF files appended to the generated PDF

Editor.js to PDF example

Template

The DOCX template is a Word document with Carbone tags. The title block prints the page properties, then the loop holds one paragraph or table per block type. The key tags:

Where Tag
Title {d.page.title}
Last edited, from the save time of Editor.js {d.time:formatD('D MMM YYYY', 'x')}
Paragraph block {d.blocks[i].data.text:html(nospace)}{d.blocks[i].type:ifNE('paragraph'):drop(p)}
Heading 2 {d.blocks[i].data.text:html(inline)}{d.blocks[i].type:ifNE('header'):or(.data.level):ifGT(2):drop(p)}
Number of a list item {d.blocks[i].data.items[i].content:print(..i):add(1)}.
Code block {d.blocks[i].data.code:convCRLF}{d.blocks[i].type:ifNE('code'):drop(p)}
Alt text of the picture {d.blocks[i].type:ifEQ('image'):show(.data.file.url):elseShow('')}
Heading row of a table {d.blocks[i].data.content[0][i,i>0]:html(inline)}
End of the loop {d.blocks[i+1].id}

JSON data

An excerpt: the page properties set by the wiki, then the output of editor.save(). The data sample has the 29 blocks of the page.

{
  "page": { "id": "PRD-142", "title": "Out-of-office approval delegation", "status": "In review", "owner": "Maya Lindqvist" },
  "time": 1794501727000,
  "blocks": [
    { "id": "Hq3vK9_xTa", "type": "header", "data": { "text": "Problem", "level": 2 } },
    { "id": "p2LmW8sQe1", "type": "paragraph", "data": { "text": "Approval steps stall when an approver is away. Across our 40 largest customers, <b>18% of approval steps</b> wait more than three business days..." } },
    { "id": "Py6eD3gM9a", "type": "list", "data": { "style": "checklist", "meta": {}, "items": [
      { "content": "Security review of the delegation API", "meta": { "checked": true }, "items": [] },
      { "content": "Help center article and in-app banner copy", "meta": { "checked": false }, "items": [] }
    ] } },
    { "id": "L0aG5iH2dq", "type": "table", "data": { "withHeadings": true, "content": [
      ["Metric", "Baseline", "Target", "Source"],
      ["Median invoice approval time", "2.9 days", "1.5 days", "Approval events"]
    ] } },
    { "id": "Mk5sB7pV1c", "type": "attaches", "data": {
      "file": { "url": "data:application/pdf;base64,JVBERi0xLjcK...", "size": 54756, "name": "usability-round-2-findings.pdf", "extension": "pdf" },
      "title": "Usability round 2: findings"
    } }
  ],
  "version": "2.31.7"
}

How to render Editor.js blocks with one Word template

1. Loop over the blocks

Editor.js saves the page as a blocks array: each block has an id, a type and a data object. The first paragraph after the title block opens the loop with {d.blocks[i].data.text:html(nospace)}, and the last paragraph closes it with {d.blocks[i+1].id}. Carbone repeats every paragraph and table in between for each block, in the order of the editor.

2. Keep one paragraph per block type

Style each paragraph as its block should look, then end it with a smart condition. The tag prints nothing and drops the paragraph when the block has another type:

{d.blocks[i].data.text:html(nospace)}{d.blocks[i].type:ifNE('paragraph'):drop(p)}
  • The two heading paragraphs combine the type and the level with :or: {d.blocks[i].type:ifNE('header'):or(.data.level):ifGT(2):drop(p)} keeps levels 1 and 2 in Heading 2, and the same tag with ifLTE(2) keeps levels 3 to 6 in Heading 3.
  • The quote caption and the image caption also test the caption, so an empty caption leaves no empty line: {d.blocks[i].type:ifNE('quote'):or(.data.caption):ifEM():drop(p)}.
  • The delimiter is a paragraph with a bottom border and a single tag: {d.blocks[i].type:ifNE('delimiter'):drop(p)}.
  • The two tables, with and without a heading row, hold :drop(table) in their first cell: {d.blocks[i].type:ifNE('table'):or(.data.withHeadings):ifNE(true):drop(table)}.

A block type that the template does not handle prints nothing.

3. Render the inline formatting with :html

Editor.js stores bold, italic, inline code and links as HTML in the text, and saves & as &amp;. The :html formatter turns them into Word formatting:

  • A paragraph block uses {d.blocks[i].data.text:html(nospace)}. :html creates its own paragraph with the Normal style, which is the body text style of this template, and keeps the line breaks. The nospace option removes the empty paragraph that :html adds after a paragraph.
  • Headings, list items, quotes, callouts, captions and table cells use :html(inline), which keeps the text inside the styled paragraph: {d.blocks[i].data.text:html(inline)}.
  • {o.preReleaseFeatureIn=5011000}, at the end of the breadcrumb, renders the inline code of Editor.js in a monospace font.

4. Number the lists and tick the checklists

A list block holds a nested loop on data.items. The template has one paragraph per list style, then {d.blocks[i].data.items[i+1].content} to close the nested loop.

  • Bullets: {d.blocks[i].data.items[i].content:html(inline)}{d.blocks[i].data.style:ifNE('unordered'):drop(p)}, in the List Bullet style.
  • Numbers: {d.blocks[i].data.items[i].content:print(..i):add(1)}. prints the index of the item plus one, so each list starts again at 1. The number has a character style, List marker, so the text after it keeps the color of the paragraph.
  • Checklists: one paragraph starts with a checked box picture and holds {d.blocks[i].data.items[i].meta.checked:ifNE(true):drop(p)}, the next one starts with an empty box and holds {d.blocks[i].data.items[i].meta.checked:ifEQ(true):drop(p)}. Both also hold {d.blocks[i].data.style:ifNE('checklist'):drop(p)}. Done items are gray.

Tip: version 1 of the List tool saves each item as a string. In that case, display {d.blocks[i].data.items[i]}.

5. Repeat the columns of tables

data.content holds one array per row. The table repeats rows and columns with a bi-directional loop. The first column is written apart, so it stays wider for the row labels, and the filter [i,i>0] repeats the other columns:

| {d.blocks[i].data.content[0][0]:html(inline)}       | {d.blocks[i].data.content[0][i,i>0]:html(inline)}       | {d.blocks[i].data.content[0][i+1,i>0]}     |
| {d.blocks[i].data.content[i,i>0][0]:html(inline)}   | {d.blocks[i].data.content[i,i>0][i,i>0]:html(inline)}   | {d.blocks[i].data.content[i,i>0][i+1,i>0]} |
| {d.blocks[i].data.content[i+1,i>0]}                 |                                                         |                                            |
  • The first row prints the headings with content[0], and [i,i>0] skips them in the body rows.
  • The table without a heading row loops on every row with content[i].
  • The width of both tables is set to 100%, so the generated columns fill the page width.

6. Insert images and append attached files

  • The image paragraph holds a temporary picture at the full text width, with a hairline border. Its alt text is {d.blocks[i].type:ifEQ('image'):show(.data.file.url):elseShow('')}: an attachment also has a data.file.url, and the condition gives the picture the URL of image blocks only. Carbone replaces the picture and keeps its width. Here the screenshot is a Base64 data URI; your app sends the URL of its uploads.
  • The attachment paragraph prints the type, title, name and size of the file, for example {d.blocks[i].data.file.extension:upperCase} and {d.blocks[i].data.file.size:div(1000):formatN(0)} KB. Then :appendFile adds the PDF files at the end of the generated PDF:
{d.blocks[i].type:ifEQ('attaches'):and(.data.file.extension):ifEQ('pdf'):show(.data.file.url):elseShow(''):appendFile}

The condition passes the URL of PDF attachments only, because an image block also has a data.file.url. Here the usability report becomes page 4, and the XLSX file is listed without being appended.

Render with "convertTo": "pdf" to get the PDF, or with "convertTo": "docx" to get a Word file. :appendFile applies to PDF output only.

Check out these similar templates to learn more:

Trusted by 800+ paid customers in 40+ countries

Finance
Healthcare
Insurance
Legal
Public
Retail
Energy
Software
Telecom
Dior
Thales
Younited
Danfoss
CMA CGM
Illumina
KparK
Scalingo
French Ministry of the Armed Forces
BMW
Telus
Bestseller
Flagship Biosciences
Insly
Ninox
Sodexo