Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spike: 4h] Research how to inject images into Word doc #19

Closed
jcscottiii opened this issue Jul 20, 2016 · 5 comments
Closed

[Spike: 4h] Research how to inject images into Word doc #19

jcscottiii opened this issue Jul 20, 2016 · 5 comments
Assignees
Labels

Comments

@jcscottiii
Copy link
Member

jcscottiii commented Jul 20, 2016

In order to get a better idea of how to handle embedding images into the ssp, we need to see if it's even possible to embed into a word doc. This spike is to test out how feasible it is to do in the current code base and if there are any alternatives / helpers.

Will determine how to tackle opencontrol/compliance-masonry#131

@jcscottiii
Copy link
Member Author

jcscottiii commented Jul 26, 2016

it can be done!

Empty docx:

image

With picture:

image

There are some complications. AFAIK, you can't simply replace text with a picture tag. The tags that contain the text, <w:t>, cannot have an image tag inside it at all. it will take it literally and render text.

In this case you see you could try one of two things:

  1. Go up the hierarchy chain of the tags and delete the <w:t> tag.
  2. Spoof a new section.
    • Prior to the image section. Close off all the text and section tags. Insert the image tag. Then reopen the text and section tags.

Additional Information

docx is a glorified .zip file. It contains many files. Until now, we only had to worry about word/document.xml, which contains the basic content of the document.

word/_rels/document.xml.rels

This file contains all the relationships for assets for the document file. Even in a blank .docx file, this file is already populated.

Sample

<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/><Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
</Relationships>

Important parts

The relationship Id. and the Type and Target

  • The Id will be used to refer to the asset (in our case the image) in the word/document.xml.
  • The Type will refer to the asset type. In our case image.
  • The Target will refer to the location in the docx archive of where the image is.

_rels considerations

When reading the .docx file, we need to load up the current relationships and keep a counter on the last used relationship id to make sure we keep unique ids.

word/media/*

All the photos have to be placed in the word/media folder of the docx archive.

Each file must have a number on it.
For example:

word/media/image1.png
word/media/image2.png

@jcscottiii
Copy link
Member Author

PR with work here: opencontrol/doc-template#5

@mogul
Copy link

mogul commented Jul 27, 2016

Research story mission accomplished.

@mogul mogul closed this as completed Jul 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants