0

My question is that i want to open documents(pdf,doc,docx,txt) in browser page using php (without using google docs viewer) can any one help me?

zubair
  • 185
  • 3
  • 15
  • 1
    So, you want to translate those document types to HTML, using PHP? – Charles Mar 12 '11 at 05:00
  • Here are two of your questions answered: [parsing .pdf files](http://stackoverflow.com/questions/1251956/is-there-a-pdf-parser-for-php) and [parsing .docx files](http://stackoverflow.com/questions/173246/parsing-and-generating-microsoft-office-2007-files-docx-xlsx-pptx). The second answer also brieflt mentions .doc files. While it sounds like getting into a docx file and reading the contents might be doable, pdf files are a complex nightmare. If you were more specific about your needs, and why you need them, we might be able to help a bit more. – Surreal Dreams Mar 12 '11 at 05:19

3 Answers3

2

Some of these are doable. Some, not so much. Let's tackle the low-hanging fruit first.

Text files

You can just wrap the content in <pre> tags after running it through htmlspecialchars.

PDF

There is no native way for PHP to turn a PDF document into HTML and images. Your best bet is probably ImageMagick, a common image manipulation program. You can basically call convert file.pdf file.png and it will convert the PDF file into a PNG image that you can then serve to the user. ImageMagick is installed on many Linux servers. If it's not available on your host's machine, please ask them to install it, most quality hosts shouldn't have a problem with this.

DOC & DOCX

We're getting a bit more tricky. Again, there's no way to do this in pure PHP. The Docvert extension looks like a possible choice, though it requires OpenOffice be installed as well. I was actually going to recommend plain vanilla OpenOffice/LibreOffice as well, because it can do the job directly from the command line. It's very unlikely that a shared host will want to install this. You'll probably need your own dedicated or virtual private server.

In the end, while these options can be made to work, the output quality is not guaranteeable. Overall, this is kind of a bad idea that you should not seriously consider implementing.

Charles
  • 50,943
  • 13
  • 104
  • 142
1

I am sure libraries and such exist that can do this. Google could probably help you there more than I can.

For txt files I would suggest breaking lines after a certain number of characters and putting them inside pre tags.

I know people will not be happy about this response, but if you are on a Linux environment and have pdf2html installed you could use shell_exec and call pdf2html.

Note: If you use shell_exec be wary of what you pass to it since it will be executed on the server outside of PHP.

Jason
  • 1,114
  • 1
  • 10
  • 24
0

I thought I'd just add that pdfs generally view well in a simple embed tag. Or use an object so you can have fall backs if it cannot be displayed on the client.

Patrick Geyer
  • 1,515
  • 13
  • 30