GithubHelp home page GithubHelp logo

office-converter's Introduction

Office Converter

PHP Wrapper for LibreOffice

Convert offices files to PDF and HTML using LibreOffice or OpenOffice. Supported conversion formats include:

  • pptx => pdf
  • ppt => pdf
  • pdf => pdf
  • docx => pdf, odt, html
  • doc => pdf, odt, html
  • xlsx => pdf
  • xls => pdf
  • png => pdf
  • jpg => pdf
  • jpeg => pdf

Installation

It is recommended to install OfficeConverter through Composer.

Run this command within your project directory

composer require ncjoes/office-converter

Dependencies

In order to use OfficeConverter, you need to install LibreOffice.

Usage

Here are some samples.

<?php
// if you are using composer, just use this
use NcJoes\OfficeConverter\OfficeConverter;

$converter = new OfficeConverter('test-file.docx');
$converter->convertTo('output-file.pdf'); //generates pdf file in same directory as test-file.docx
$converter->convertTo('output-file.html'); //generates html file in same directory as test-file.docx

//to specify output directory, specify it as the second argument to the constructor
$converter = new OfficeConverter('test-file.docx', 'path-to-outdir');
?>

License

The OfficeConverter package is open-sourced software licensed under the MIT license.

Feedback & Contribute

Notify me of any issues, bugs, or improvements. Thanks 👍

office-converter's People

Contributors

arieffikhrie avatar busgurlu avatar caiosousaupp avatar ciki avatar ivanwitzke avatar liasica avatar mdeprezzo avatar nasirbest avatar ncjoes avatar neopheus avatar nooruliman avatar sunnyworm avatar technicallycyber avatar tetreum avatar zxp86021 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

office-converter's Issues

Convertion Failure! Contact Server Admin

Hello, Im using laravel 8. I already install libre office 7.0.5 in centos 7.
I also already update
public function __construct($filename, $tempPath = null, $bin = ' ', $prefixExecWithExportHome = true)
{
if ($this->open($filename)) {
$this->setup($tempPath, $bin, $prefixExecWithExportHome);
}
}

and
protected function makeCommand($outputDirectory, $outputExtension)
{
$oriFile = escapeshellarg($this->file);
$outputDirectory = escapeshellarg($outputDirectory);

    if (pathinfo($this->file,PATHINFO_EXTENSION) == 'docx' || pathinfo($this->file,PATHINFO_EXTENSION) == 'doc' || pathinfo($this->file,PATH_EXTENSION) == 'DOCX' 
    || pathinfo($this->file,PATHINFO_EXTENSION) == 'DOC') { return "soffice --headless --convert-to {$outputExtension} {$oriFile} --outdir {$outputDirectory}";}
    else if(pathinfo($this->file,PATHINFO_EXTENSION) == 'xlsx' || pathinfo($this->file,PATHINFO_EXTENSION) == 'xls' || pathinfo($this->file,PATHINFO_EXTENSION) == 'XLSX'
    || pathinfo($this->file,PATHINFO_EXTENSION) == 'XLS'){ 
       return "scalc --headless --convert-to {$outputExtension} {$oriFile} --outdir {$outputDirectory}";
    }
}

but it still return the Convertion failure. Contact Admin

Imagequality bad & not lossless

I now convert .docx documents to PDF and noticed a huge drop in Imagequality.
When I insert a PNG into the document it converts it to JPEG which decrese imagequality and also makes it lose its transparency.

Is there a option to convert images in the same format, or even lossless?
The Image I inserted is just 20Kb in the source so this would not matter, but let me show you what is happening by converting from DOCX to PDF:

PDF (screenshot)
phpWord (original)

Here the same with high contrast:
phpWord (modified)

Even higher contrast:
phpWord (modified 2)

So you can clearly see where the image begins and ends as its background starts to have this pattern.
phpWord (modified 2) - Kopie

But the original Image does not havw this and is a 100% transparent PNG with NO Background..
Does someone know how to force the office converter to not convert from PNG to JPEG and to not drop quality?

Best regards

QUESTION: Possible to add .txt

Easiest converter I found for libreoffice, thanks for sharing.

If you do update in future, please consider .txt export option.

Cheers!

Convertion Failure! Contact Server Admin. LibreOffice 7.2.2 Linux

