A tool to create office documents (currently only Word Documents) with JS
This library is deprecated. A newer, more modern one is in development here!
$ npm install --save office-clippy
var officeClippy = require('office-clippy');
// Used to create docx files
var docx = officeClippy.docx;
// Used to export the file into a .docx file
var exporter = officeClippy.exporter;
var doc = docx.create();
var paragraph = docx.createParagraph("Some test");
doc.addParagraph(paragraph);
You can add properties to the Word document by specifying options, for example:
var doc = docx.create({
creator: 'Joe Bloggs',
description: 'Some descriptive information here',
title: 'My first word document'
});
creator
description
title
subject
keywords
lastModifiedBy
revision
You can mix and match whatever properties you want, or provide no properties.
Every text block in OpenXML is organised in paragraphs. You can add more text to the paragraph by doing this:
var paragraph = docx.createParagraph();
var text = docx.createText("Lorem Ipsum Foo Bar");
var paragraph = docx.createParagraph();
paragraph.addText(text);
var paragraph = docx.createParagraph("Short hand notation for adding text.");
After you create the paragraph, you must add the paragraph into the document
:
doc.addParagraph(paragraph);
Styles is a very important part of the look of a word document. At the moment, only headings and title is supported, but son the rest will be supported along with custom styles!
paragraph.heading1();
paragraph.heading2();
paragraph.heading3();
paragraph.heading4();
paragraph.heading5();
paragraph.title();
To change the text alignment of a paragraph, for center, left, right or justified:
paragraph.center();
paragraph.left();
paragraph.right();
paragraph.justified();
paragraph.heading1().center();
The above will create a heading 1
which is centered
.
To add a break in the page, simply add .thematicBreak()
on a paragraph:
var paragraph = docx.createParagraph("Amazing Heading").heading1().thematicBreak();
The above example will create a heading with a page break directly under it.
To move to a new page (insert a page break), simply add .pageBreak()
on a paragraph:
var paragraph = docx.createParagraph("Amazing Heading").heading1().pageBreak();
The above example will create a heading and start a new page immediately afterwards.
Paragraphs need text objects. To create text:
var text = docx.createText("My awesome text here for my university dissertation");
paragraph.addText(text);
Text objects have methods inside which changes the way the text is displayed.
More info here
text.bold();
text.italic();
text.underline();
text.strike();
text.doubleStrike();
text.superScript();
text.subScript();
text.allCaps();
text.smallCaps();
Sometimes you would want to put text underneath another line of text but inside the same paragraph.
text.break();
What if you want to create a paragraph which is bold and italic?
paragraph.bold().italic();
To make a bullet point, simply make a paragraph into a bullet point:
var text = docx.createText("Bullet points");
var paragraph = docx.createParagraph(text).bullet();
var text2 = docx.createText("Are awesome");
var paragraph2 = docx.createParagraph(text2).bullet();
doc.addParagraph(paragraph);
doc.addParagraph(paragraph2);
This will produce:
- Bullet points
- Are awesome
If you do not know why tab stops are useful, then I recommend you do a bit of research. It enables side by side text which is nicely laid out without the need for tables, or constantly pressing space bar.
Note: At the moment, the unit of measurement for a tab stop is counter intuitive for a human. It is using OpenXMLs own measuring system. For example, 2268 roughly translates to 3cm. Therefore in the future, I may consider changing it to percentages or even cm.
How it works is that, you create a tabStop
, then add it to the paragraph. Then just add a tab()
method call to a text object. Adding multiple tabStops
will mean you would have to chain tab()
until the desired tabStop
is selected. Example is shown below.
var tabStop = docx.createLeftTabStop(2268);
paragraph.addTabStop();
2268 is the distance from the left side.
var tabStop = docx.createCenterTabStop(2268);
paragraph.addTabStop();
2268 is the distance from the left side.
var tabStop = docx.createRightTabStop(2268);
paragraph.addTabStop();
2268 is the distance from the left side.
var tabStop = docx.createMaxRightTabStop();
paragraph.addTabStop();
This will create a tab stop on the very edge of the right hand side. Handy for right aligning and left aligning text on the same line.
var tabStop = docx.createMaxRightTabStop();
var paragraph = docx.createParagraph().addTabStop(tabStop);
var leftText = docx.createText("Hey everyone").bold();
var rightText = docx.createText("11th November 2015").tab();
paragraph.addText(leftText);
paragraph.addText(rightText);
The example above will create a left aligned text, and a right aligned text on the same line. The laymans approach to this problem would be to either use text boxes or tables. YUK!
var tabStop = docx.createMaxRightTabStop();
var tabStop2 = docx.createLeftTabStop(1000);
var paragraph = docx.createParagraph();
paragraph.addTabStop(tabStop);
paragraph.addTabStop(tabStop2);
var text = docx.createText("Second tab stop here I come!").tab().tab();
paragraph.addText(text);
The above shows the use of two tab stops, and how to select/use it.
I used the express exporter in my website. It's very useful, and is the preferred way if you want to make a downloadable file for a visitor. it is much better than generating a physical file on the server, and then passing a download link to that file.
Simply use the exporter, and pass in the necessary parameters:
var officeClippy = require('office-clippy');
var docx = officeClippy.docx;
var exporter = officeClippy.exporter;
var doc = docx.create();
exporter.express(res, doc, 'My first word document');
where res
is the response object obtained through the Express router. It is that simple. The file will begin downloading in the browser.
var fs = require('fs');
var doc = docx.create();
var output = fs.createWriteStream(__dirname + '\\My first word document.docx');
exporter.local(output, doc);
I used this library in my personal portfolio/CV website. Click generate CV for a demonstration. http://www.dolan.bio
var doc = docx.create();
var output = fs.createWriteStream(__dirname + '\\example.docx');
var paragraph = docx.createParagraph("Hello World");
var institutionText = docx.createText("University College London").bold(),
var dateText = docx.createText("5th Dec 2015").tab().bold();
paragraph.addText(institutionText);
paragraph.addText(dateText);
doc.addParagraph(paragraph);
exporter.local(output, doc);
Or:
var doc = docx.create();
var output = fs.createWriteStream(__dirname + '\\example.docx');
var paragraph = docx.createParagraph("Hello World");
var institutionText = docx.createText("University College London").bold(),
var dateText = docx.createText("5th Dec 2015").tab().bold();
paragraph.addText(institutionText);
paragraph.addText(dateText);
doc.addParagraph(paragraph);
exporter.local(output, doc);
Would produce:
Hello World
Lorem Ipsum foo bar
MIT © Dolan Miu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.