senny / sablon

Ruby Document Template Processor based on docx templates and Mail Merge fields.
MIT License
446 stars 128 forks source link

Dynamic image sizes #106

Closed LukasGutwinski closed 5 years ago

LukasGutwinski commented 6 years ago

First of all, thanks for working on Sablon! Apparently a lot has happened since I last checked the repo a year ago :+1:

Since images are now supported, I wonder if it’s possible to add images with dynamic sizes. We would like to include images that were uploaded by users in our Word report and they can have quite different dimensions.

The placeholder images seem to work well if the sizes are known in advance when creating the template but easily distort the images when working with dynamic content.

stadelmanma commented 6 years ago

In principle it's possible to support a wide range of attributes, although I'm not entirely certain how consistent the XML generated by MS Word is in regards to images. For now I'll assume the XML test snippet is representative all most modern word docs.

I don't have time right now to add it in myself but if you'd like to implement it and submit a PR I'd be happy to review it. Some initial thoughts:

I think these are the relevant bit that control the image size (extracted from the snippet ) <wp:extent cx="1875155" cy="1249045"/> and <a:ext cx="1875155" cy="1249045"/>. The units are in EMUs which is some strange internal unit Word uses to avoid floating point maths when doing conversions between inches and centimeters.

Useful link on sizing that explains things pretty well: https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml

XML snippet for images: https://github.com/senny/sablon/blob/master/test/fixtures/xml/image.xml

Sablon::Content class for images: https://github.com/senny/sablon/blob/b7c6fdb167823561de450f28bfdc178de98040f7/lib/sablon/content.rb#L176

Sablon::Processor::Document::ImageBlock class that handles inserting an image https://github.com/senny/sablon/blob/b7c6fdb167823561de450f28bfdc178de98040f7/lib/sablon/processor/document/blocks.rb#L89

LukasGutwinski commented 6 years ago

Thanks for your super fast and informative reply 😄 I gave this a try and created pull request #107 I could not quite figure out where to place the local_rid and how it behaves exactly so I just added the properties hash next to it. This is probably not ideal... (I'm not a professional developer and this is my first pull request for an open source project.)

Would be happy to hear your comments.

stadelmanma commented 6 years ago

@LukasGutwinski no worries, we all had to start somewhere. I still shudder at the thought of the first code I wrote. And congrats on your first PR! I have to warn you there is a certain addictive quality to being able to influence the software you use compared to the black boxes we usually have to work with :grinning:.

stadelmanma commented 5 years ago

I forgot to close this after merging in #107