Tried to convert doc to pdf but it says "Convertion Failure! Contact Server Admin." I used same codes as in documentation

use NcJoes\OfficeConverter\OfficeConverter;

$converter = new OfficeConverter('test-file.docx');
$converter->convertTo('output-file.pdf'); 

Im using Laravel 8, in a server

Feedback - Bugs, Issues, Method

Hey there

I appreciate all the work gone into this api wrapper, but honestly, given all the bugs and troubleshooting I've had to do, using a simple exec() command directly from php without this wrapper is way easier.

So unfortunately this api feels a little like using a horse to pull a car.

Would be great if you could simplify the backend so it does all the work of selecting the output formats, filters, validates inputs, and other options that come with libreoffice, and also to make it bulletproof so a) updates in php, and b) error types and troubleshooting are taken care by your api.

For example, my two biggest problems were:

  1. that my libreOffice location in windows had a space because in windows, programs are stored in "Program Files".. Adding double quotes around the location path messed up everything because they weren't escaped. But I only figured this out using an exec() command directly and passing back the results.
  2. PHP deprecated the use of null in realpath. Not sure why you are using realpath, because that also converts full paths to empty/null. You could simply check the inputs are absolute and if not, automatically find the fullpath prefix and add it using DIR or FILE

Anyway, my two cents worth.

Thanks and good luck

Not working for Hindi Language

when I am converting ppt to pdf using this library .In ppt there is hindi words included that is not converted in pdf display box .Please do the needful as soon as possible

Error throwing: File does not exists but It exists on AWS S3 bucket

Hi There,

I am implementing Office-converter to convert PPT to PDF but I am getting error like

message: File does not exist --http://s3-eu-west-2.amazonaws.com/SomeDirName/SomeFile.ppt
I checked file is exists but showing above error message.

I used code like :
$converter = new OfficeConverter($request->s3_url);
$converter->convertTo('output_file.pdf'); //generates pdf file in same directory as test-file.docx

Please help me on this

Thank you!

Dependency Install

to install via cpanel/whm there is a plethora of libreoffice packages which is the essential package

image
image

Not working on PHP 8.x

I successfully run this on PHP 7.4 but as soon as I switch over to PHP 8.0.5 I get this error:

AH01071: Got error 'PHP message: PHP Warning: rename(/var/www/vhosts/DOMAIN.TLD/httpdocs/SUB/archiv/tmp/K\xc3\x83\xc2\xbcndigung.pdf,/var/www/vhosts/DOMAIN.TLD/httpdocs/SUB/archiv/tmp/../../archiv/tmp/K\xc3\x83\xc2\xbcndigung.pdf): No such file or directory in /var/www/vhosts/DOMAIN.TLD/httpdocs/ph/vendor/ncjoes/office-converter/src/OfficeConverter/OfficeConverter.php on line 144', referer: https://SUB.DOMAIN.TLD/

When I run the very same code on PHP 7.4 everything works fine. So my question is:
is this problem related to my application only and Office Converter is indeed PHP 8 compatible or not?

Notice:

  • "K\xc3\x83\xc2\xbcndigung" stands for "Kündigung" is this maybe related to UTF-8 encoding?

EDIT:

When I remove the "ü" from all texts and names then it works even with PHP 8, but as soon as "Umlaute" are in the filenames it breaks functionality on PHP 8

Error converter

Fatal error: Uncaught exception 'NcJoes\OfficeConverter\OfficeConverterException' with message 'Convertion Failure! Contact Server Admin.' in /home/site/domains/domain.com/public_html/vendor/ncjoes/office-converter/src/OfficeConverter/OfficeConverter.php:54

  | Stack trace:
  | #0 /home/site/domains/domain.com/public_html/index.php(12): NcJoes\OfficeConverter\OfficeConverter->convertTo('2.html')
  | #1 {main}

Problem when converting my DOCX file to PDF

Hello,

I have a font problem when converting my DOCX file to PDF.

To make my project, I started from dunglas/symfony-docker " https://github.com/dunglas/symfony-docker " which combines Symfony 6.3 with Docker, running on a Caddy server. My php version is 8.2-fpm-alpine.

Here's my problem:

I'm publishing a docx document with PHPWORD "phpoffice/phpword" in the latest version.

Here's a screenshot of my document before posting:

befor-publipost

