Files
docx-js/docs/usage/packers.md

118 lines
2.9 KiB
Markdown
Raw Normal View History

2018-08-04 03:28:27 +01:00
# Packers
2018-08-04 03:40:41 +01:00
> Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`.
2018-08-04 03:28:27 +01:00
2018-08-21 21:46:12 +01:00
## Version 4
Packers in `version 4` and above are now one single `Packer`. It works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of version 4, this library will not have options to export to PDF.
### Export as Buffer
This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead.
```js
const packer = new docx.Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
```
### Export as a `base64` string
```js
const packer = new docx.Packer();
packer.toBase64String(doc).then((string) => {
console.log(string);
});
```
### Export as Blob
This is useful if you want to send it as an downloadable in a browser environment.
```js
const packer = new docx.Packer();
packer.toBlob(doc).then((blob) => {
// saveAs from FileSaver will download the file
saveAs(blob, "example.docx");
});
```
## Version 3 and below
### File System Packer
2018-08-04 03:28:27 +01:00
```js
const docx = require("docx");
const doc = new docx.Document();
const exporter = new docx.LocalPacker(doc);
exporter.pack("My Document");
// Word Document is in file system
```
2018-08-21 21:46:12 +01:00
### Buffer Packer
2018-08-04 03:28:27 +01:00
```js
const docx = require("docx");
const doc = new docx.Document();
const exporter = new docx.BufferPacker(doc);
const buffer = exporter.pack();
```
2018-08-21 21:46:12 +01:00
### Stream Packer
2018-08-04 03:28:27 +01:00
Creates a `node` `Readable` stream
```js
const docx = require("docx");
const doc = new docx.Document();
const exporter = new docx.StreamPacker(doc);
2018-08-07 02:23:58 +01:00
const stream = exporter.pack();
2018-08-04 03:28:27 +01:00
```
2018-08-21 21:46:12 +01:00
### Express Packer
2018-08-04 03:28:27 +01:00
2018-08-07 02:23:58 +01:00
The old express packer is now deprecated and may disappear soon, so you should upgrade.
The reason for this is because it means this project needs to know about and use `express`, which for a Word document generator, does not sound right. Seperation of concerns.
It will still be usable (for now), but it is ill advised.
2018-08-04 03:40:41 +01:00
I used the express exporter in my [website](http://www.dolan.bio).
2018-08-07 02:23:58 +01:00
The recommended way is to use the `StreamPacker` and handle the `express` magic outside of the library:
2018-08-04 03:28:27 +01:00
```js
const docx = require("docx");
const doc = new docx.Document();
2018-08-07 02:23:58 +01:00
const exporter = new docx.StreamPacker(doc);
const stream = exporter.pack();
// Express' response object
2018-08-21 21:46:12 +01:00
res.attachment("yourfile.xlsx");
2018-08-07 02:23:58 +01:00
stream.pipe(res);
2018-08-04 03:28:27 +01:00
```
where `res` is the response object obtained through the Express router. It is that simple. The file will begin downloading in the browser.
2018-08-21 21:46:12 +01:00
### PDF Exporting
2018-08-04 03:28:27 +01:00
You can export your word document as a PDF file like so:
```js
const exporter = new docx.LocalPacker(doc);
exporter.packPdf("My Document");
// Express
const exporter = new docx.ExpressPacker(doc, res);
exporter.packPdf("My Document");
```