Create, read, write Word and PDF in PHP
To use GemBox.Document in PHP, you'll need to:
- Download and install GemBox.Document Setup.
- Expose GemBox.Document to COM Interop with Regasm.exe tool:
:: Add GemBox.Document to COM registry for x86 (32-bit) applications. C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe [path to installed GemBox.Document.dll] :: Add GemBox.Document to COM registry for x64 (64-bit) applications. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe [path to installed GemBox.Document.dll]
Working with Word files in PHP
The following example shows how you can read a template Word file from PHP, edit its content (with Find and Replace, Mail Merge and Modify Bookmarks operations) and write it as an output file of PDF format.
<?php // Create ComHelper object. $comHelper = new Com("GemBox.Document.ComHelper", null, CP_UTF8); // If using Professional version, put your serial key below. $comHelper->ComSetLicense("FREE-LIMITED-KEY"); // Read Word document. $document = $comHelper->Load(getcwd() . "\%#ComTemplate.docx%"); // Find and replace text. $document->Content->Replace("PLACEHOLDER1", "Sample Value 1"); $document->Content->Replace("PLACEHOLDER2", "Sample Value 2"); $document->Content->Replace("PLACEHOLDER3", "Sample Value 3"); // Execute mail merge process. $source = new Com("System.Collections.Hashtable", null, CP_UTF8); $source->Add("Name", "John"); $source->Add("Surname", "Doe"); $source->Add("Age", 30); $document->MailMerge->Execute($source); // Modify bookmarks content. $document->Bookmarks->Item("Bookmark1")->GetContent(true)->LoadText("Sample Content 1."); $document->Bookmarks->Item("Bookmark2")->GetContent(true)->LoadText("Sample Content 2."); // Write document as PDF. $document->Save(getcwd() . "\ComExample.pdf"); ?>
Note, not all members of GemBox.Document are accessible because of the COM limitations like unsupported static and overload methods. That is why you can use the
ComHelper class which provides alternatives for some members that cannot be called with COM Interop.
However, if you need to use many GemBox.Document members from PHP, a recommended approach is to create a .NET wrapper library instead. Your wrapper library should do all the work within and exposes a minimal set of classes and methods to the unmanaged code.
This will enable you to take advantage of GemBox.Document's full capabilities, avoid any COM limitations, and improve performance by reducing the number of COM Callable Wrappers created at runtime.