Here's a screenshot of my document after posting:

after-publipost

Here's a screenshot of my document after conversion to pdf with OfficeConverter "ncjoes/office-converter" latest version :

after-publipost-pdf

Have you ever had this problem? If so, can you help me solve it?

Undefined array key "HOME"

Hi

i got this on my test environment - a VPS:

Undefined array key "HOME"
/vendor/ncjoes/office-converter/src/OfficeConverter/OfficeConverter.php line 147

This is the code line in question:

$userInstallationDirectoryOption = "-env:UserInstallation=file://{$_SERVER['HOME']}/.config/libreoffice-profile{$randomNumber}";

Bug in SetValue() when replace contains "&"

When usingt setValue() for replacing a variable ${variable} containing "&" the function does not respond with any error but the document will be saved corrupted and therefore you will not be able to use or open it.

Our Solution was to replace the "&" with the xml code for that character, so'&' and then it will be correctly replaced.


$variableToSearch = 'some_variable';
$variableToReplace = 'some_variable_with_&';

//Fix & error
$variableToReplace = str_replace('&','&#38;',variableToReplace );

$templateProcessor = new TemplateProcessor($doc_source_filename);
$templateProcessor->setValue($variableToSearch,$variableToReplace);

I hope that might help!

Works on Homestead/Vagrant but error on RedHat/Centos/Apache-Permissions Issue

On my dev environment, which is Laravel homestead/Vagrant, it works fine.
However on my production env , which is a redhat7/apache2.4.6 , it does not.
I have installed Libreoffice v6.3.3.2 on both envs.

Before installing your package, I was calling “libreoffice command ” from Laravel through an exec call:

exec("libreoffice --headless --convert-to pdf --outdir '".$tmpdir."' '".$tmpdir.$filename."'");

If $tmpdir has a very large path, I get a “ file not found error” message, even if the “libreoffice --headless --convert-to pdf” command is launched directly in the terminal, but if the path is relatively small it works and converts the file , but only if run in the terminal.
If I try to run the exec function with the libreoffice command passing a $tmpdir variable with a small path inside my Laravel controller, the word document is generated on my Dev environment but not in my Production environment, the file is not converted, apparently due to permissions issues , the “apache user” does not have permissions to run the libreoffice command. I searched how to allow “apache user” to execute external scripts using php “exec” function but it seems it’s required to give root permissions to apache user which is not recommended at all for security reason obviously.

It was then when I decide to look for more options, and found your package, which I installed in both my dev and prod env, and the code goes :

 use NcJoes\OfficeConverter\OfficeConverter;

     $outputdir=$this->storage_documents->path($this->file_path."/");
	$filename_parts = explode('.', $filename);
    $filename_parts[1] = 'pdf';
    $pdf_filename= implode('.', $filename_parts);

    $converter = new OfficeConverter($outputdir.$filename);
    $converter->convertTo($pdf_filename);

On my local dev env it works perfectly ( homestead/vagrant on a mac) , but in production, I get the following error message :

rename(/var/www/html/jep_indi_test/storage/app/documentos_expedientes/Sala de Definición de Situaciones Jurídicas/Despacho 001/1569335404-141/SUBSDSJ-SOME-SusAsu-PEDR-563-2019/secretaria judicial_oficio-o
ficio-Orfeo.pdf,/var/www/html/jep_indi_test/storage/app/documentos_expedientes/Sala de Definición de Situaciones Jurídicas/Despacho 001/1569335404-141/SUBSDSJ-SOME-SusAsu-PEDR-563-2019/secretaria judicial_oficio-oficio-Orfeo.pdf): No such file or directory

It seems the PDF was never created , maybe due to permissions or any other reason!

I would appreciate you could give any advice or hint with my case.

thx

Conversion from docx to pdf not working on v.1.0.0

Hi..
i'm using office-converter v.1.0.0 installed from composer.
I installed Libre Office 6.2.5.2 x64 on win 10.
Installed ncjoes/office-converter via composer.
Add this code to my php page (for testing) :

convertTo('output-file.pdf'); ?>

and nothing happend. Code seems to stop on "$converter->convertTo('output-file.pdf'); " because all my codes after that not working (not shown on my web page). I tried convert from cmd and it works.
Can U please help. Thanks.

Convertion Failure! Contact Server Admin.

