A .docx file writer with Rust/WebAssembly.
[dependencies]
docx-rs = "0.2"
$ yarn add docx-wasm
use docx_rs::*;
pub fn hello() -> Result<(), DocxError> {
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(())
}
import { saveAs } from "file-saver";
// // Note that a dynamic `import` statement here is required due to webpack/webpack#6615,
import("docx-wasm").then((w) => {
const { buffer } = new w.Docx()
.addParagraph(
new w.Paragraph().addRun(new w.Run().addText("Hello world!!"))
)
.build();
saveAs(new Blob([buffer]), "hello.docx");
});
const w = require("docx-wasm");
const { writeFileSync } = require("fs");
const { buffer } = new w.Docx()
.addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!!")))
.build();
writeFileSync("hello.docx", buffer);
- Node.js 16+
- yarn 1+
- wasm-pack0.10.1 (https://rustwasm.github.io/wasm-pack/)
- insta (https://github.com/mitsuhiko/insta)
You can run example with following code.
Please see examples
directory.
$ cargo run --example [EXAMPLE_NAME]
For Example if you want to run hello
example.
Please run following command.
$ cargo run --example hello
So you can see output file in output directory.
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
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
- Paragraph
- Alignment
- Indent
- Numbering
- Run
- Bold
- Size
- Font
- Color
- Highlight
- Underline
- vanish
- Italic
- TextBorder
- Break
- Header
- Footer
- Comment
- Image
- Style
- Table
- HIstory
- Table of contents
- Section
- Textbox