Skip to main content

package_to_bytes

Function package_to_bytes 

Source
pub fn package_to_bytes(pkg: &OoxmlPackage) -> Result<Vec<u8>, String>
Expand description

Writes the package as a ZIP archive.

Entries are written straight into one output buffer in name order. XML and uncompressed binaries use DEFLATE; already-compressed media, and any entry that DEFLATE does not shrink, are stored.

Examples found in repository?
examples/image_fidelity.rs (line 19)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut args = std::env::args_os().skip(1);
5    let input = args.next().ok_or("Input document is required")?;
6    let output = args.next().ok_or("Output model path is required")?;
7    let package = parse_document_bytes(&std::fs::read(input)?)?;
8    let model = build_doc_model(&package);
9    std::fs::write(output, serde_json::to_vec(&model)?)?;
10    if let Some(output) = args.next() {
11        let mut rebuilt = model;
12        for node in &mut rebuilt.nodes {
13            if let rust_docx::DocNode::Paragraph(paragraph) = node {
14                paragraph.source_xml = None;
15                paragraph.source_text_patch = None;
16            }
17        }
18        let exported = try_serialize_doc_model(&rebuilt, Some(&package))?;
19        std::fs::write(output, package_to_bytes(&exported)?)?;
20    }
21    Ok(())
22}