Here is my code:

// this way is using LibreOffice
    $converter = new OfficeConverter($filename);
    $nameFile = pathinfo($filename, PATHINFO_FILENAME) . ".html";
    $content = "";
    try {
        $converter->convertTo($nameFile); //generates pdf file in same directory as test-file.docx
        $content = file_get_contents(dirname($filename)."\\".$nameFile);
        return $content;

    } catch (\NcJoes\OfficeConverter\OfficeConverterException $e) {
        return $e->getMessage();
    }

And when I run, It return message: Convertion Failure! Contact Server Admin.
I am using Yii2 PHP Framework to convert .doc to .html then to plain text. Please help me!

works in linux mint

i tried this code ...but i have some error i don't know if is for mi server lampp add my code and error

convertTo('Documento2.pdf'); // $result->saveFiles('Documento2.pdf'); header('Content-Type: application/pdf'); header("Content-Disposition: attachment; filename=".$id_fol."_".$newNom.".pdf; charset=iso-8859-1"); echo file_get_contents('Documento2.pdf'); ?>

Fatal error: Uncaught NcJoes\OfficeConverter\OfficeConverterException: Convertion Failure! Contact Server Admin. in /opt/lampp/htdocs/sefaureo2.5/vendor/ncjoes/office-converter/src/OfficeConverter/OfficeConverter.php:54 Stack trace: #0 /opt/lampp/htdocs/sefaureo2.5/sources/ajax/generar_Word.php(301): NcJoes\OfficeConverter\OfficeConverter->convertTo('Documento2.pdf') #1 {main} thrown in /opt/lampp/htdocs/sefaureo2.5/vendor/ncjoes/office-converter/src/OfficeConverter/OfficeConverter.php on line 54

ConvertTo function is not working properly. No return

Hi, Issue with using the tool as the error is with this part of the code as the command is not made properly and its not returning.

 public function convertTo($filename)
    {
        $outputExtension = pathinfo($filename, PATHINFO_EXTENSION);
        $supportedExtensions = $this->getAllowedConverter($this->extension);

        if (!in_array($outputExtension, $supportedExtensions)) {
            throw new OfficeConverterException("Output extension({$outputExtension}) not supported for input file({$this->basename})");
        }

        $outdir = $this->tempPath;
        $shell = $this->exec($this->makeCommand($outdir, $outputExtension));
        if (0 != $shell['return']) {
            throw new OfficeConverterException('Convertion Failure! Contact Server Admin.');
        }

        return $this->prepOutput($outdir, $filename, $outputExtension);
    }

The error:

Fatal error: Uncaught NcJoes\OfficeConverter\OfficeConverterException: Convertion Failure! Contact Server Admin. in C:\xampp\htdocs\vendor\ncjoes\office-converter\src\OfficeConverter\OfficeConverter.php:54
Stack trace:
#0 C:\xampp\htdocs\a\create_thumbnail.php(136): NcJoes\OfficeConverter\OfficeConverter->convertTo('Architecture_xy...')
#1 C:\xampp\htdocs\a\create_thumbnail.php(181): getThumbnail('../uploads/ppt/...', 'Architecture_xy...', 'C:/xampp/htdocs...', '../uploads/tmp/', 'ppt', '146', Object(mysqli))
#2 {main}
  thrown in C:\xampp\htdocs\vendor\ncjoes\office-converter\src\OfficeConverter\OfficeConverter.php on line 54

Only Fail convert images and XLS, DOCS WORK (Convertion Failure! Contact Server Admin.)

Hi people,

I am getting this error when I select an image or excel, the rest works like DOCS.
I attach the code that I use.
ERROR:

exception: "NcJoes\\OfficeConverter\\OfficeConverterException"
file: "/Users/alejandrocastronantes/Documents/Trabajo/gestor-documental/vendor/ncjoes/office-converter/src/OfficeConverter/OfficeConverter.php"
line: 54
message: "Convertion Failure! Contact Server Admin."

ME CODE:

 use NcJoes\OfficeConverter\OfficeConverter;
 
  function convertToPdf($pathDocument, $newNameDocument)
     {
         $newNameFile = str_replace(' ', '_', $newNameDocument);
         $converter = new OfficeConverter($pathDocument);
       $converter->convertTo($newNameFile);
       return $newNameFile;
     }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.