Forum


Replies: 4   Views: 2442
Replacevariablebywordfragment and mergedocx errors
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 jim.alexander@fundingbenchmarks.org  · 01-12-2017 - 13:40

Hi,

I have a two .docx files that I want to merge. One file is simple docx, and one file has been previously created from a template and contains only a single barchart.

Both .docx files are readable and the bar chart appears as expected in the .docx.

I use this to insert the barchart into the file.

    $chart = new WordFragment($docx, 'document');
    $chart->addChart($paramsChart);
    $docx->replaceVariableByWordFragment(array('Ranking_1' => $chart), array('type' => 'inline'));
$docx->createDocx($OutputFile_1);

when I try to merge this file ( $merge = new MultiMerge();) I always get the error:

"Fatal error: Call to a member function setAttribute() on a non-object in C:\wamp\www\fb\public_html\phpdocx\classes\MultiMerge.inc on line 1713"

Help?

Thanks.

Jim.

 

Posted by admin  · 01-12-2017 - 15:40

Hello,

That error (in that specific line) should appear only if there's no valid wp:docPr tag, but that should never happen. Please send both DOCX to contact[at]phpdocx.com so we can do some tests.

Regards.

Posted by admin  · 01-12-2017 - 19:27

Hello,

We have done some tests with your DOCX and we don't get any error. We have tested it with PHP 5.2 to PHP 7.1 on Linux, Windows and macOS with phpdocx 7.5 (the version you are using).

This is the script we have run using your templates:

<?php

require_once 'classes/MultiMerge.inc';

$docx = new CreateDocxFromTemplate('FBTemplate_Cat.docx');
$chart = new WordFragment($docx, "document");
$data = array(
    'legend' => array('Legend 1', 'Legend 2', 'Legend 3'),
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10, 20, 5),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20, 60, 3),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50, 33, 7),
        ),
    ),
);
$paramsChart = array(
    'data' => $data,
    'type' => 'barChart',
    'color' => 5,
    'sizeX' => 15,
    'sizeY' => 10,
    'chartAlign' => 'center',
    'legendPos' => 'none',
    'legendOverlay' => '0',
    'border' => '1',
    'hgrid' => '1',
    'vgrid' => '0',
    'showTable' => '1'
);
$chart->addChart($paramsChart);

$docx->replaceVariableByWordFragment(array('Ranking_1' => $chart), array('type' => 'inline'));

$docx->createDocx('output_3');

$merge = new MultiMerge();
$merge->mergeDocx('output_3.docx', array('Test_Front_Page.docx'), 'output_2.docx', array());

Please compare it with the one you are running. This script replaces the placeholder with a chart, then generates a DOCX and finally merges the documents; it doesn't return any error. Please check if your script read access to the DOCX and that both files exist before merging them.

Regards,

Posted by jim.alexander@fundingbenchmarks.org  · 01-12-2017 - 23:05

Hi,

Thanks for the quick response.

I agree the version you sent works, however, if you reverse the merge file order, like this, 

$merge->mergeDocx('Test_Front_Page.docx', array('output_3.docx'), 'output_2.docx', array());

You get the error.

( The "Test_Front_Page.docx" is the cover page i.e. 1st page)

Regards

Posted by admin  · 02-12-2017 - 09:12

Hello,

Yes, you are right, we didn't test that specific case and it returns an error. The problem comes from the textbox; complex replacements such as replacing a placeholder by a chart in a textbox may require using DOCXPath that is more flexible.

You can use two approaches to fix your issue.

1. Use DOCXPath (it's the easiest solution): instead of the replaceVariableByWordFragment method, you need to use the replaceWordContent one:

<?php

require_once 'classes/MultiMerge.inc';

$docx = new CreateDocxFromTemplate('FBTemplate_Cat.docx');
$chart = new WordFragment($docx, "document");
$data = array(
    'legend' => array('Legend 1', 'Legend 2', 'Legend 3'),
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10, 20, 5),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20, 60, 3),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50, 33, 7),
        ),
    ),
);
$paramsChart = array(
    'data' => $data,
    'type' => 'barChart',
    'color' => 5,
    'sizeX' => 15,
    'sizeY' => 10,
    'chartAlign' => 'center',
    'legendPos' => 'none',
    'legendOverlay' => '0',
    'border' => '1',
    'hgrid' => '1',
    'vgrid' => '0',
    'showTable' => '1'
);
$chart->addChart($paramsChart);

$referenceNode = array(
    'type' => 'paragraph',
    'contains' => 'Ranking_1',
);

$docx->replaceWordContent($chart, $referenceNode);

$docx->createDocx('output_3');

$merge = new MultiMerge();
$merge->mergeDocx('Test_Front_Page.docx', array('output_3.docx'), 'output_2.docx', array());

2. Use the replaceVariableByWordFragment method but first, you merge the DOCX documents and then you replace the placeholder.

Using any of these approaches will generate the correct output.

Regards.

 

 

Regards.