Skip to content

Commit b225d33

Browse files
fix: node example document corrupted on dl
1 parent 80d0aeb commit b225d33

5 files changed

Lines changed: 43 additions & 13 deletions

File tree

examples/nodejs-example/blank.docx

12.9 KB
Binary file not shown.

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');
13 KB
Binary file not shown.

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)