PHP Builder Forum IndexPHP Builder
free solutions for web developers
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in            Calendar



COMPUSORY UPGRADE!!! Request an upgrade NOW! 32+ Pre-installed Modifications! 3 Server Locations to choose from: USA, UK and JAPAN.

11th December 2012 - phpBB88: All servers are upgraded to run using SSD drive. Click Here to report problems!

[CODE-Snippet]Making DOC-files with PHP

 
Post new topic   Reply to topic    PHP Builder Forum Index -> Snippets, Tutorials & Other Stuff
View previous topic :: View next topic  
Author Message
zewa666
Site Moderator
Site Moderator


Joined: 01 Feb 2007
Posts: 606
:


Items

PostPosted: Tue Mar 20, 2007 8:15 am    Post subject: [CODE-Snippet]Making DOC-files with PHP Reply with quote

Hi there ...

this here is not a true Datagrid topic, but as I written it while using Datagrid it maybe be of interest for you too

What I do here is simply to create a RTF document with some KEYWORDS inside of ?? --> like ?KEYWORD1?
next I parse the whole document and replace the Keyword with a value from a databasequery.

the rtf can be simply renamed to .doc and it opens up in every office word or openoffice writter version.

Sure there are some more nice and precise ways to solve it ... like the DOM features. But they all are restricted to Windows, and since my customers all have LINUX-Servers I needed a platform independent solution. so here it is.

Right now I include a template.doc with some keywords to download:
http://zewa.ze.funpic.de/datagrid/template.doc

and here is my sample code... you need to correct it regarding to your needs and make some query to test it.
It is supposed that the phpscript and the template.doc are lying all together in the same folder, and the new file is also created there.



Code:

$rid = $_GET['f_rid'];  //SELECTING THE ID OF THE ROW BEEING MANIPULATED
$sql = mysql_query("SELECT * FROM blablabla WHERE id = '$rid'", $connection) or die(mysql_error());
$row_sql = mysql_fetch_assoc($sql);

//DEFINING MY KEYWORDS IN THE RTF

$w_field1 = "?ADRESS?";
$w_field2 = "?NAME?";
$w_field3 = "?NAME2?";
$w_field4 = "?DATEFIELD?";
$w_field5 = "?PLACE?";
$w_field6 = "?PRICE?";
$w_field7 = "?VAT?";
$w_field8 = "?SUM?";
$w_field9 = "?TODAY?";

//DEFINING WITH WHAT TO REPLACE

$n_field1 =  $row_sql['Titel']." \par ".$row_sql['Name']."\par"."z.H.: ".$row_sql['Ansprechpartner']." \par ".$row_sql['Strasse']." \par ".$row_sql['PLZ']." \par ".$row_sql['Ort'];
$n_field2 = $row_sql['name'];
$n_field3 = $row_sql['name2'];
$n_field4 = strftime("%d.%m.%Y",strtotime($row_sql['datefield']));
$n_field5 = $row_sql['place'];
$n_field6 = $row_sql['price'];
$n_field7 = $row_sql['price'] * 0.2;
$n_field8 = $n_field6 + $n_field7;
$n_field9 = date(d).".".date(m).".".date(Y);



//NEW FILE THAT SHOULD BE GENERATED
$new_file = "BLABLABLA_$rid.doc";
//THE TEMPLATE RTF FILE AS DOC
$template_file = "template.doc";

//READING EVERYTHING OF THE TEMPLATE INTO $CONTENTS
$handle = fopen($template_file , "r");
$contents = fread($handle, filesize($template_file));

//CHANGING THE KEYWORD WITH NEW VALUE
$newphrase1 = str_replace($w_field1, $n_field1, $contents);
$handle2 = fopen($new_file , "w");
fwrite ($handle2 ,$newphrase1);
fclose ($handle2);
fclose ($handle);

//NOW THAT THE FILE IS CREATED I DEFINE THE REST OF THE KEYWORDS AND VALUES INSIDE 2 ARRAYs
$w = array($w_field2, $w_field3, $w_field4, $w_field5, $w_field6, $w_field7, $w_field8, $w_field9);
$n = array($n_field2, $n_field3, $n_field4, $n_field5, $n_field6, $n_field7, $n_field8, $n_field9);

//AND LOOP THE WHOLE THING AGAIN
for($i = 0; $i < count($w); $i++) {
$handle = fopen($new_file , "r");  //HERE IS NOW THE NEWFILE NOT THE TEMPLATE
$contents = fread($handle, filesize($new_file));
$newphrase2 = str_replace($w[$i], $n[$i], $contents);
$handle2 = fopen($new_file , "w");
fwrite ($handle2 ,$newphrase2);
fclose ($handle2);
fclose ($handle);
clearstatcache();  //THIS IS IMPORTANT OTHERWISE YOU HAVE A BUFFEROVERFLOW
}


