php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73605 COM Word.application SaveAs Type conflict
Submitted: 2016-11-25 09:22 UTC Modified: 2016-11-26 05:30 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:3 (100.0%)
From: tt at prof4 dot net Assigned:
Status: Verified Package: COM related
PHP Version: 7.0.13 OS: Windows 10 Pro Apache 2.4
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-11-25 09:22 UTC] tt at prof4 dot net
Description:
------------
I am not sure if this is relating to the topic of "PDF functions". We tested the following script with PHP5.4 and PHP7. It works perfectly with PHP5.4 but generates an error in PHP7 (Same configuration for the environment but the Port for Apache24 is different to the port for Apache22 with PHP5.4)

We also tested it with the SaveAs2 function and also we checked the parameters for "Open()".

We are using Word Version 14.

Test script:
---------------
function rtf2pdf($path) {
    if (!class_exists('COM')) {
		return false;
	}
	$word = new COM("word.application");
    if (!$word) {
		return false;
	}
    if ($word->Version < 12) {
		$word->Quit();
		return false;
	}    
    $word->Documents->Open($path, false, true);    
    $wdSaveFormats = array(
		'docx' => 16,
		'html' => 10,
		'rtf' => 6,
		'txt' => 2,
		'doc' => 0,
		'pdf' => 17
	);
	$new_file = str_replace('.rtf', '.pdf', $path);
	$word->ActiveDocument->SaveAs($new_file, $wdSaveFormats['pdf']);
	$word->ActiveDocument->SaveAs2($new_file, $wdSaveFormats['pdf']);
	$word->ActiveDocument->Close(false);
	$word->Quit();
	return $new_file;
}

echo rtf2pdf('C:\\mydoc.rtf');

Expected result:
----------------
C:\mydoc.pdf

Actual result:
--------------
Fatal error: Uncaught com_exception: Parameter 0: Typenkonflikt. in rtftest.php:24 Stack trace: #0 rtftest.php(24): variant->SaveAs('C:\\mydoc.pdf', 17) #1 rtftest.php(31): rtf2pdf('C:\\mydoc.rtf') #2 {main} thrown in rtftest.php on line 24


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-25 09:44 UTC] tt at prof4 dot net
-Package: *PDF functions +Package: COM related
 [2016-11-25 09:44 UTC] tt at prof4 dot net
moved from PDF functions to COM related
 [2016-11-25 14:05 UTC] ab@php.net
-Status: Open +Status: Feedback
 [2016-11-25 14:05 UTC] ab@php.net
Does it make a difference, when another word document is used? Or another Office version? Please supply a test file in that case.

Thanks.
 [2016-11-25 14:16 UTC] tt at prof4 dot net
-Status: Feedback +Status: Open
 [2016-11-25 14:16 UTC] tt at prof4 dot net
Its quite hard to test with another Word-Version ...
It doesnt matter which file you use. You can test it with every *.doc, *.rtf, it will not be converted to another format.

Every time the same message.
 [2016-11-25 17:39 UTC] ab@php.net
-Status: Open +Status: Verified
 [2016-11-25 17:39 UTC] ab@php.net
I come to the point now. Your PHP build is obviously 64-bit. In that case, and it is new in PHP7, when the actual arguments are passed to COM, they're converted to default variants. In PHP5, an integer in PHP is always VT_I4, whereby in PHP7 it depends on the exact build. A 64-bit build of PHP 7 will send the PHP integer type as VT_I8 to the com object, a 32-bit build of PHP 7 will handle it still as VT_I4.

So this is a subtle breach, yeah, as the SaveAs method expects an enumerator value which is the exact 32-bit integer type. What you can do in this case - don't use a PHP variable directly, but create a variant, eg

'pdf' => new VARIANT(17, VT_I4),

This is compatible disregarding the bitness, so will work with any PHP build. AFM, it it is not a bug, at most a doc issue.

Thanks.
 [2016-11-26 05:30 UTC] tt at prof4 dot net
-Type: Bug +Type: Documentation Problem
 [2016-11-26 05:30 UTC] tt at prof4 dot net
Thanks for the information. It works!
Hopefully it will help some1 sometimes.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 14:01:30 2024 UTC