I know, I know. I’ve been absolutely terrible about updating. My running days have been rough. I’m nursing a knee injury that I’d rather take care of and not have come back. I managed to knock off 12 minutes off my 10K PR though in a race about a month ago! I was very excited about that.

Anywho, this post is not about running. It’s about something I learned at work. I have been developing an application that spits out a filled Word document. Not only that, I had a template I wanted to use as well. What to do? Enter PHPWord. It’s a PHP class that allows you to create brand new Word documents, use a template, add images. All this without having to open Microsoft Word. The zip file from Github has a gazillion examples. The issue I was having was trying to add an image to the template. I ran into this StackOverflow form post. If you add the code they provide to the Template.php, this gives you the ability to replace an image placeholder in the document. They even give you this code bit on how to use it:

$document->setImageValue('image1.jpg', 'my_image.jpg');

Okay. Looks simple. however, this part took me hours to figure out where the first parameter is coming from. The second parameter is the new image you want to place in the template. The first parameter is supposed to be my image placeholder. So I placed the image in, changed the first param to what I called the image. No bueno. No matter what I did. I just couldn’t get it. Until I saw in another form to view the Word template doc using WinRAR. I found that odd, but I did it. And viola!! Word documents are really like one big folder. Which makes sense when you think of it. If you go into the ‘word/media’ folder, you’ll see what the name of your place holder image. Simple once you think about it.

That’s my Programming tip of the day (err…month) is. 😀

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *