Skip to content

Commit 43bb4b0

Browse files
fix: node example document corrupted on dl (#729)
1 parent 0a5ba36 commit 43bb4b0

4 files changed

Lines changed: 44 additions & 14 deletions

File tree

examples/nodejs-example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ HTML only: http://localhost:3000?html=<p>I am a paragraph</p><p></p><p><strong>I
2323
```
2424

2525
## Additional docs
26-
Please see [SuperDoc docs](https://docs.superdoc.dev/components/#supereditor) for additinoal editor commands and hooks.
26+
Please see [SuperDoc docs](https://docs.superdoc.dev/guide/components#superdoc) for additinoal editor commands and hooks.
2727

2828
You can get a list of all available editor commands from editor.commands as well. For instnace, commands such as the examples below all will work while using the SuperDoc editor in the backend:
2929
```

examples/nodejs-example/document-b64.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import b64 from './document-b64.js';
2+
3+
const base64ToDocx = (base64String, filename) => {
4+
try {
5+
const binaryString = atob(base64String);
6+
const bytes = new Uint8Array(binaryString.length);
7+
for (let i = 0; i < binaryString.length; i++) {
8+
bytes[i] = binaryString.charCodeAt(i);
9+
}
10+
11+
const blob = new Blob([bytes], {
12+
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
13+
});
14+
15+
const file = new File([blob], filename, {
16+
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
17+
});
18+
19+
return file;
20+
} catch (error) {
21+
console.error('Error converting base64 to DOCX:', error);
22+
}
23+
};
24+
25+
export default base64ToDocx(b64, 'sample-document.docx');

examples/nodejs-example/server.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import fs from 'fs/promises';
33
import express from 'express';
44
import { JSDOM } from 'jsdom';
5+
import documentBlob from './document.js';
56

67
// In Node, we use the Editor class directly from superdoc/super-editor
78
import { Editor, getStarterExtensions } from '@harbour-enterprises/superdoc/super-editor';
@@ -17,23 +18,26 @@ const server = express();
1718
* If no param is passed, the document will be returned as as-is (blank template with header and footer).
1819
*/
1920
server.get('/', async (req, res, next) => {
20-
// Load our example document - a blank template with a header and footer
21-
let documentData = await fs.readFile('./sample-document.docx');
22-
23-
// Get the text and html from the query parameters
2421
const { text, html } = req.query;
2522

26-
// If we have text or html, we will to load the editor and insert the content
27-
if (text || html) {
28-
const editor = await getEditor(documentData);
23+
// Load the specified document:
24+
// if using stackblitz:
25+
let documentData = documentBlob;
26+
const arrayBuffer = await documentData.arrayBuffer();
27+
documentData = Buffer.from(arrayBuffer);
2928

30-
if (text) editor.commands.insertContent(text);
31-
if (html) editor.commands.insertContent(html);
29+
// otherwise, you can read from disk:
30+
// let documentData = await fs.readFile(`./sample-document.docx`);
31+
32+
const editor = await getEditor(documentData);
33+
34+
// If we have text or html, we will to load the editor and insert the content
35+
if (text) editor.commands.insertContent(text);
36+
if (html) editor.commands.insertContent(html);
3237

33-
// Export the docx and create a buffer to return to the user
34-
const zipBuffer = await editor.exportDocx();
35-
documentData = Buffer.from(zipBuffer);
36-
}
38+
// Export the docx and create a buffer to return to the user
39+
const zipBuffer = await editor.exportDocx();
40+
documentData = Buffer.from(zipBuffer);
3741

3842
// Download the file
3943
res

0 commit comments

Comments
 (0)