How can I resize an image in an HTML generated word document whilst retaining the aspect ratio?

Tags: php,html,css,ms-word

Problem :

I've been building a word document from some HTML as per these fantastic stackoverflow resources:

For those who are about to try it, prepare yourself for some pain as Word is patchy to say the least when it comes to what CSS it works with. If you want columns, you really need to go back to using tables to lay stuff out.

I've been having trouble with getting an image to size properly - There are images that the user uploads which will be used in this Word document, and I would like to resize them based on the 'height' property - to a height of 50px and then have the width scaled accordingly.

It seems there is no documentation for how to do this anywhere on the web.

I've also used some html2canvas code to turn a nice looking (CSS-based) bar-chart into an image, which is then usable directly in the word document as per the excellent

This image is not printing out well, so I'm planning to make the source HTML larger on screen (hidden in memory) (using jQuery) before running the html2canvas code on it - this way I'll have a larger image which I can then reduce in size and get a higher DPI number.

Firstly, I've learned that CSS width and height have no impact on images in Word. Similarly, the following doesn't work.

<img class="cobrandedlogo" src="' . $logourl . '" height="50px" />';

But I have found that using 'points' works like so:

<img class="cobrandedlogo" src="' . $logourl . '" height="50" />';

But this produces a very stretched image (depending on the source material of course). I want it to be scaled according to the original image.

I'm using PHP.

I've discovered this SO article which it seems will help: Calculating image size ratio for resizing

Here's some code I've been using:

jQuery code to generate the image of a div, modify the displayed HTML a bit and submit it to a PHP file:

