2020-01-17 16:07:44 +02:00
|
|
|
<p align="center"><img src ="https://github.com/bokuweb/docx-rs/blob/master/logo.png?raw=true" /></p>
|
|
|
|
|
|
|
|
<p align="center">
|
2020-04-21 14:49:37 +03:00
|
|
|
A .docx file writer with Rust/WebAssembly.
|
2020-01-17 16:07:44 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
---
|
2019-11-12 06:45:04 +02:00
|
|
|
|
|
|
|
[](https://github.com/bokuweb/docx-rs/actions)
|
2020-02-14 03:37:46 +02:00
|
|
|
[](https://crates.io/crates/docx-rs)
|
|
|
|
[](https://www.npmjs.com/package/docx-wasm)
|
2019-11-15 11:15:43 +02:00
|
|
|
|
2020-01-24 12:46:01 +02:00
|
|
|
## Installation
|
2020-01-24 12:44:43 +02:00
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
### Rust
|
|
|
|
|
2020-01-24 12:44:43 +02:00
|
|
|
```
|
|
|
|
[dependencies]
|
2021-09-29 07:33:51 +03:00
|
|
|
docx-rs = "0.2"
|
2020-01-24 12:44:43 +02:00
|
|
|
```
|
|
|
|
|
2020-02-13 12:04:45 +02:00
|
|
|
### Browser/Node.js
|
2020-01-30 16:14:25 +02:00
|
|
|
|
|
|
|
```
|
2021-10-20 09:03:31 +03:00
|
|
|
$ yarn add docx-wasm
|
2020-01-30 16:14:25 +02:00
|
|
|
```
|
|
|
|
|
2020-01-17 17:19:07 +02:00
|
|
|
## Example
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
### Rust
|
|
|
|
|
2020-01-24 11:50:16 +02:00
|
|
|
```rust
|
2020-01-24 12:44:43 +02:00
|
|
|
use docx_rs::*;
|
2020-01-17 17:19:07 +02:00
|
|
|
|
|
|
|
pub fn hello() -> Result<(), DocxError> {
|
2020-01-24 12:44:43 +02:00
|
|
|
let path = std::path::Path::new("./hello.docx");
|
|
|
|
let file = std::fs::File::create(&path).unwrap();
|
|
|
|
Docx::new()
|
|
|
|
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
|
|
|
|
.build()
|
|
|
|
.pack(file)?;
|
|
|
|
Ok(())
|
2020-01-17 17:19:07 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
### Browser
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
import { saveAs } from "file-saver";
|
|
|
|
|
|
|
|
// // Note that a dynamic `import` statement here is required due to webpack/webpack#6615,
|
2021-03-02 05:31:46 +02:00
|
|
|
import("docx-wasm").then((w) => {
|
2020-12-21 11:44:31 +02:00
|
|
|
const { buffer } = new w.Docx()
|
2020-01-30 16:14:25 +02:00
|
|
|
.addParagraph(
|
|
|
|
new w.Paragraph().addRun(new w.Run().addText("Hello world!!"))
|
|
|
|
)
|
|
|
|
.build();
|
2020-12-21 11:44:31 +02:00
|
|
|
saveAs(new Blob([buffer]), "hello.docx");
|
2020-01-30 16:14:25 +02:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-02-13 12:04:45 +02:00
|
|
|
### Node.js
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
const w = require("docx-wasm");
|
|
|
|
const { writeFileSync } = require("fs");
|
|
|
|
|
2020-12-21 11:44:31 +02:00
|
|
|
const { buffer } = new w.Docx()
|
2020-02-13 12:04:45 +02:00
|
|
|
.addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!!")))
|
|
|
|
.build();
|
|
|
|
|
2020-12-21 11:44:31 +02:00
|
|
|
writeFileSync("hello.docx", buffer);
|
2020-02-13 12:04:45 +02:00
|
|
|
```
|
|
|
|
|
2020-01-17 17:20:46 +02:00
|
|
|
### More examples
|
|
|
|
|
2020-01-24 11:50:16 +02:00
|
|
|
- [Minimum](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/hello.rs)
|
|
|
|
- [Indent](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/indent.rs)
|
|
|
|
- [Alignment](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/alignment.rs)
|
2020-06-08 07:41:13 +03:00
|
|
|
- [Font](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/font.rs)
|
2020-01-24 11:50:16 +02:00
|
|
|
- [Numbering](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/numbering.rs)
|
|
|
|
- [Table](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/table.rs)
|
|
|
|
- [Comment](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/comment.rs)
|
2020-06-02 21:27:04 +03:00
|
|
|
- [Image](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/image_inline.rs)
|
2020-01-24 11:50:16 +02:00
|
|
|
- [History](https://github.com/bokuweb/docx-rs/blob/master/docx-core/examples/history.rs)
|
2020-01-17 17:20:46 +02:00
|
|
|
|
2021-10-20 09:03:31 +03:00
|
|
|
## Development
|
|
|
|
|
|
|
|
### Requirements
|
|
|
|
|
|
|
|
- Node.js 16+
|
|
|
|
- yarn 1+
|
2021-11-26 12:07:10 +02:00
|
|
|
- wasm-pack0.10.1 (https://rustwasm.github.io/wasm-pack/)
|
2021-10-20 09:03:31 +03:00
|
|
|
- insta (https://github.com/mitsuhiko/insta)
|
|
|
|
|
|
|
|
### Examples
|
|
|
|
|
|
|
|
You can run example with following code.
|
|
|
|
Please see `examples` directory.
|
|
|
|
|
|
|
|
``` sh
|
|
|
|
$ cargo run --example [EXAMPLE_NAME]
|
|
|
|
```
|
|
|
|
|
|
|
|
For Example if you want to run `hello` example.
|
|
|
|
Please run following command.
|
|
|
|
|
|
|
|
``` sh
|
|
|
|
$ cargo run --example hello
|
|
|
|
```
|
|
|
|
|
|
|
|
So you can see output file in output directory.
|
|
|
|
|
|
|
|
### Testing
|
|
|
|
|
|
|
|
#### Rust
|
|
|
|
|
|
|
|
Please run following command.
|
|
|
|
|
|
|
|
```
|
|
|
|
make lint && make test
|
|
|
|
```
|
|
|
|
|
|
|
|
If snapshot testing is failed, fix code or update snapshot files. (See https://insta.rs/).
|
|
|
|
|
|
|
|
```
|
|
|
|
$ cargo-insta review
|
|
|
|
```
|
|
|
|
|
|
|
|
Then re run test.
|
|
|
|
|
|
|
|
```
|
|
|
|
$ make test
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Wasm
|
|
|
|
|
|
|
|
Please run following command.
|
|
|
|
|
|
|
|
```
|
|
|
|
$ cd docx-wasm && yarn install && yarn test
|
|
|
|
```
|
|
|
|
|
|
|
|
If snapshot testing is failed, fix code or update snapshot files. (See https://jestjs.io/docs/snapshot-testing).
|
|
|
|
|
|
|
|
```
|
|
|
|
$ yarn test -- --updateSnapshot
|
|
|
|
```
|
|
|
|
|
2020-01-17 17:19:07 +02:00
|
|
|
## Features
|
2019-11-15 11:15:43 +02:00
|
|
|
|
|
|
|
- [x] Paragraph
|
2020-06-08 07:41:13 +03:00
|
|
|
- [x] Alignment
|
|
|
|
- [x] Indent
|
|
|
|
- [x] Numbering
|
2020-01-24 11:50:16 +02:00
|
|
|
- [x] Run
|
2020-06-08 07:41:13 +03:00
|
|
|
- [x] Bold
|
|
|
|
- [x] Size
|
|
|
|
- [x] Font
|
|
|
|
- [x] Color
|
|
|
|
- [x] Highlight
|
|
|
|
- [x] Underline
|
|
|
|
- [x] vanish
|
|
|
|
- [x] Italic
|
2021-03-20 17:16:43 +02:00
|
|
|
- [x] TextBorder
|
2020-01-24 11:50:16 +02:00
|
|
|
- [x] Break
|
2021-03-02 05:31:46 +02:00
|
|
|
- [x] Header
|
2021-11-24 18:50:11 +02:00
|
|
|
- [x] Footer
|
2020-01-24 11:50:16 +02:00
|
|
|
- [x] Comment
|
2020-06-02 21:27:04 +03:00
|
|
|
- [x] Image
|
2020-01-24 11:50:16 +02:00
|
|
|
- [x] Style
|
|
|
|
- [x] Table
|
|
|
|
- [x] HIstory
|
|
|
|
- [ ] Table of contents
|
|
|
|
- [ ] Section
|
2020-02-26 13:26:32 +02:00
|
|
|
|