Forum


Replies: 11   Views: 2124
Pdf lossless compression doesn't work?
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by jeremie  · 30-03-2020 - 15:06

I'm trying to use option to do a lossless compression when using transformDocument.

The following code, without lossless, works fine:

$docx->transformDocument('TestTransformDocument.docx', 'TestTransformDocument.pdf', 'libreoffice', ['debug' => true]);

But the following code, with lossless, doesn't work and returns a warning:

$docx->transformDocument('TestTransformDocument.docx', 'TestTransformDocument.pdf', 'libreoffice', ['lossless' => true, 'debug' => true]);

The warning is:

Warning: rename(./TestTransformDocument.pdf,TestTransformDocument.pdf): File not found. (code: 2) in \phpdocx-premium-9.5\classes\TransformDocAdvLibreOffice.php on line 157

Posted by admin  · 30-03-2020 - 15:38

Hello,

Lossless compression works correctly. To use it you need to add and use one of the included macros (Lossless compression), please check the documentation available in the docs folder of the package.

Regards.

Posted by jeremie  · 30-03-2020 - 16:54

You'll have to be more specific. I followed the procedure in your "How to use macros.txt" file.

Here is what my "Module1.xba" file looks like when I copy and paste the macro:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">REM  *****  BASIC  *****

Sub LosslessCompressionPdf(cFile)

  Dim oPropertyValue As new com.sun.star.beans.PropertyValue
  Dim oPropertyData As new com.sun.star.beans.PropertyValue
  Dim oPropertyDataValue As new com.sun.star.beans.PropertyValue

  cURL = convertToURL(cFile)

  oPropertyValue.Name = &quot;Hidden&quot;
  oPropertyValue.Value =  True
  oDoc = StarDesktop.loadComponentFromURL(cURL, &quot;_blank&quot;, 0, Array(oPropertyValue))

  dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
  dispatcher.executeDispatch(oDoc.CurrentController.Frame, &quot;.uno:UpdateAllIndexes&quot;, &quot;&quot;, 0, Array())

  GlobalScope.BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  cFileNew = Tools.Strings.GetFileNameWithoutExtension(cFile) + &quot;.pdf&quot;
  cURL = ConvertToURL(cFileNew)

  oPropertyValue.Name = &quot;FilterName&quot;
  oPropertyValue.Value =  &quot;writer_pdf_Export&quot;

  oPropertyDataValue.Name = &quot;UseLosslessCompression&quot;
  oPropertyDataValue.Value =  1
  oPropertyData.Name = &quot;FilterData&quot;
  oPropertyData.Value = Array(oPropertyDataValue)

  oDoc.storeToURL(cURL, Array(oPropertyValue, oPropertyData))

  oDoc.close(True)

End Sub
</script:module>

It still doesn't work.

Posted by admin  · 30-03-2020 - 18:25

Hello,

Everything looks correct. There are no more steps to add the macro.

Please rename the function macro name from:

Sub LosslessCompressionPdf(cFile)

to

Sub LosslessPdf(cFile)

and try again.

If after doing this change you still can't generate the document, if you send to contact[at]phpdocx.com SSH access and the path where phpdocx is installed we'll check it.

Regards.

Posted by jeremie  · 30-03-2020 - 18:49

It still doesn't work when I rename the macro name.

Unfortunately I can't give you an SSH access to our server.

Can it be that I removed the following instruction from Module1.xba ?

Sub Main

End Sub

 

Posted by admin  · 30-03-2020 - 19:28

Hello,

We never remove the Sub Main method, but it should work without it. Anyway, please restore it.

Maybe you haven't added the macro to the correct Module1? Please note that the macro should be added to the Module1.xba file of the user that is running LibreOffice; each user may have its own .config folder and Module1.xba file.

As we can't connect to your server, please do the following tests, using the command line, please run libreoffice directly:

/opt/libreoffice6.4/program/soffice --invisible "macro:///Standard.Module1.LosslessPdf(/var/www/document/output.docx)"

Changing /opt/libreoffice6.4/program/soffice by the path where LibreOffice is installed. And /var/www/document/output.docx by the absolute path of the DOCX to be transformed. Please run this command in the folder of the DOCX to be transformed. Please note that LosslessPdf is the name of the added macro, do not use LosslessCompressionPdf in Module1.xba but LosslessPdf.

If the previous test works, then please create a simple script to run the code from PHP:

<?php
passthru('/opt/libreoffice6.4/program/soffice --invisible "macro:///Standard.Module1.LosslessPdf(/var/www/document/output.docx)"');

We are doing many tests and in all cases, it is working correctly. The previous command is used by phpdocx to transform the DOCX to PDF using LosslessPdf.

Regards.

Posted by jeremie  · 31-03-2020 - 09:54

Unfortunately it still doesn't work.

File Module1.xba is installed in the right directory:

/var/www/wonder/.config/libreoffice/4/user/basic/Standard/Module1.xba

Our document to transform is called TestTransformDocument.docx. It's located here:

/var/www/wonder/www/Les_tests/TestTransformDocument.docx

We went to directory /var/www/wonder/www/Les_tests/ and we ran the following command line:

/usr/lib/libreoffice/program/soffice --invisible "macro:///Standard.Module1.LosslessPdf(/var/www/wonder/www/Les_tests/TestTransformDocument.docx)"

Nothing happened. No PDF file was created and we didn't get any error message.

 

Here is our full script in Module1.xba where we put back the MAIN command and we changed the script name to LosslessPdf:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">REM  *****  BASIC  *****

Sub Main

End Sub

Sub LosslessPdf(cFile)

  Dim oPropertyValue As new com.sun.star.beans.PropertyValue
  Dim oPropertyData As new com.sun.star.beans.PropertyValue
  Dim oPropertyDataValue As new com.sun.star.beans.PropertyValue

  cURL = convertToURL(cFile)

  oPropertyValue.Name = &quot;Hidden&quot;
  oPropertyValue.Value =  True
  oDoc = StarDesktop.loadComponentFromURL(cURL, &quot;_blank&quot;, 0, Array(oPropertyValue))

  dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
  dispatcher.executeDispatch(oDoc.CurrentController.Frame, &quot;.uno:UpdateAllIndexes&quot;, &quot;&quot;, 0, Array())

  GlobalScope.BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  cFileNew = Tools.Strings.GetFileNameWithoutExtension(cFile) + &quot;.pdf&quot;
  cURL = ConvertToURL(cFileNew)

  oPropertyValue.Name = &quot;FilterName&quot;
  oPropertyValue.Value =  &quot;writer_pdf_Export&quot;

  oPropertyDataValue.Name = &quot;UseLosslessCompression&quot;
  oPropertyDataValue.Value =  1
  oPropertyData.Name = &quot;FilterData&quot;
  oPropertyData.Value = Array(oPropertyDataValue)

  oDoc.storeToURL(cURL, Array(oPropertyValue, oPropertyData))

  oDoc.close(True)

End Sub
</script:module>

 

Posted by admin  · 31-03-2020 - 10:26

Hello,

If no error nor document appears then the macro hasn't been added to the correct .config directory. Please note that /var/www/wonder/.config seems the .config folder of the web user, if you are running it using the command line the correct .config folder should be in the home folder of the user. You can get the HOME folder (in the same way you are testing LibreOffice) using PHP:

<?php
echo getenv("HOME");

or changing to ~ folder.

We have tested the same steps using fresh installations of Debian, Ubuntu, CentOS, RedHat, Windows and macOS with LibreOffice 5 and LibreOffice 6, from PHP 5 to PHP 7.4 and in all cases adding the macro to the correct Module1.xba file, the conversion works correctly. Sorry but we don't know the issue without checking it, we'd need to connect to the server to test it.

Regards.

Posted by jeremie  · 01-04-2020 - 14:09

We found another Module1.xba file in another directory at the following address:

/usr/lib/libreoffice/presets/basic/Standard/Module1.xba

We replaced this other Module1.xba file with the new Module1.xba file. We tried again to make a lossless transform both by calling transformDocument in PHP and by running the same command line from our directory /var/www/wonder/www/Les_tests/ where our TestTransformDocument.docx is located:

/usr/lib/libreoffice/program/soffice --invisible "macro:///Standard.Module1.LosslessPdf(/var/www/wonder/www/Les_tests/TestTransformDocument.docx)"

Again nothing happened. No error message and no file created.

The problem is that we don't have any other Module1.xba file in our server. Could it be another file?

Posted by admin  · 01-04-2020 - 18:07

Hello,

Each user of the OS that runs LibreOffice has its own .config folder with its own Module1.xba. This .config folder is always in the home folder of the user. /usr/lib/libreoffice/presets/basic/Standard/Module1.xba isn't a home folder, and the macro needs to be added to the Module1.xba of the user that is running LibreOffice.

Regards.

Posted by jeremie  · 01-04-2020 - 19:25

I understand. So our home folder for the user is:

/var/www/wonder/.config/libreoffice/4/user/basic/Standard/Module1.xba

It's also the folder returned when we execute getenv("HOME");

But still nothing happens.

What script can we run to show you that we're in the right folder. Maybe a script generating an error?

Posted by admin  · 01-04-2020 - 20:22

Hello,

What version of LibreOffice are you using? Maybe you are using a very old version?

We are sure that the problem comes from not using the correct Module1.xba or an external problem we can't guess. We recommend you to install the latest release of libreoffice (https://www.libreoffice.org/) and try the same macro using libreoffice directly. Running LibreOffice macros from the command line is a common task:

https://superuser.com/questions/1135850/how-do-i-run-a-libreoffice-macro-from-the-command-line-without-the-gui

To be able to use them with PHP, first, they need to work directly with LibreOffice. And also note that PHP may use another home folder than calling the main libreoffice exec from the command line, so first you need to be able to call the macro using the command line:

/opt/libreoffice6.4/program/soffice --invisible "macro:///Standard.Module1.LosslessPdf(/var/www/document/output.docx)"

Using --backtrace you can debug the output of LibreOffice: https://ask.libreoffice.org/en/question/40490/enable-debug-or-log-errors-using-libreoffice-for-linux/

Regards.