SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Filling Word Doc from Mysql Database Using Php

    Good day Mr. Moderator.
    I have a challenge presently. I have a project I'm working and I'm provided with a template by my client ( a word document template with spaces like name, address etc to be filled in from the database or web form. I have worked on getting the info stored in the database but the challenge is how do I get the info from the database to fill in the placeholders in the word document so that they can download it and print it out. Any advice or suggestion will be highly appreciated. Thanks a lot.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Maybe use a PDF instead?
    You could then create the PDF dynamically using a library such as Zend_Pdf and populate it with the relevant information from your database.
    Here's a tutorial on PHP master to get you started: http://phpmaster.com/generating-invoices-with-zend_pdf/

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    I agree with @Pullo ;

    I was part of a maintenance team that had to support a web application that filled out Word documents about a year ago. The problem we ran into was pretty big, first Word is the only Office product by Microsoft that does not support multi-threading. Secondly, getting the instance to close properly and consistently was a nightmare, so we ultimately ended up with multiple instances that would all take up large amounts of memory, because Word was never designed in a Server scenario. Lastly, the changes that occurred between versions to the WordML and the likes made it a maintenance nightmare as you tried to support multiple versions.

    If you can talk the client into using a PDF, there are some nice PDF frameworks out there. fpdf is the one I consistently use.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    The problem we ran into was pretty big, first Word is the only Office product by Microsoft that does not support multi-threading.
    More than just Word I'm afraid. IMO, whatever MS Office is built on is garbage. When running more than one product in the Office line, if one of them hang, they all hang, even Microsofts built in clock / calendar on the task bar.

    Quote Originally Posted by cpradio View Post
    Secondly, getting the instance to close properly and consistently was a nightmare, so we ultimately ended up with multiple instances that would all take up large amounts of memory, because Word was never designed in a Server scenario.
    Probably wrote in VBA?

    Stay far far away from MS Word if possible. PDFs are the way to go.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Probably wrote in VBA?

    Stay far far away from MS Word if possible. PDFs are the way to go.
    No, it was written in .NET using the API's provided by Microsoft for handing Office applications. Nonetheless, I would never recommend interacting with a Microsoft Product via a web application (or desktop for that matter) ever again (not that I have recommended it).
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Nonetheless, I would never recommend interacting with a Microsoft Product via a web application (or desktop for that matter) ever again (not that I have recommended it).
    @cpradio ; Ditto

  7. #7
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    http://dev.mysql.com/doc/refman/5.0/...wordexcel.html

    I don't know if that would work for the online version of office
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  8. #8
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Why not just create a styled web page to display the information? They can print that directly and you wouldn't need to use any proprietary software, or download any files.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot everyone who have taken their time to respond to my question. I think pdf is the way to go and I've studied fpdf within the last 2 days to see how easy it'll be to implement. Though the tables making up the document is quite complex but I should be able to get it done. My other worry is how to fill in the spaces in the document with result from the database. I hope fpdf supports that. Thanks a lot

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by papadammy View Post
    Thanks a lot everyone who have taken their time to respond to my question. I think pdf is the way to go and I've studied fpdf within the last 2 days to see how easy it'll be to implement. Though the tables making up the document is quite complex but I should be able to get it done. My other worry is how to fill in the spaces in the document with result from the database. I hope fpdf supports that. Thanks a lot
    This is what I usually do with fpdf
    1. Download fpdi
    2. Take the existing word document or template and turn it into a PDF (especially if it only contains the static content)
    3. Using fpdf with fpdi, load the template and then write whatever text you need on top of the template to produce the final PDF


    Sample code:
    PHP Code:
      require_once('pdf-templates/fpdf.php');
      require_once(
    'pdf-templates/fpdi.php');

      
    $pdf = new FPDI('P''pt''Letter');
      
    $pdf->AddPage('P''Letter');
      
    $pdf->setSourceFile('template.pdf');
      
    $templatePage $pdf->importPage(1);
      
    $pdf->useTemplate($templatePage0000true);

      
    $pdf->Image($itemData->qrcodeAbsolutePath49064000"png");

      
    $pdf->SetFont('Arial''B'28);
      
    $pdf->SetTextColor(1593132);
      
    $pdf->SetXY(28180);
      
    $pdf->MultiCell(028$boatDescription0'C'false); 
    Hopefully this should get you started. I've had great success with this route.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much Cpradio for your response. I will surely try it. Please can you put me through on the procedure of displaying the results from my mysql database in the resulting pdf and how to mail it to the concerned client right away. I know how to extract from the database but the challenge is to how input it into fpf and create the final pdf document. I understand how cell work in fpdf but I've not seen any example on how to use result from the database in fpdf. Thanks in advance

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by papadammy View Post
    Thanks so much Cpradio for your response. I will surely try it. Please can you put me through on the procedure of displaying the results from my mysql database in the resulting pdf and how to mail it to the concerned client right away. I know how to extract from the database but the challenge is to how input it into fpf and create the final pdf document. I understand how cell work in fpdf but I've not seen any example on how to use result from the database in fpdf. Thanks in advance
    Sure, if you look at my prior code example, just replace $boatDescription, and the other variables with the $row['column'] of your MySQL result and you'll be good to go.

    As for how to e-mail it, that part requires a bit of work.
    1. First you will need to call the Output method using the "Save" parameter http://www.fpdf.org/en/doc/output.htm
    2. Then you will need to either a) create a mail framework that works with attachments, or b) find one that allows you to easily add attachments. I suggest PHPMailer
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much Cpradio. Your input has been of immense help. I'll work on it.

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2012
    Location
    Scarborough, North Yorkshire, United Kingdom
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see you seem to have settled on generating a PDF (correct choice) but if anyone is interested I did lots of work with WordML many moons ago so know its possible - http://www.codeproject.com/Articles/...-WordML-Basics
    Richard
    Resell SSL Certificates - API / WHMCS / HostBill / ClientExec
    ServerTastic - RapidSSL, Geotrust, Thawte, Symantec, SmarterTools and more

  15. #15
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Richard, I'll chek out your work as well. I just want to get through with this load in front of me first.

  16. #16
    SitePoint Zealot
    Join Date
    Jul 2012
    Location
    Scarborough, North Yorkshire, United Kingdom
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops, sorry I wasn't too clear.

    I didn't create the linked to content, just found it useful before
    Richard
    Resell SSL Certificates - API / WHMCS / HostBill / ClientExec
    ServerTastic - RapidSSL, Geotrust, Thawte, Symantec, SmarterTools and more

  17. #17
    SitePoint Member guido24's Avatar
    Join Date
    Oct 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Disclaimer is that I work for a company that sells one of these, but nonetheless I want to share my experiences because I have been confronted with this question so often.

    Using Invantive Composition (http://www.invantive.com/products/invantive-composition) you can fill Word documents (letters, legal pleadings, contracts and other things with flexible conditions) with data from a supported database platform. Currently these are Oracle, MySQL, Teradata, DB2/UDB and SQL Server). And then fully change the contents at will manually. It is intended for non-technical users. The central webservice proxies the data requests for the backend databases. Allows nested repeating groups of data (such as: "order with order lines").

    In the past, JasperReports (http://community.jaspersoft.com/proj...eports-library) has been used a lot to generate letters using the RTF output of JasperReports (designer tool is named iReports). It is free and works fine as long as you do not want to edit the output more than a few words and have some Java development skills. It integrates well in various websites but you need to integrate Java runtime. Just as Invantive Composition it works fine for large numbers of different reports.

    As long as you can control the environment completely, you can also consider using RTF as intermediate language (not for end-users, only real developers). Save document as RTF, replace parts of the text you need to be replacable, write a webservice that accepts the parameter and dumps back the resulting RTF. Takes some time to generate more complex tables (tables are obviously something invented by the human race after the RTF specification was written :-) This approach only works with very limited number of templates and when you have sufficient developer time available to get it up and running and stabilized.

    Sometimes it is also possible to use XML (docx is actually a zip with xml in it). Using the RTF approach. This one is harder upto very hard, because for instance Word adds XML-tags anywhere in the text. You really need to write a multi-stage parser for that to do it reliably.

    I think when you need just a few templates and want to save out-of-pocket costs and have sufficient time, I would go for RTF in your case.

  18. #18
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks man. I actually used fpdf as it'll ease the pain of printing as well. I appreciate you

  19. #19
    SitePoint Member guido24's Avatar
    Join Date
    Oct 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are welcome.


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
  •