Phpword: docx extension word document content getting unformatted after adding new section and saving the document.

Created on 28 Aug 2017  路  4Comments  路  Source: PHPOffice/PHPWord

Hello,

I have installed phpword script and started using it. There are two issues which I am facing:-

1.) When I add some new content after reading a file, and save the document it loses it's originality. But the content gets added with unformatted data.

2.) The content I am adding I want to append it in already existing document rather everytime creating new file each time content getting added.

Please help me with this situation. Would really appreciate.

Warm Regards,
Akash Rai


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

All 4 comments

You need to give it formatting information. See the examples.

and with 2. you probably are doing:

        use PhpOffice\PhpWord\PhpWord;
    $phpWord = new PhpWord();
    $template = $phpWord->loadTemplate(storage_path('greetings.docx'));
    $section = $phpWord->addSection();
        $section->addText('Hello world!');
        $phpWord->save(storage_path('app/public/x.docx'));

instead of the correct (some commands are from Laravel):

use PhpOffice\PhpWord\Reader\Word2007;
$phpWord = new Word2007();
$template = $phpWord->load(storage_path('greetings.docx'));
$section = $template->addSection();
$myStyle = $template->getSections()[0]->getelements()[0]->getFontStyle();
$section->addText('Hello world2!', $myStyle);
$template->save(storage_path('app/public/x.docx'));
return response()->download(storage_path('app/public/x.docx'))->deleteFileAfterSend(true);

(which still adds "Hello world!" and steals styling, which you can get from the previous section by using the get methods, or, if you want the raw data, die-and-dump all the structure with dd($template), or any other PHP method to show the content of a complex variable)

@akashraiesoft Have you solved your problem? If so, close the issue, if not, elaborate on the problem.

@troosan any pointers on how to fix the styling issues ?
I would like to start looking into the code to fix / support style loading.... ?

You need to give it formatting information. See the examples.

and with 2. you probably are doing:

        use PhpOffice\PhpWord\PhpWord;
  $phpWord = new PhpWord();
  $template = $phpWord->loadTemplate(storage_path('greetings.docx'));
  $section = $phpWord->addSection();
        $section->addText('Hello world!');
        $phpWord->save(storage_path('app/public/x.docx'));

instead of the correct (some commands are from Laravel):

use PhpOffice\PhpWord\Reader\Word2007;
$phpWord = new Word2007();
$template = $phpWord->load(storage_path('greetings.docx'));
$section = $template->addSection();
$myStyle = $template->getSections()[0]->getelements()[0]->getFontStyle();
$section->addText('Hello world2!', $myStyle);
$template->save(storage_path('app/public/x.docx'));
return response()->download(storage_path('app/public/x.docx'))->deleteFileAfterSend(true);

(which still adds "Hello world!" and steals styling, which you can get from the previous section by using the get methods, or, if you want the raw data, die-and-dump all the structure with dd($template), or any other PHP method to show the content of a complex variable)

Not working.

Was this page helpful?
0 / 5 - 0 ratings