Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

DOCX Demo 5

node v8.17.0
version: 4.0.0
endpointsharetweet
const docx = require('docx@4.0.0'); const fs = require('fs'); const request = require('request@2.88.0'); const express = require("@runkit/runkit/express-endpoint/1.0.0"); const app = express(exports); const { Document, Paragraph, Packer } = docx; // https://stackoverflow.com/questions/12740659/downloading-images-with-node-js const download = (uri, filename, callback) => { request.head(uri, (err, res, body) => { request(uri).pipe(fs.createWriteStream(filename)).on('close', callback); }); }; const URL = 'https://raw.githubusercontent.com/dolanmiu/docx/ccd655ef8be3828f2c4b1feb3517a905f98409d9/demo/images/cat.jpg'; app.get("/", (req, res) => { download(URL, 'cat.jpg', async () => { const doc = new Document(); const paragraph = new Paragraph("Hello World"); doc.addParagraph(paragraph); const image = doc.createImage(fs.readFileSync('./cat.jpg')); const packer = new Packer(); const b64string = await packer.toBase64String(doc); res.setHeader('Content-Disposition', 'attachment; filename=My Document.docx'); res.send(Buffer.from(b64string, 'base64')); }); })
Loading…

no comments

    sign in to comment