Hope this is usefull and some of you guys can need it.
I'm going to implement the it for my datagrid, so that you dont need to specify a new query, but to be able to use the select of the datagrid details.
also i wanna try to include it to datagrid that way, that you can display it in every row as an image, like the "EXPORT TO EXCEL". so this would be "EXPORT TO DOC".

After all ... why DOC some of you will ask. well its right ... doc isnt the standard way of internet textfile changing. its PDF. But some of my customers, want to create automaticly created DOC files, because they can append or erase unnecessary text there and create PDFs from within by there own.

Surely the whole thing can be done with PDF too, using the FPDF or PDFlibs of PHP but thats another story Smile
Maybe Ill build some tutorial template of my using of PHP2Pdf too and post it here.

OK so if any problems appear with this script youre welcome to ask here or write me a pn, or ask my via ICQ.

Greetings
Zewa
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ferd
Newbie
Newbie


Joined: 01 Jan 2008
Posts: 5
:


Items

PostPosted: Wed Jan 02, 2008 8:14 am    Post subject: Reply with quote

Hi Zewa,

happy new year, and great datagrid!

I am trying out this doc function, but not sure where to put it.

I tried placing beside the csv function in datagrid.class.php, but it could not pick up the variables it required.

Then I tried placing in the other file, but then I could not call it from the exportTo function in the original file.

Please excuse my ignorance, not an expert Sad

Thanks in advance for any help!

Alex
Back to top
View user's profile Send private message
zewa666
Site Moderator
Site Moderator


Joined: 01 Feb 2007
Posts: 606
:


Items

PostPosted: Wed Jan 02, 2008 9:11 am    Post subject: Re: [CODE-Snippet]Making DOC-files with PHP Reply with quote

zewa666 wrote:
Hi there ...

this here is not a true Datagrid topic, but as I written it while using Datagrid it maybe be of interest for you too



Like you see its no Datagrid Expansion ... its just a standalone script.

What you'd like to do with that DOC export ... tell me that and maybe there will be an easy workaround for you that works.

If its only the mather that you need the selected rows for that datagrid export than its no problem, we can easiely make an MULTIOPERATION that calls another php file which does that creating of DOC files with the according selected values.

Greetings
Zewa
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ferd
Newbie
Newbie


Joined: 01 Jan 2008
Posts: 5
:


Items

PostPosted: Wed Jan 02, 2008 3:56 pm    Post subject: Reply with quote

Thanks for the reply Zewa.
I understand that this is not a included feature of the datagrid, so appreciate any help you can give! Very Happy

I have got it kind-of working back in datagrid.class.php.
instead of the sql, I copied over the

$row = $this->data_set->fetchRow();

which seems to have picked up the record details ok.
I am calling the values with row[1], row[2] etc...

What I want to do is have an export to 'doc' button beside the excel on, which I have done with no problems! When this is press, a word doc will open, with a standard form letter to a customer with their nam, address details etc.

My next problem is that, after changing some of the field placeholders on the template.rtf, only some of the values are being passed across. Is there a hidden way to make the placeholders become active? I am pretty sure I am spelling field values similarly.

Cheers

Alex
Back to top
View user's profile Send private message
zewa666
Site Moderator
Site Moderator


Joined: 01 Feb 2007
Posts: 606
:


Items

PostPosted: Wed Jan 02, 2008 9:11 pm    Post subject: Reply with quote

so as I understand it works but simply some keywords are not filled properly?

Have you checked the spelling of the keywords cause, on Binary reading its CASE SENSITIVE meaning upper and lowercase is important.

Greetings
Zewa
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ferd
Newbie
Newbie


Joined: 01 Jan 2008
Posts: 5
:


Items

PostPosted: Thu Jan 03, 2008 5:16 am    Post subject: Reply with quote

Thanks Zewa,

that fixed it. It was a combination of that and getting my variable numbers mixed up with the row[x] ordinals Embarassed

Now my final problem is, getting the doc to open somehow in the browser or Word. The populated rtf file has been created in the local directory of the server, but had no luck so far in opening it up like your excel and pdf functions.

The closest I got is with the

$this->exportDownloadFile("template.rtf");

but get the error 'Can not find such path: ../template.rtf !' in the popup.

Thanks again
Back to top
View user's profile Send private message
zewa666
Site Moderator
Site Moderator


Joined: 01 Feb 2007
Posts: 606
:


Items

PostPosted: Thu Jan 03, 2008 7:20 am    Post subject: Reply with quote

thats because datagrid standardly generates the outputs in the main DG folder.

if your structure is:

WWWROOT (OR HTDOCS)
|
----> YOURSUBFOLDER
|
----> DG-FOLDER


what you should do is either creating the template inside the dg-folder or call the above function this way:

Code:
$this->exportDownloadFile("../../template.rtf");


this way you jump back to your HTDOCS folder.


When finished it would be very nice if you could tell us the changes you did so everyone can profit from your efforts.

Greetings
Zewa
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ferd
Newbie
Newbie


Joined: 01 Jan 2008
Posts: 5
:


Items

PostPosted: Thu Jan 03, 2008 11:44 am    Post subject: Reply with quote

