Comments (17)
Regarding the module organization, I like the idea of making drawing
its own top-level. I would also suggest putting more than one element per file when they're tightly coupled
from docx.
Done! docx
now supports images:
https://github.com/dolanmiu/docx/wiki/Images
Will Release in 4.x.x
from docx.
Work is being done here:
https://github.com/dolanmiu/docx/tree/image-support
Following the following documentation:
https://github.com/dolanmiu/Office-Open-XML-Reference/wiki/DrawingML-Pictures-Overview
https://github.com/dolanmiu/Office-Open-XML-Reference/wiki/Pictures-Image-Properties
from docx.
@felipeochoa What do you think of this idea:
A Media
class, which is like a data store for all the images, and other media etc. It will have addMedia()
, and getMedia()
methods. It is analogous to the Style
and Numbering
class. The Media
class then gets added into the exporter/packer
as a parameter, like the rest.
I say this because whenever you add an image, the image goes into the /media
folder in the .docx
file.
Then you can do:
const document = new docx.Document();
const media = new docx.Media();
media.addMedia("./my/cool/image.png", "cool-image");
const paragraph = new docx.Paragraph();
paragraph.addPictureRun(media.getMedia("cool-image"));
document.addParagraph(paragraph);
What do you think? Got any other suggestions?
I think this would be cool:
paragraph.addPicture("./my/cool/image.png");
But I do not think its possible with the way Word
works with the whole rId5
and media
folder and all.
from docx.
I just looked over the references you linked, and I like the approach. What would this look like when packing? If I understand correctly, it looks like there are three things that need to happen:
- The
<pic/>
element is inserted in the<w:body>
somewhere - The basic picture information needs to be stored somewhere. I guess this is like the
styles
top-level file? - The picture binary data needs to be added to the
media
folder
The first point is taken care of by paragraph.addPictureRun
and is straightforward. The second two are where I think you'll need support from the packer. In order to keep things simple and flexible, would it be better to have addMedia
accept a readable stream, a filename, and a media ID instead of filename and ID? So you would do:
media.addMedia(fs.createReadableStream("./my/cool/image.png"), "cool-image", "cool-image.png");
That way if users have non file-system images, they can add them easily. It also enables users to specify a different in-archive name. The third argument could even be optional and default to a random 16-character word. Archiver's .append
accepts streams, so it would be straightforward to implement the 2nd and 3rd points.
from docx.
Also, a couple of small consistency points:
- What other types of media are there? Is there a reason to call the class
Media
rather thanPictures
? - I think as it looks now, it should be
createMedia
notaddMedia
from docx.
<pic>
is added inside<graphic>
, which is inside<drawing>
, which is inside a<run>
- I do not think picture information is stored in the styles, its stored inside the
<graphic>
mentioned above - Yes, the actual binary file is inside the media folder
I want paragraph to have a universal addRun
, which supports both TextRun
and PictureRun
Yes I agree createMedia
will make the API more consistent
and yes, createMedia
should take file-path as well as readable stream, perhaps an overloaded function
Whoops! I got mixed up, yes there is only one type of media (I believe), which is picture, but there are multiple types of drawing:
- Pictures
- Shapes
- Diagrams
- Textboxes
Reference: https://github.com/dolanmiu/Office-Open-XML-Reference/wiki/DrawingML-Overview
from docx.
perhaps createMedia
should now be called createPicture
from docx.
Oh ok got it. I like createPicture
and the overall approach!
from docx.
Hey @felipeochoa, could you lend a hand? I have you most of the images working, but when I try to create it, it gives a blank picture with an error. I am not sure whats wrong or whats causing this.
Could you take a look. Screenshot is attached:
Everything is on the image-support
branch
from docx.
@dolanmiu I was looking through the code, but there's a lot going on. How do I reproduce the file you in the screenshot? Can you give me a quick overview of the code you've written for this?
from docx.
To reproduce, simply:
npm run demo
In the command line
p.s. I have invited you as a collaborator on the project :D
from docx.
Thanks for adding me! I'll continue to send PRs to give you a chance to weigh in on the code if you want 😄
Regarding the images, here are some small issues I discovered:
- The
pic:pic
element isn't setting thexmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
attritbue, which it should so that the XML reader can interpret all thepic:
namespaces. - The
wp:inline
element must havedocPr
andextent
children - The
a:graphicData
element requires auri
attribute. (LibreOffice sets it to "http://schemas.openxmlformats.org/drawingml/2006/picture") pic:pic
must havenvPicPr
andspPr
children
Unfortunately, fixing those issues is not enough to get the picture to display. I still don't have a great understanding of how the picture embedding works, so not sure I can be of much more use right now. Maybe these comments are enough to get you unstuck.
As a side note, splitting the code across so many files and directories makes it challenging to understand what is going on. Especially ts/docx/run/run-components/drawing/
with its deeply nested directories was tough.
from docx.
That would be nice 😂
on the image-support
branch, I have done all those things! On master
, its outdated.
This is the following result:
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="3947160" cy="3947160"></wp:extent>
<wp:effectExtent l="0" t="0" r="0" b="0"></wp:effectExtent>
<wp:docPr id="1" name="Picture 1" desc="cats.jpeg"></wp:docPr>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1">
</a:graphicFrameLocks>
</wp:cNvGraphicFramePr>
<a:graphic
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="Picture 1"></pic:cNvPr>
<pic:cNvPicPr>
<a:picLocks noChangeAspect="1" noChangeArrowheads="1"></a:picLocks>
</pic:cNvPicPr>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId6"></a:blip>
<a:srcRect></a:srcRect>
<a:stretch>
<a:fillRect></a:fillRect>
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0"></a:off>
<a:ext cx="3947160" cy="3947160"></a:ext>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst></a:avLst>
</a:prstGeom>
<a:noFill></a:noFill>
<a:ln>
<a:noFill></a:noFill>
</a:ln>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
Yes, I agree, it is quite tough to understand. But I thought that is better than having it all in one single folder. I am thinking of having drawing
as a whole new root folder, the same level as docx, because in the Open Office XML Documentation, drawing is cross-app. By that I mean, drawing works with docx
, xlsx
, pptx
etc.
I will write unit tests after an image appears on screen, because all this is experimental and broken. Hope that is ok!
from docx.
Oh oops. Didn't see that. I created a test document in LibreOffice and then manually modified to mimick this structure, and it works just fine. One thing that struck me is the use of id 0
in cNvPr
, but that didn't break the LO document. Also, instead of desc
cNvPr
should have descr
, but that didn't break the document either.
This is the document I made, in case it's useful to compare.
from docx.
Hi there.
Are there any plans of merging image-support into master? Or is this a bit stuck?
Thanks and BR
from docx.
It's a bit stuck, image support branch isnt completed yet
Feel free to make a PR! 😄
from docx.
Related Issues (20)
- Repeat header in row HOT 1
- Attempted import error: 'Packer' is not exported from 'docx' (imported as 'Packer'). HOT 7
- Packer.toBuffer error in browser: nodebuffer not supported by this platform HOT 1
- Lists - instance not working with level greater than 0
- ExternalHyperlink renders empty line HOT 3
- tableHeader repeats row only if declared on the first row in a table
- el.elements not iterable when patching comments
- can not set indent by chars HOT 2
- Adding different numbering within the same line of paragraph HOT 12
- Field Codes in SimpleField Formulas HOT 1
- Corrupt Word document from patching with an XML attribute with an ampersand HOT 3
- After Patching, Following Text is Wrong Font Family HOT 1
- Possibility of adding hanging without affecting left indent of the paragraph.
- `PageNumber.TOTAL_PAGES_IN_SECTION` not documented and not functional, but within TS enum HOT 2
- Colors are not showing properly HOT 2
- When I open a document using WPS office, the width of the table cannot fill the entire page HOT 2
- Add accent characters
- The MathIntergral limLoc seems to be wrong
- Doesn't work with bg-black HOT 1
- Couple issues with embedded fonts HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docx.