var wordreport = jQuery('#container').clone();
jQuery(wordreport).find('#stats').html('<img src="' + jQuery('#imgname').val() + '" />');
jQuery(wordreport).find('.columncontainer').each(function() {
// these were 3 columns on the screen - converted to a table and tds for Word
jQuery(wordreport).find('.memberstrengths').wrap('<td valign=top>'); 
jQuery(wordreport).find('.membercommunication').wrap('<td valign=top>');
jQuery(wordreport).find('.memberimproving').wrap('<td valign=top>');


var htmlpluscss = jQuery(wordreport).html();
htmlpluscss = '<style>.reportsection {border:1px solid #fff!important; padding:0px; margin-bottom:0px;}</style>' + htmlpluscss;
    onrendered: function (canvas) {
        //Set hidden field's value to image data (base-64 string)
        //Submit the form manually

Also this HTML for the button to generate the word doc:

<div id="generateword">
<form method="post" action="/createword.php" target="_blank" id="getword"> 
<input type="hidden" name="docname" id="docname" />
<input type="hidden" name="dochtml" id="dochtml" />
<input type="hidden" name="orientation" id="orientation" value="landscape" />
<input type="hidden" name="logourl" value="<?php echo get_user_meta(get_current_user_id(), 'reportlogourl', true); ?>" />
<input type="hidden" name="img_val" id="img_val" value="" />
<input type="hidden" name="imgname" id="imgname" value="<?php echo $_GET['teamid']; ?>.png" />
<input type="button" value="View in Word" id="createword" />

Finally - here's the PHP file I used:


header("Content-type: application/");
header("Content-Disposition: attachment;Filename=" . $_POST['docname'] . ".doc");

$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
//Save the image
$imgfile = $_POST['imgname'];
//file_put_contents("/assets/teamstats/" . $imgfile, $unencodedData);
file_put_contents("assets/teamstats/" . $imgfile, $unencodedData);

$orientation = 'portrait';
if(isset($_POST['orientation'])) {
$orientation = 'landscape';
$logourl = '';
if(isset($_POST['logourl'])) {
$logourl = $_POST['logourl'];

echo '<html xmlns:v="urn:schemas-microsoft-com:vml"
<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><title></title>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
if ($orientation == 'landscape') {
    echo 'mso-page-orientation: landscape;
    size:29.7cm 21cm;    margin:1cm 1cm 1cm 1cm;';
else {
    echo 'mso-page-orientation: portrait;
    size:21cm 29.7cm;    margin:1cm 1cm 1cm 1cm;';
echo '
@page Section1 {
    mso-header: h1;
    mso-footer: f1;
div.Section1 { page:Section1; 
if ($orientation == 'landscape') {
    echo 'font-size:12px; ';
else {
    echo 'font-size:14px; line-height:20px;';
echo '
    margin:0in 0in 0in 900in;
p.MsoHeader {
if ($orientation == 'landscape') {
    echo '    tab-stops:right 29.0cm;';
else {
    echo '    tab-stops:right 21.0cm;';
echo '
p.MsoFooter, li.MsoFooter, div.MsoFooter
if ($orientation == 'landscape') {
    echo 'tab-stops:center 14.5cm right 29.0cm;';
else {
    echo 'tab-stops:center 10.5cm right 21.0cm;';
echo '

echo "<body>";
echo '<div class="Section1">';
echo $_POST['dochtml'];
echo '<style>h3, table {
div.membername {
div.membername, span.membershape, span.membertraits {
h2.membername {
table.tdmembername, table.tdmembername tr, table.tdmembername td, table.tdmembername span {
div.columncontainer {
img.cobrandedlogo {
echo '
    <table id="hrdftrtbl" border="0" cellspacing="0" cellpadding="0">
    <tr><td>        <div style="mso-element:header" id=h1 >
            <p class=MsoHeader ><img src="" style="height:50px" height="50px"/><span style=mso-tab-count:1"></span>';
if ($logourl != "") {
    $newheight = 50;
    list($originalwidth, $originalheight) = getimagesize($logourl);
    $ratio = $originalheight / $newheight;
    $newwidth = $originalwidth / $ratio;
    echo '<img class="cobrandedlogo" src="' . $logourl . '" height="50" width="' . $newwidth . '" />';

echo '</p>
    <div style="mso-element:footer" id=f1><span style="position:relative;z-index:-1"> 
        <p class=MsoFooter>
           <span style=mso-tab-count:1"></span>
           <span style=mso-tab-count:1"></span>
           Page <span style="mso-field-code: PAGE "><span style="mso-no-proof:yes"></span> of <span style="mso-field-code: NUMPAGES "></span></span>

    <div style="mso-element:header" id=fh1>
        <p class=MsoHeader><span lang=EN-US style="mso-ansi-language:EN-US">&nbsp;<o:p></o:p></span></p>
        <div style="mso-element:footer" id=ff1>
        <p class=MsoFooter><span lang=EN-US style="mso-ansi-language:EN-US">&nbsp;<o:p></o:p></span></p>

echo "</body>";
echo "</html>";


How can I resize the images I'm inserting into word (using HTML) using just the height or width, so that Word retains the aspect ratio?

Solution :

I've managed to solve this myself:

If you want to scale an image based on one side in word using HTML then this code will do it:

$newheight = 50;
list($originalwidth, $originalheight) = getimagesize($logourl);
$ratio = $originalheight / $newheight;
$newwidth = $originalwidth / $ratio;
echo '<img class="cobrandedlogo" src="' . $logourl . '" height="50" width="' . $newwidth . '" />';

Swap newheight and newwidth if you want to scale based on the width instead.

    CSS Howto..

    How can I reposition a DIV with jQuery? [closed]

    How to get table-row css and border property work together?

    How to create and apply CSS to javascript Alert

    How do I add a class to a default bootstrap class

    How to Layout a Watermark Image that is transparent along with a banner image and content div?

    Bootstrap: How to get part of my banner image to hide under my sidebar?

    Bootstrap: navbar showing a line

    How to make div stack first vertically then horizontally? [duplicate]

    How to slice an Image Border for use in CSS?

    How to get the CSS width of a mobile device in ASP.NET MVC?

    How to cancel left or right rules with an !important tag?

    Showing images next to paragraph

    How to make a pure CSS div with a gradient background?

    How do I get only two form fields to show instead of all 5 on initial page load? CSS

    How to override PrimeFace CSS to customize my button?

    How do I remove the text underline (that I set myself) from a on hover?

    How to trigger CSS-Filter animation on Click (or Scrolling)

    How to set Highcharts data series in x-axis using crosshairs?

    How to make div size bigger?

    How to make a site have a built in user-friendly editor?

    How to do CSS nth-child in span tags?

    How to set width of a
    in CSS to X number of pixels less than the width:auto?

    How to add element to existing css class [closed]

    How can I turn off hardware acceleration for certain HTML elements via CSS?

    how to show button and images in one line using css

    How to create a underline with small break in middle with css?

    How do I auto play my slideshow

    How to detect which button is clicked in javascript?

    How to allow my CSS slide out element to push its neighboring content?

    background image of div element can't show in php file