Success!
Well close to it, and good enough for me Smile
This is the main function I placed in datagrid.class.php, after the exportToXml() function (about line 1400).

Code:
   //--------------------------------------------------------------------------
    // Export to DOC (if you change export file name - change file name length in download.php)
    //--------------------------------------------------------------------------   
   function exportToDoc(){
   $this->req_page_size = (isset($_REQUEST[$this->unique_prefix.'page_size']))?$_REQUEST[$this->unique_prefix.'page_size']:$this->req_page_size;
   
   $row = $this->data_set->fetchRow();
   //$row_sql = mysql_fetch_assoc($sql);
   
   //DEFINING KEYWORDS IN THE RTF
   $w_field1 = "?NAME?";
   $w_field2 = "?ADDRESS1?";
   $w_field3 = "?ADDRESS2?";
   $w_field4 = "?ADDRESS3?";
   $w_field5 = "?POSTCODE?";
   $w_field6 = "?MORTPROVIDER?";
   $w_field7 = "?MORTEXPIRY?";
   $w_field9 = "?TODAY?";
   
   //DEFINING WITH WHAT TO REPLACE
   $n_field1 = $row[2]; // name
   $n_field2 = $row[3]; // address1
   $n_field3 = $row[4]; // address2
   $n_field4 = $row[5]; // address3
   $n_field5 = $row[6]; //postcode
   
   $n_field6 = $row[9]; //mort provider
   //$n_field7 = $row[11];  //mort expiry
   $n_field7 = strftime("%d/%m/%Y",strtotime($row[11]));
   $n_field9 = date(jS)." ".date(F)." ".date(Y);
   
   //NEW FILE THAT SHOULD BE GENERATED
   $new_file = "datagrid/tester.rtf";
   //THE TEMPLATE RTF FILE AS DOC
   $template_file = "datagrid/template.rtf";
   
   //READING EVERYTHING OF THE TEMPLATE INTO $CONTENTS
   $handle = fopen($template_file , "r");
   $contents = fread($handle, filesize($template_file));
   
   //CHANGING THE KEYWORD WITH NEW VALUE
   $newphrase1 = str_replace($w_field1, $n_field1, $contents);
   $handle2 = fopen($new_file , "w");
   fwrite ($handle2 ,$newphrase1);
   fclose ($handle2);
   fclose ($handle);
   
   //NOW THAT THE FILE IS CREATED I DEFINE THE REST OF THE KEYWORDS AND VALUES INSIDE 2 ARRAYs
   $w = array($w_field1, $w_field2, $w_field3, $w_field4, $w_field5, $w_field6, $w_field7, $w_field8, $w_field9);
   $n = array($n_field1, $n_field2, $n_field3, $n_field4, $n_field5, $n_field6, $n_field7, $n_field8, $n_field9);
   
   //AND LOOP THE WHOLE THING AGAIN
   for($i = 0; $i <count>exportDownloadFile($fileToDownload);
   clearstatcache();  //THIS IS IMPORTANT OTHERWISE YOU HAVE A BUFFEROVERFLOW
   }

}


Then added this to the function exportTo() :

Code:
else if($export_type == "doc"){
                $this->exportToDoc();   



The template rtf file I placed in the datagrid folder. I had trouble with download.php saying it could not find the file, but realised that the rtf I created (tester+id.rtf) had a filename longer than 10 chars it expected!

To create the button, I copied the excel button code and image.

I may have missed something, just let me know if so.

Hope it helps someone!

Cheers
Alex
Back to top
View user's profile Send private message
zewa666
Site Moderator
Site Moderator


Joined: 01 Feb 2007
Posts: 606
:


Items

PostPosted: Thu Jan 03, 2008 1:56 pm    Post subject: Reply with quote

nice thing ... but doing it this way its only usable for one special function and thats putting this out

$w_field1 = "?NAME?";
$w_field2 = "?ADDRESS1?";
$w_field3 = "?ADDRESS2?";
$w_field4 = "?ADDRESS3?";
$w_field5 = "?POSTCODE?";
$w_field6 = "?MORTPROVIDER?";
$w_field7 = "?MORTEXPIRY?";
$w_field9 = "?TODAY?";

it would be better if you create 2 new arrays in the class, lets say one called "doc_keywords" and the second "doc_values".

these can be now filled in the bindpage with values, so that regarding to the bind page you always can do seperate exports.

Thx for the post this surely is an interesting thing.

Greetings
Zewa
Back to top
View user's profile Send private message Send e-mail Visit poster's website
     
Display posts from previous:   
Post new topic   Reply to topic    PHP Builder Forum Index -> Snippets, Tutorials & Other Stuff All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
PHP Builder topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

FREE FORUM HOSTING by AtFreeForum. Terms of Service - Privacy Policy
FASHION ACCESSORIES - BLING BLING - LADIES WATCHES - KOREAN CHILDREN CLOTHING - ONLINE BARGAIN STORE - FASHION JEWELLERIES