PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.29k stars 2.7k forks source link

ziparchive error when phpword templateprocessor called #657

Open guiramadasa opened 9 years ago

guiramadasa commented 9 years ago

Hi,

I am trying to replace some content in a word file, using following code.

<?php      
 require_once 'includes/PhpWord/Autoloader.php';
 \PhpOffice\PhpWord\Autoloader::register();

if($ext == 'docx')
     $phpWord = \PhpOffice\PhpWord\IOFactory::load($target_file);
else if($ext == 'doc')
    $phpWord = \PhpOffice\PhpWord\IOFactory::load($target_file, 'MsDoc');     

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($target_file);
?>

But it gives following errrors.

Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object in /home2/xxx/public_html/xxx/FL/xxx/sc/includes/PhpWord/Shared/ZipArchive.php on line 198

Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object in /home2/xxx/public_html/xxx/FL/xxx/sc/includes/PhpWord/Shared/ZipArchive.php on line 201

The file path is correct and also the solution needs to support both .doc and .docx formats.

It will be a great help if you can support me on this.

Thank you.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/28067334-ziparchive-error-when-phpword-templateprocessor-called?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
guiramadasa commented 9 years ago

When checked files with is_readable(), it is readable, but get above error.

ozilion commented 9 years ago

As I know you can only use .docx format for temaplate files.

philippfrenzel commented 8 years ago

Hi, did you find a solution, I try to read the file from an file abstraction layer which returns sadly doesn't work at all, now I tried to create a temporary file for processing, but still doesn't work:( Thanks

erpwebadmin2 commented 4 years ago

Has anyone made .DOC file work on templateprocessor?

arifulhb commented 3 years ago

Still didn't find a solution for .doc format.

PoojaJadav commented 2 years ago

I'm facing the same issue. I have a DOC file. It is readable as well. I got Invalid or uninitialized Zip object error.

I tried to loading format change as well with MsDoc. but still not woking. Can you please support to here :) Thanks!!!

MolbioUnige commented 2 years ago

This happended to me, the problem was due to the line endings in the doc file. My server runs on linux, my doc file was saved with LF line ending, but pulling the repo changed the line endings to CRLF. I solved it by specifying in .gitattributes that the doc(x) files where to be considered as binary.

*.doc binary
*.docx binary

The problem disappeared.

You can check the line endings of your file using

cat -e yourfile

If lines end with M$ you have CRLF line endings.