Possibility to view doc,docx in the browser

Asked

Viewed 1,783 times

1

Is there a library that can view documents (doc,docx) to be viewed in the browser on an intranet without needing external resources like google Docs or office live? I need some component that doesn’t need external access.

  • I don’t think so, unless you implement a solution like the Google Docs Api, to open as a document in your browser you can use this: http://stackoverflow.com/questions/4346117/how-can-i-view-open-a-word-document-in-my-browser-using-with-php-or-html

  • @lvcs requires a plugin installed in the browser to view the doc through the solution put in the response of this link, unless it does what you mentioned above, implement with Google Docs. However, if the user is not editing the file it would be better to use the PDF format, at least browsers already support it.

  • I understand the issue that we cannot prevent the upload of attachments of type doc or docx and there is a need to check these files. I searched some places and some people convert to pdf to view in the browser ai to searching how to do this way.

  • Another option is to upload the file to some cloud api, hosting the doc, and from there, taking the link from it and sending it to the google Docs api

  • @lvcs But following this line we would be stuck to an external solution that with google Docs would solve.

  • http://stackoverflow.com/questions/5538584/convert-word-doc-docx-and-excel-xls-xlsx-to-pdf-with-php

  • @Thallesdaniel but meets the goal which is to render even if it is on local server

Show 2 more comments

1 answer

5


If the case is just visualize

Solution practice, if it is to render only, convert to PDF, this technically solves a lot of compatibility problem between different browsers and even absence of plugins or activex (Internet Explorer), as today the popular browsers already have natively embedded PDF readers:

  • Google Chrome (or Chromium-based browsers) has the Chrome PDF Viewer
  • Firefox has PDF.js (which can be used for even in the client-side)

Mobile browsers apparently already have built-in API which for PDF makes view

  • Alternative solutions (taken from Soen):

    Office 365:

      <iframe src="https://view.officeapps.live.com/op/embed.aspx?src={URL DO DOCUMENTO}"></iframe>
    

    Use google Docs:

      <iframe src="http://docs.google.com/gview?url={URL DO DOCUMENTO}&amp;embedded=true"></iframe>
    

Note: change {URL DO DOCUMENTO} by the desired URL

However you can implement with PDF.js in any modern browser and even customize the viewer your needs, simple example:

Using server side conversion

Although I really believe that the best is to send in PDF format to the production server for the following reasons:

  • Not every conversion will be perfect using server-side converters
  • If you do the conversion previously using Msword itself (or another office) that usually already has native add-on to save as PDF and send.

However still you may want to do the conversion on the server, if the server is a linux is install the unoconv, some linux will have this via repository, with a SSH and apt-get or yum may be able to install supported distros:

  • Red Hat
  • Debian
  • Fedora
  • Mandriva
  • Ubuntu Lucid
  • Opensuse

Or you can compile on the server:

Requirements: unoconv requires Python and requires Libreoffice or Openoffice with UNO.

An example of terminal use:

$ /home/compilado/unoconv /home/user/meudocumento.doc

In PHP you can use:

<?php

//Seta o arquivo que será convertido (pode ser um arquivo vindo de um upload)
$input  = '/home/user/meudocumento.doc';

//Define aonde será salvo
$output = '/home/user/meudocumento.pdf';


//Caminho do executável acaso não esteja global, se estiver global basta setar 'unoconv'
$exec = '/home/compilado/unoconv';

//Escapar os argumentos (este é necessário para evitar problemas com espaços e acentos)
$inputarg  = escapeshellarg($input);
$outputarg = escapeshellarg($output);

//Executa o comando
$resultado = shell_exec($exec . ' -f pdf -o ' . $outputarg . ' ' . $inputarg);

//Resposta da conversão
var_dump($resultado);

Phpoffice

As I explained in this reply /a/100798/3635, Phpoffice offers such support

To install add to require: of your composer.json (it is necessary to have the dompdf also to write the PDF):

{
    "require": {
        "dompdf/dompdf": "0.6.*",
        "phpoffice/phpword": "v0.13.*"
    }
}

And then run on the terminal or cmd:

cd c:\wamp\www\projeto
composer update

To convert a Word to PDF you only need to import the libraries (you will need to use composer), follows an example (source: https://github.com/PHPOffice/PHPWord):

<?php

require_once 'bootstrap.php';

use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;

Settings::setPdfRendererPath('vendor/dompdf/dompdf');
Settings::setPdfRendererName('DomPDF');

$temp = IOFactory::load('pasta/doc.docx');

$xmlWriter = IOFactory::createWriter($temp , 'PDF');
$xmlWriter->save('pasta/doc.pdf', true);

If you are using PHP7, there is a BUG https://github.com/PHPOffice/PHPWord/issues/732, however in the version dev from the repository the problem has been fixed:

{
    "require": {
       "phpoffice/phpword": "dev-develop"
    }
}

Using repositories/branchs in development can be a risk, I recommend testing well before.

  • A detail by the office package the fidelity is much greater than by the php libraries to see this now

  • Please avoid long discussions in the comments; your talk was moved to the chat

Browser other questions tagged

You are not signed in. Login or sign up in order to post.