Possibility to view doc, docx in browser

1

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

    
asked by anonymous 07.01.2017 / 15:10

1 answer

5

If the case is just rendering

Practice solution, 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) popular has already natively embedded PDF readers:

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

Mobile browsers apparently already have internal built-in APIs for PDF rendering

  • Workarounds (removed from SOen ):

    Office 365:

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

    Use google docs:

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

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

Using server-side conversion

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

  • Not all conversions will be seamless 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.

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

  • 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 using the terminal:

$ /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 response link , PHPOffice offers such support

To install add to require: of your composer.json (you must have dompdf also to write the PDF):

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

And then run in terminal or cmd:

cd c:\wamp\www\projeto
composer update

To convert a Word to PDF you only need to import to libraries (you will need to use composer ), follow an example (source: link ):

<?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 link , however in the dev from the repository the problem has already been fixed:

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

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

    
08.01.2017 / 14:41