SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting in output written through Fwrite() function

    Hi,

    I am exporting output through fwrite() function in .doc file.

    Here is my code

    Code:
    $folder = $_GET['folder'];
    $fileName= $folder ."/" .  $filename . ".doc";
    
    $output = $ _POST['cv']
    
    $fh = fopen($fileName, 'w');
    $i=0;
    while ($i<3) {
        fwrite($fh, $output);
        $i++;
    }
    fclose($fh);
    Output variable contains html tags eg: "<b>This is my first <i>output</i></b>" . I want to apply bold and other html formatting styles in doc file. Is this possible. If yes how?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your question is about .doc files, but your sample code is about an .xls file, so you should probably start by being clear about what you really want.

    You're also treating these proprietary binary formats as simple text files, which they're not. You need a library that supports the formats you want to write, or to use the PHP COM extension (if you're on a Windows server with MS Office installed)
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you provide me a sample code for this


    Quote Originally Posted by Mark Baker View Post
    Your question is about .doc files, but your sample code is about an .xls file, so you should probably start by being clear about what you really want.

    You're also treating these proprietary binary formats as simple text files, which they're not. You need a library that supports the formats you want to write, or to use the PHP COM extension (if you're on a Windows server with MS Office installed)

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rashidr View Post
    Can you provide me a sample code for this
    First, do you want to write Excel spreadsheets (.xls files) or Word documents (.doc files).

    Second, are you running this on a Windows server with MS Office installed, or on some other platform?

    Third, do you want to write real MS Office files, or just something that can be read by Excel/Word/whatever?
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a. I want to write .doc files
    b. Windows Server with MS Office installed
    c. If we can write real office files that would be great but someother files that can be ready by ms word would also be okay.


    Quote Originally Posted by Mark Baker View Post
    First, do you want to write Excel spreadsheets (.xls files) or Word documents (.doc files).

    Second, are you running this on a Windows server with MS Office installed, or on some other platform?

    Third, do you want to write real MS Office files, or just something that can be read by Excel/Word/whatever?

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In that case, you probably want to look at using the COM extension for PHP.

    I don't use COM, because I'm not running Windows servers, but something like:
    PHP Code:
    $word = new COM("word.application") or die ("couldnt create an instance of word");
    echo 
    "loaded , word version{$word->version}";
    //bring word to the front
    $word->visible 1;
    //open a word document
    $word->Documents->Add();
    //add some text to the document
    $word->Selection->TypeText("this is some sample text in the document");
    //save the document as sampleword.doc
    $word->Documents[1]->SaveAs("sampleword.doc");
    //close word
    $word->Quit();
    //free object resources
    $word->Release();
    $word null
    You'd need to check yourself how to set the formatting via COM.

    You might also take a look at libraries such as PHP MS Word which use COM, but provide a higher-level interface to the Word objects.
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •