Forum


Replies: 7   Views: 4089
Add blank page if odd page count
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 loktron  · 17-11-2016 - 06:44

I have the following problem:

I create a big number of letters and then merge them into a pdf file for printing. The merged pdf is then to be printed as duplex (both sites of the paper) The problem is, that if one letter has for example 3 pages (odd number in general), the next letter starts on the back side of the letter with an odd page count.

I now tried to getStatistics pagecount for every created letter before merging them and if it has an odd count, create a new document out of it, add a blank page and then save it again.

The problem is, the temp file is not created in the statistics function and i dont know why... I did it like in the example i Think.

I get the following error in my testfunction:


Warning: fopen(C:\inetpub\wwwroot\Dev\pdf\ziel\59.docx_txt582d513f8c431): failed to open stream: No such file or directory in C:\inetpub\wwwroot\Dev\phpdocx\classes\TransformDocAdvLibreOffice.inc on line 42
Unable to open stats file

I use windows server 2012

iis8

libreoffice 5

; libreoffice installation path, absolute path
path = "C:\Program Files(x86)\LibreOffice5"

Or is there maybe an easier way to do what i try to do?

 

Posted by admin  · 17-11-2016 - 10:08

Hello,

Are you able to run the conversion plugin to transform any DOCX? Or the problem you get is only with the statistics module?

Please follow the structions available in the docs folder to use the statistics module. You need to install a macro in the conversion plugin before using it.Also please check that LibreOffice is closed before running it and try using PHP CLI mode.

As alternative approach to accomplish your task (checking the content of a DOCX and know if it has contents), the Indexer class, available since the release of phpdocx 6 can return content information using PHP (without the conversion plugin):

http://www.phpdocx.com/api-documentation/docxutilities/indexer-parse-word-documents-with-PHP

And DOCXPath, available since phpdocx 6.5 includes a new class to return the content of the document:

http://www.phpdocx.com/api-documentation/docx-path/get-text-word-contents-in-docx

You can upgrade your license at anytime on MYPHPDOCX after login.

Regards.

Posted by loktron  · 18-11-2016 - 06:46

Can i use dotm files with phpdocx ? Because there is a start ne section allways on uneven page option in word.
 The problem is that, if i activate this in the template, word will add two useless blank pages if the pagecount is even. if it is odd, it works as desired. So my idea was to write a macro in word that counts the pages on save and, if the count is odd, activates this option.

Would that be possible?

Here my Macro:

Sub oddPageNumber(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim count As Integer
    Dim rest As Integer
    
    count = ActiveDocument.ComputeStatistics(wdStatisticPages)
    rest = count Mod 2
    If rest > 0 Then
    Selection.InsertBreak Type:=wdSectionBreakEvenPage
    End If
End Sub

Posted by admin  · 18-11-2016 - 08:03

Hello,

Phpdocx supports dotm documents and allows adding macros, but the library doesn't run them (to run new macros using the conversion plugin you'd need to change the library adding new code). The macros will run when the document is open using MS Word (or other apps such as LibreOffice).

The available solutions to accomplish what you need are explained in our previous post.

Regards.

Posted by loktron  · 18-11-2016 - 10:00

I did It :)

instead of using a macro i rebuilt the macro with php com functions

the problem i have in general with this is that for example the function for counting pages in vb is:

count = ActiveDocument.ComputeStatistics(wdStatisticPages)

But the PHP function is not:

$count = $word->ActiveDocument->ComputeStatistics('wdStatisticPages');

but:

$count = $word->ActiveDocument->ComputeStatistics(2);

Do you know a page with a good documentation for all these come functions where i can see wich integer stands for wich parameter in the functions?

for the other solutions:

upgrading is not an option at the moment (budget this year is closed) and i did not get LibreOffice to work in invisible mode at all on the server

regards

Posted by admin  · 18-11-2016 - 11:56

Hello,

Sorry but unfortunately there's no good site to get more information about that; all information available is a mess. Sometimes we use this page:

https://msdn.microsoft.com/en-us/library/office/ff837519.aspx

Anyway, maybe we can help you to configure the conversion plugin based on LibreOffice.

After installing LibreOffice and set the values in phpdocxconfig.ini, please try doing a transformation from DOCX to PDF (without macros) using PHP CLI mode. An important point about config is setting:

path = "'C:\Program Files(x86)\LibreOffice5\program'"

this is with ' and adding program to the path. As the path to LibreOffice has spaces your server may need these single quotes.

Regards.

Posted by loktron  · 21-11-2016 - 10:46

I did it now :) i read about word typelib, but the including didnt work so I read out all the constants and defined them myself. I dont wanna use LibreOffice if i dont have to because creating the docs with word, then analyse them with libre and then convert them with word are too many components that can go wrong i think.

So here is my function. I use it on every created document before merging:

public function printDuplex($file){
            $word=new COM("Word.Application") or die("Cannot start MS Word");
            $word->visible = 0 ;
            //6 = wdStory VB (end of document)
            define('endOfPage', 6);
            //(2 = wdStatisticPages VB)
            define('pageCount', 2);
            //open File
            $word->Documents->Open($file);
            // get pagecount
            $count = $word->ActiveDocument->ComputeStatistics(pageCount);
            //If odd pagecount
            if($count%2 > 0){
                //6 = wdStory VB (end of document)
                //go to end of document
                $word->Selection->EndKey(endOfPage);
                //add page
                $word->Selection->InsertBreak();
            }
            //save document
            $word->Documents[1]->SaveAs($file);
            $word->Quit( );
            $word = null;
        }

 

Thank you for your help you sat me on the right track :)

 

regards

Posted by loktron  · 21-11-2016 - 12:34

Maybe the function helps someone else.

you can close this thread now :)