ncjoes / office-converter Goto Github PK
View Code? Open in Web Editor NEWPHP Wrapper for LibreOffice
License: MIT License
PHP Wrapper for LibreOffice
License: MIT License
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:
Here the same with high contrast:
So you can clearly see where the image begins and ends as its background starts to have this pattern.
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
"Convertion Failure! Contact Server Admin."
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:
Anyway, my two cents worth.
Thanks and good luck
It does not looks like the specified logPath is used
$logCmd
in makeCommand
is never used
Not working under windows.
Can this work with windows?
Do I need any special setup requirements?
if running via shell, its ok.
but via php code, returning this error.
Hi, I installed LibreOffice on my Linux Centos VM and installed office-converter. But when i try to convert a docx to Pdf I get the "Convertion Failure! Contact Server Admin." error.
Please what are the possible causes of this error
Easiest converter I found for libreoffice, thanks for sharing.
If you do update in future, please consider .txt export option.
Cheers!
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:
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
@ncjoes @liasica @zxp86021 @busgurlu
Hi,
There is an issue while creating ppt to pdf all my content is showing as attached can someone please assist me in this
16704978472022120811104724523_product_file1670580711.pdf
Here is my code
$converter = new OfficeConverter('/var/www/html/storage/app/public/uploads/products/2396.ppt');
try{
$r = $converter->convertTo('16704978472022120811104724523_product_file'.time().'.pdf');
}
catch(exception $e){
}
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}";
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!
We use LibreOffice and our documents are saved to .odt
Can this convert from that?
Thank you
Peter Taylor
Is it support xls to HTML conversion?
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
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!
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
what do you mean ?
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
| Stack trace:
| #0 /home/site/domains/domain.com/public_html/index.php(12): NcJoes\OfficeConverter\OfficeConverter->convertTo('2.html')
| #1 {main}
Another solution guys?
i followed all steps and it keep saying Convertion Failure! Contact Server Admin. W10 ,LibreOffice 6.4 and the last version
How can I pass additional command line options to libreoffice ?
I have facing two issues on converting excel to pdf :
I have tried with below file:
Monthly budget.xlsx
Changing line 31 in OfficeConverter.php
public function __construct($filename, $tempPath = null, $bin = 'libreoffice', $prefixExecWithExportHome = true, $logPath = null)
to
$logPath ='' or $logPath = false
Fixed the problem for me.
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
Can I install it using LibreOffice comporse for a php project?
Hi there
Can I run this solely with OpenOffice? Or is LibreOffice still a dependency even if I go the OpenOffice route?
Your readme file says this wrapper runs with both, but only lists one as a dependency.
Cheers.
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('&','&',variableToReplace );
$templateProcessor = new TemplateProcessor($doc_source_filename);
$templateProcessor->setValue($variableToSearch,$variableToReplace);
I hope that might help!
Is this programm directly converting to PDF or does it make a Step between at DOCX?
Like:
DOCX --> ODT --> PDF?
Or is it directly converting to PDF like:
DOCX --> PDF?
I'm getting the error. Can you help me?
I have a document with paper size of 8.5inx13in. When i convert it to pdf it cropped the document to A4. Is there a work around this?
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;
}
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
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:
Here's a screenshot of my document after posting:
Here's a screenshot of my document after conversion to pdf with OfficeConverter "ncjoes/office-converter" latest version :
Have you ever had this problem? If so, can you help me solve it?
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
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) :
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.