05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Now we can complete the invoice with data we get from an HTML form. We also<br />

want to print the form, though.<br />

If we only wanted to save an electronic copy, it would be as simple as:<br />

$word->ActiveDocument->SaveAs("c:/path/to/invoices/myinvoice.doc");<br />

This has the side effect of setting the ActiveDocument->Saved flag to True, which lets<br />

us close the application without being prompted to save the modified invoice.<br />

If we want to print the document, there are three steps: print, mark the document as<br />

saved so we can quit without a dialog box, then wait until the printing has finished.<br />

Failure to wait means the user will see a “Closing this application will cancel printing”<br />

warning. Here’s the code for doing this:<br />

$word->Application->Run("invoiceprint");<br />

$word->Application->ActiveDocument->Saved=True;<br />

while($word->Application->BackgroundPrintingStatus>0){sleep (1);}<br />

In this code, we’ve created a macro, InvoicePrint, with our desired printer settings.<br />

Once we call the macro, we loop until the value of BackgroundPrintingStatus is set<br />

to 0.<br />

Example 15-2 shows the complete <strong>PHP</strong> program to complete and print the invoice<br />

using Word.<br />

Example 15-2. Completing and printing a Word invoice from <strong>PHP</strong><br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!