-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support image in header footer (#680)
* wip * wip * wip * fix: image in header/footer * fix: Changelog
- Loading branch information
Showing
55 changed files
with
4,534 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use docx_rs::{Docx, Header, Paragraph, Pic, Run}; | ||
use std::{error::Error, io::Cursor}; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let cat = Pic::new(include_bytes!("../../images/cat_min.jpg")); | ||
let header = | ||
Header::new().add_paragraph(Paragraph::new().add_run(Run::new().add_image(cat.clone()))); | ||
let mut out = Vec::new(); | ||
let docx = Docx::new() | ||
.header(header) | ||
.add_paragraph(Paragraph::new().add_run(Run::new().add_image(cat))); | ||
docx.build().pack(Cursor::new(&mut out))?; | ||
|
||
std::fs::write("/tmp/out.docx", &out)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::documents::BuildXML; | ||
use crate::{xml_builder::*, ImageIdAndPath}; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct FooterRels { | ||
pub images: Vec<(String, String)>, | ||
} | ||
|
||
impl FooterRels { | ||
pub fn new() -> FooterRels { | ||
Default::default() | ||
} | ||
|
||
pub fn add_image(mut self, id: impl Into<String>, path: impl Into<String>) -> Self { | ||
self.images.push((id.into(), path.into())); | ||
self | ||
} | ||
|
||
pub(crate) fn set_images(&mut self, images: Vec<ImageIdAndPath>) { | ||
self.images = images; | ||
} | ||
} | ||
|
||
impl BuildXML for FooterRels { | ||
fn build(&self) -> Vec<u8> { | ||
let mut b = XMLBuilder::new(); | ||
b = b | ||
.declaration(None) | ||
.open_relationships("http://schemas.openxmlformats.org/package/2006/relationships"); | ||
|
||
for (id, path) in self.images.iter() { | ||
b = b.relationship( | ||
id, | ||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", | ||
path, | ||
) | ||
} | ||
|
||
b.close().build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::documents::BuildXML; | ||
use crate::{xml_builder::*, ImageIdAndPath}; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct HeaderRels { | ||
pub images: Vec<(String, String)>, | ||
} | ||
|
||
impl HeaderRels { | ||
pub fn new() -> HeaderRels { | ||
Default::default() | ||
} | ||
|
||
pub fn add_image(mut self, id: impl Into<String>, path: impl Into<String>) -> Self { | ||
self.images.push((id.into(), path.into())); | ||
self | ||
} | ||
|
||
pub(crate) fn set_images(&mut self, images: Vec<ImageIdAndPath>) { | ||
self.images = images; | ||
} | ||
} | ||
|
||
impl BuildXML for HeaderRels { | ||
fn build(&self) -> Vec<u8> { | ||
let mut b = XMLBuilder::new(); | ||
b = b | ||
.declaration(None) | ||
.open_relationships("http://schemas.openxmlformats.org/package/2006/relationships"); | ||
|
||
for (id, path) in self.images.iter() { | ||
b = b.relationship( | ||
id, | ||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", | ||
path, | ||
) | ||
} | ||
|
||
b.close().build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use crate::{ | ||
DeleteChild, DrawingData, InsertChild, Paragraph, ParagraphChild, RunChild, | ||
StructuredDataTagChild, Table, TableCellContent, TableChild, TableRowChild, TocContent, | ||
}; | ||
|
||
pub(crate) fn collect_images_from_paragraph( | ||
paragraph: &mut Paragraph, | ||
images: &mut Vec<(String, String)>, | ||
image_bufs: &mut Vec<(String, Vec<u8>)>, | ||
id_prefix: Option<&str>, | ||
) { | ||
for child in &mut paragraph.children { | ||
if let ParagraphChild::Run(run) = child { | ||
for child in &mut run.children { | ||
if let RunChild::Drawing(d) = child { | ||
if let Some(DrawingData::Pic(pic)) = &mut d.data { | ||
let b = std::mem::take(&mut pic.image); | ||
let buf = image_bufs.iter().find(|x| x.0 == pic.id || x.1 == b); | ||
let pic_id = if let Some(prefix) = id_prefix { | ||
format!("{}{}", prefix, pic.id) | ||
} else { | ||
pic.id.clone() | ||
}; | ||
if buf.as_ref().is_none() { | ||
images.push(( | ||
pic_id.clone(), | ||
// For now only png supported | ||
format!("media/{}.png", pic_id), | ||
)); | ||
image_bufs.push((pic_id.clone(), b)); | ||
pic.id = pic_id; | ||
} else { | ||
pic.id = buf.unwrap().0.clone(); | ||
} | ||
} | ||
} | ||
} | ||
} else if let ParagraphChild::Insert(ins) = child { | ||
for child in &mut ins.children { | ||
match child { | ||
InsertChild::Run(run) => { | ||
for child in &mut run.children { | ||
if let RunChild::Drawing(d) = child { | ||
if let Some(DrawingData::Pic(pic)) = &mut d.data { | ||
images.push(( | ||
pic.id.clone(), | ||
// For now only png supported | ||
format!("media/{}.png", pic.id), | ||
)); | ||
let b = std::mem::take(&mut pic.image); | ||
image_bufs.push((pic.id.clone(), b)); | ||
} | ||
} | ||
} | ||
} | ||
InsertChild::Delete(del) => { | ||
for d in &mut del.children { | ||
if let DeleteChild::Run(run) = d { | ||
for child in &mut run.children { | ||
if let RunChild::Drawing(d) = child { | ||
if let Some(DrawingData::Pic(pic)) = &mut d.data { | ||
images.push(( | ||
pic.id.clone(), | ||
// For now only png supported | ||
format!("media/{}.png", pic.id), | ||
)); | ||
let b = std::mem::take(&mut pic.image); | ||
image_bufs.push((pic.id.clone(), b)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} else if let ParagraphChild::Delete(del) = child { | ||
for d in &mut del.children { | ||
if let DeleteChild::Run(run) = d { | ||
for child in &mut run.children { | ||
if let RunChild::Drawing(d) = child { | ||
if let Some(DrawingData::Pic(pic)) = &mut d.data { | ||
images.push(( | ||
pic.id.clone(), | ||
// For now only png supported | ||
format!("media/{}.png", pic.id), | ||
)); | ||
let b = std::mem::take(&mut pic.image); | ||
image_bufs.push((pic.id.clone(), b)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn collect_images_from_table( | ||
table: &mut Table, | ||
images: &mut Vec<(String, String)>, | ||
image_bufs: &mut Vec<(String, Vec<u8>)>, | ||
id_prefix: Option<&str>, | ||
) { | ||
for TableChild::TableRow(row) in &mut table.rows { | ||
for TableRowChild::TableCell(cell) in &mut row.cells { | ||
for content in &mut cell.children { | ||
match content { | ||
TableCellContent::Paragraph(paragraph) => { | ||
collect_images_from_paragraph(paragraph, images, image_bufs, id_prefix); | ||
} | ||
TableCellContent::Table(table) => { | ||
collect_images_from_table(table, images, image_bufs, id_prefix) | ||
} | ||
TableCellContent::StructuredDataTag(tag) => { | ||
for child in &mut tag.children { | ||
if let StructuredDataTagChild::Paragraph(paragraph) = child { | ||
collect_images_from_paragraph( | ||
paragraph, images, image_bufs, id_prefix, | ||
); | ||
} | ||
if let StructuredDataTagChild::Table(table) = child { | ||
collect_images_from_table(table, images, image_bufs, id_prefix); | ||
} | ||
} | ||
} | ||
TableCellContent::TableOfContents(t) => { | ||
for child in &mut t.before_contents { | ||
if let TocContent::Paragraph(paragraph) = child { | ||
collect_images_from_paragraph( | ||
paragraph, images, image_bufs, id_prefix, | ||
); | ||
} | ||
if let TocContent::Table(table) = child { | ||
collect_images_from_table(table, images, image_bufs, id_prefix); | ||
} | ||
} | ||
|
||
for child in &mut t.after_contents { | ||
if let TocContent::Paragraph(paragraph) = child { | ||
collect_images_from_paragraph( | ||
paragraph, images, image_bufs, id_prefix, | ||
); | ||
} | ||
if let TocContent::Table(table) = child { | ||
collect_images_from_table(table, images, image_bufs, id_prefix); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.