This library allows you to quickly convert Microsoft Word documents to image through msword2image.com using PHP for free!
Example conversion: From demo.docx to output.png.
Note that you can try this out by visting msword2image.com and clicking "Want to convert just one?"
You can simply download this github repo as a zip file. Extract contents and include MsWordToImageConvert.php
require_once 'lib/MsWordToImageConvert.php';
Note: Please make sure cURL is enabled with your PHP installation
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromURL('http://mswordtoimage.com/docs/demo.doc');
$convert->toFile('demo.jpeg');
// Please make sure output file is writable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromURL('http://mswordtoimage.com/docs/demo.doc');
$base64String = $convert->toBase46EncodedString();
echo "<img src='data:image/jpeg;base64,$base64String' />";
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('demo.doc');
$convert->toFile('demo.jpeg');
// Please make sure output file is writable and input file is readable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('demo.doc');
$base64String = $convert->toBase46EncodedString();
echo "<img src='data:image/jpeg;base64,$base64String' />";
// Please make sure input file is readable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('demo.doc');
$base64String = $convert->toBase46EncodedString(\MsWordToImageConvert\OutputImageFormat::GIF);
echo "<img src='data:image/gif;base64,$base64String' />";
// Please make sure input file is readable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('demo.doc');
$base64String = $convert->toBase46EncodedString(\MsWordToImageConvert\OutputImageFormat::JPEG);
echo "<img src='data:image/png;base64,$base64String' />";
// Please make sure input file is readable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('demo2.doc');
$toPageCount = $convert->toPageCount();
// $toPageCount will be integer representing the page count in the word file
// Please make sure input file is readable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('http://msword2image.com/docs/demo2.doc');
$toPageCount = $convert->toPageCount();
// $toPageCount should be 5
// Please make sure input file is readable by your PHP process.
$convert = new MsWordToImageConvert($apiUser, $apiKey);
$convert->fromFile('demo2.doc');
$base64String = $convert->toBase46EncodedString(
\MsWordToImageConvert\OutputImageFormat::JPEG,
2
);
echo "<img src='data:image/jpeg;base64,$base64String' />";
// Note that pages are 0-indexed. Above code will print the third page of word document
// Please make sure input file is readable by your PHP process.
Input\Output | PNG | GIF | JPEG | Page count |
DOC | ✔ | ✔ | ✔ | ✔ |
DOCX | ✔ | ✔ | ✔ | ✔ |