2

I am using ampps as a windows 10 apache server, my php version is 7.3. I downloaded it from the LibreOffice download page and installed it on my computer. Then I installed this library via composer https://github.com/ncjoes/office-converter. I try as in the example given, but it does not convert and gives an error. I would be very grateful if you could help me where I am wrong. Here is my code sample and the error I encountered:

<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';

use NcJoes\OfficeConverter\OfficeConverter;
use PHPUnit\Framework\TestCase;

class OfficeConverterTest extends TestCase
{
    /**
     * @var OfficeConverter $converter
     */
    private $converter;
    private $outDir;

    public function setUp()
    {
        parent::setUp();

        $DS = DIRECTORY_SEPARATOR;
        $file = __DIR__."{$DS}sources{$DS}test.docx";
        $this->outDir = __DIR__."{$DS}results";

        $this->converter = new OfficeConverter($file, $this->outDir);
    }

    public function testDocxToPdfConversion()
    {
        $output = $this->converter->convertTo('result.pdf');

        $this->assertFileExists($output);
    }

    public function testDocxToHtmlConversion()
    {
        $output = $this->converter->convertTo('result.html');

        $this->assertFileExists($output);
    }
}

$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
?>

Fatal error: Uncaught Error: Call to a member function convertTo() on null in C:\Program Files\Ampps\www\converter\converter.php:29 Stack trace: #0 C:\Program Files\Ampps\www\converter\converter.php(43): OfficeConverterTest->testDocxToPdfConversion() #1 {main} thrown in C:\Program Files\Ampps\www\converter\converter.php on line 29

1 Answers1

1

When running tests, we are supposed to leave running them to phpunit, but you are trying to call manually (in last 2 lines of your code).

But if you insist on calling manually, change:

$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();

Into:

$donustur = new OfficeConverterTest();
$donustur->setUp();
$donustur->testDocxToPdfConversion();

So that you call setUp() which phpunit would normally call for you automatically.

See also: How do I run all my PHPUnit tests?
How to run single test method with phpunit?

Example for page

If you want to use this logic on a Web-Page, it should look something like:

<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';

use NcJoes\OfficeConverter\OfficeConverter;

echo 'Converting...<br>';

$input = __DIR__ . '/test.docx';
$converter = new OfficeConverter($input, __DIR__.'/results');
$output = $converter->convertTo('result.pdf');

echo 'Saved at:' . $output . '<br>';
Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • //$donustur= new OfficeConverterTest(); //$donustur->testDocxToPdfConversion(); I commented out the codes, but when I refresh the page, I see a blank page. When I check the results folder, it's empty, so it didn't convert. – Huseyin Telli Sep 20 '21 at 16:44
  • @HuseyinTelli That code is for testing with PHPUnit, not for putting on page (and always converts the same `test.docx` file), copy the logic that you need only (see my example). – Top-Master Sep 20 '21 at 18:55
  • Thank you very much for the example. However, I am getting an error. So, if you have knowledge, can you help me with the error I got? – Huseyin Telli Sep 20 '21 at 19:38
  • Fatal error: Uncaught NcJoes\OfficeConverter\OfficeConverterException: Convertion Failure! Contact Server Admin. in C:\Program Files\Ampps\www\converter\vendor\ncjoes\office-converter\src\OfficeConverter\OfficeConverter.php:54 Stack trace: #0 C:\Program Files\Ampps\www\converter\converter.php(11): NcJoes\OfficeConverter\OfficeConverter->convertTo('result.pdf') #1 {main} thrown in C:\Program Files\Ampps\www\converter\vendor\ncjoes\office-converter\src\OfficeConverter\OfficeConverter.php on line 54 – Huseyin Telli Sep 20 '21 at 19:39
  • @HuseyinTelli Ensure both the input file and destination directory exist, if the error still happens, you will need to read into it's codes as "Contact server Admin" is not much info. – Top-Master Sep 20 '21 at 20:58
  • I took a look at the codes and compiled them one by one. A minor mistake was overlooked, probably because this library was written in an older version of libreoffice. I fixed the errors and now it works... Thanks for your help. – Huseyin Telli Sep 20 '21 at 21:11