function getImageURL($file_url, $thumb_width) {
$html = file_get_html('http://docs.google.com/gview?url=' . $file_url);
return doUrl(html_to_URL($html, "{svUrl:'", "46chan"), $thumb_width);
}
The first line will load the HTML of the Google docs page that displays your msword document as an image. The Second line will glean the docid. Unfortunately, we can't just use jQuery or this parser to grab the image's src because it is programatically generated by Google docs, it's won't show up in the source code. But the information that we need shows up inside a JavaScript variable in the source code. It looks messy, but, next we will run some string manipulations on the URL and retain the pertinent parts (the docid).
<script type="text/javascript">
function finalizeApp() {
if (!gviewApp) {
return;
}
gviewApp.setDisplayData(
{svUrl:'?url\75http://www.patsmitty.com/gview/word_documents/test.docx\46docid\7593c3e45e33f8c096913511ef8fe32a92\46chan\75DwAAAM2xz0nJvnNLiNHc/RtPoug%3D\46a\75sv',biUrl:'?url\75http://www.patsmitty.com/gview/word_documents/test.docx\46docid\7593c3e45e33f8c096913511ef8fe32a92\46chan\75DwAAAM2xz0nJvnNLiNHc/RtPoug%3D\46a\75bi',chanId:'DwAAAM2xz0nJvnNLiNHc/RtPoug\075',gpUrl:'http://doc-0k-8g-docsviewer.googleusercontent.com/viewer/securedownload/dsn1aovipa7l846lsfcf94nedj8q2p4u/vgceh33q6a9930abfmgliebnltb0nljm/1312587900000/dXJs/AGZ5hq8BgbJY1gwaOYx83cPOdNw6/aHR0cDovL3d3dy5wYXRzbWl0dHkuY29tL2d2aWV3L3dvcmRfZG9jdW1lbnRzL3BhdF9zbWl0aC5kb2N4?a\75gp\46filename\75test.docx\46chan\75DwAAAM2xz0nJvnNLiNHc/RtPoug%3D\46docid\7593c3e45e33f8c096913511ef8fe32a92\46sec\75AHSqidYnfLc11kcuxtjsvPOT1apoyI52utATnDA0dbZG7oiQ3GYFpmaw454_bppEvls9ZMLaqb-V',docId:'93c3e45e33f8c096913511ef8fe32a92',numPages:1,gtUrl:'?url\75http://www.patsmitty.com/gview/word_documents/test.docx\46docid\7593c3e45e33f8c096913511ef8fe32a92\46chan\75DwAAAM2xz0nJvnNLiNHc/RtPoug%3D\46a\75gt',thWidth:138,dlUrl:'http://www.patsmitty.com/gview/word_documents/test.docx',thHeight:179});
gviewApp.finalizeApp();
gviewApp.loadLateDeps();
}
gviewApp.setProgress(90);
finalizeApp();
window.jstiming.load.tick('prt');
</script>
Notice on line 10 the docid parameter is available in between the 2 strings noted above. So inside this html_to_URL function we're going to single out the following from the example above:
?url\75http://www.patsmitty.com/gview/word_documents/test.docx\46docid\7593c3e45e33f8c096913511ef8fe32a92Here is the code that goes inside this function to accomplish this result:
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
$final = substr($string,$ini,$len-1);
return $final;
By looking at the my example URL above, we can see a couple of things that need fixing up. Look at the beginning, it starts with "?url\75http://www...". This is obviously the url parameter in the final image's URL. So we need to make the beginning look like this instead: "?url=http://www...". Also, a little later, we see a "\46". This is supposed to be an ampersand that declares the next parameter which is the most important: docid. So this last function will take care of this generated URL and make it usable. This function called doURL takes 2 parameters called $final_url and $width. $final_url is the raw URL in quotes above. This function will turn
?url\75http://www.patsmitty.com/gview/word_documents/test.docx\46docid\7593c3e45e33f8c096913511ef8fe32a92into
?url=http://www.patsmitty.com/word_documents/patty.docx&docid=3e4a3f6ecf6625dccc407c11df17dbfcHere is the code for doURL:
$url_final = str_replace("\\75", "=", $url_final);
$url_final = str_replace("\/", "%2F", $url_final);
$url_final = str_replace("\\46", "&", $url_final);
$url_final = "http://docs.google.com/gview" . $url_final;
$url_final = $url_final . "&a=bi&pagenumber=1&w=" . $width;
return $url_final;
Notice lines 4 and 5... They add "http://docs.google.com/gview" to the beginning and "&a=bi&pagenumber=1&w=" . $width" to the end. So now our URL looks exactly like it does when we click on "view image" or "copy image location" from the context menu when we right-clicked on the image of the first page of our msword document that we viewed in Google docs!!!
<?php
include('msword.php');
if ($handle = opendir('word_documents')) {
echo '<table>';
$c = 0;
while (false !== ($file = readdir($handle))) {
if(strripos($file, ".doc")!=false):
if($c==4):
echo '</tr>';
$c=0;
endif;
if($c==0):
echo '<tr>';
endif;
$url = getImageUrl('http://www.patsmitty.com/gview/word_documents/'.$file, '125');
$click_url = 'http://docs.google.com/gview?url=http://www.patsmitty.com/gview/word_documents/'.$file;
echo '<td><div style="padding-left:5px;padding-top:5px;" class="imgdiv"><img class="docs" onmouseover="this.style.cursor=\'pointer\'" onclick="window.location=\''.$click_url.'\'" src="'.$url.'" /></div><td>';
$c++;
endif;
}
echo '</table>';
closedir($handle);
}
?>
That's It!!!We now have images of our word documents that we can show on our page. Take note that Google Docs supports other formats than just .doc and .docx but .pdfs and spreadsheets as well so these are all feasible with my hack as well. I have only tested .doc, .docx, and .pdf. Also, please look at my source code as it shows how to connect all these functions together as I am not explaining any html, parsing, css, or other php functions consumed here - I'm simply explaining my hack so you too can enhance your web applications. Also remember to change all the references from "www.patsmitty.com/gview/..." to your respected servers.
If you have any questions please post here or email me at psmith@patsmitty.com and I'll post them here.gview.zip
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)