Jump to content

problem writing to word document


BigD

Recommended Posts

I tried to write html page to word file. the word file is blank. Any one who knows how to do that please help. Here is my code, sorry it is lengthy: <?phpheader("Content-type: application/vnd.ms-word");header("Content-Disposition: attachment; filename=document/report.doc");//header("Pragma: no-cache");//header("Expires: 0");/* <html><head><style>@page { size 8.5in 11in; margin: 2cm }div.page { page-break-after: always }</style></head><body>*/include("../mylibrary/dograde.php");include("../mylibrary/login.php"); login(); function onepage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid) {echo "<h1> xxxxx School of Massage</h2></br>";echo "<h3>17043 El Camino Real, xxxxxxxx</h1></br>";echo "<h2>PROGRESS REPORT</h3></br>";echo "<h4> Evaluation # $eval Begins: $bdate Ends: $edate</h4></br>";echo "Student Name $fname $lname !";$query = "SELECT score FROM test where classid = $classid and stdid = $stdid and testname = 'M&M'";//echo "SQL $query</br>"; $result = mysql_query($query) or die(mysql_error());$row = mysql_fetch_array($result, MYSQL_ASSOC);$mscore = $row['score'];$query = "SELECT score FROM test where classid = $classid and stdid = $stdid and testname = 'Hist'";$result = mysql_query($query) or die(mysql_error());$row = mysql_fetch_array($result, MYSQL_ASSOC);$hscore = $row['score'];//echo "mscore, hscore are $mscore, $hscore</br>";$score =($mscore+$hscore)/2;//echo "score, $score</br>";$grade = dograde($score); $mttp = 'S';echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; echo "<tr><td><h2>Massage Therapy Technique(Written)</h2></td>\n"; echo "<td><h2>$grade</h2></td></tr>\n"; echo "<tr><td><h2>Massage Therapy Technique(pratical)</h2></td>\n"; echo "<td><h2>$mttp</h2></td></tr>\n"; $query = "SELECT stdid, AVG(score) FROM test where classid = $classid and stdid = $stdid and coursename = 'APP' GROUP BY coursename"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $avgscore = $row['AVG(score)']; $avggrade = dograde($avgscore); echo "avg score is $avgscore<br />"; echo "avg grade is $avggrade<br />"; echo "<tr><td><h2>Anatomy,Physiology & Pathology</h2></td>\n"; echo "<td><h2>$avggrade</h2></td></tr>\n"; $query = "SELECT stdid, AVG(score) FROM test where classid = $classid and stdid = $stdid and coursename = 'Kines' GROUP BY coursename"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $avgscore = $row['AVG(score)']; $avggrade = dograde($avgscore); echo "avg score is $avgscore<br />"; echo "avg grade is $avggrade<br />"; echo "<tr><td><h2>Kinesiology</h2></td>\n"; echo "<td><h2>$avggrade</h2></td></tr>\n"; $query = "SELECT stdid, AVG(score) FROM test where classid = $classid and stdid = $stdid and coursename = 'B & E' GROUP BY coursename"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $avgscore = $row['AVG(score)']; $avggrade = dograde($avgscore); echo "avg score is $avgscore<br />"; echo "avg grade is $avggrade<br />"; echo "<tr><td><h2>Business Practice and Professional Ethics</h2></td>\n"; echo "<td><h2>$avggrade</h2></td></tr>\n"; $query = "SELECT stdid, score FROM test where classid = $classid and stdid = $stdid and coursename = 'H & H'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $score = $row['score']; $grade = dograde($score)."/S/S"; echo "<tr><td><h2>Human Health & Hygiene / CPR / SFA</h2></td>\n"; echo "<td><h2>$grade</h2></td></tr>\n"; $query = "SELECT stdid, AVG(score) FROM test where classid = $classid and stdid = $stdid and coursename = 'Hydro' GROUP BY coursename"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $avgscore = $row['AVG(score)']; $avggrade = dograde($avgscore); echo "avg score is $avgscore<br />"; echo "avg grade is $avggrade<br />"; echo "<tr><td><h2>Hydrotherapy</h2></td>\n"; echo "<td><h2>$avggrade</h2></td></tr>\n"; if ( $eval < 5) $itn = 'NG'; echo "<tr><td><h2>Internship Program</h2></td>\n"; echo "<td><h2>$itn</h2></td></tr>\n"; echo "</table>\n";$mttw = 125; echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; echo "<tr><td><h2>Required Classroom Hours:</h2></td>\n"; echo "<td><h2>$mttw</h2></td></tr>\n"; echo "<tr><td><h2>Hours Attended to Date:</h2></td>\n"; echo "<td><h2>$mttw</h2></td></tr>\n"; echo "<tr><td><h2>Make-up Hours Needed:</h2></td>\n"; echo "<td><h2>$mttw - $mttw</h2></td></tr>\n"; echo "</table>\n"; } ?><div class="page"></div></body></html

Link to comment
Share on other sites

you need to use either COM http://php.net/com to manupulat word document but it will only work in windows enviourment, or http://phpword.codeplex.com/ to create doc file natively with phphere is one article http://www.joelonsoftware.com/items/2008/02/19.html

Link to comment
Share on other sites

I think the problem is I am using a function call that does not work well with word. anything outside the function gets created in word file. I will change the code not to use function calls. Are there anyone able to use functions and word?

Link to comment
Share on other sites

I think the problem is that you're outputting HTML code and calling it a .doc file. Word doc files are binary files, they aren't HTML documents with a different extension. Anyway, in the code above, you're never running that function. You define it, but you don't run it.

Link to comment
Share on other sites

You are right JSG, the function is called from the following code,<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="print.css" /></head><?php include("../mylibrary/onepage.php"); $classid = $_REQUEST['classid']; $atnstd = $_REQUEST['atnstd']; $fname = $_REQUEST['fname']; $lname = $_REQUEST['lname']; $eval = $_REQUEST['rptno']; $sdate = $_REQUEST['sdate']; $bdate1 = $sdate; $edate1 = strtotime(date("Y-m-d", strtotime($bdate1)) . " +3 month"); $edate1 = date('Y-m-d', $edate1); $bdate2 = $edate1; $edate2 = strtotime(date("Y-m-d", strtotime($bdate2)) . " +3 month"); $edate2 = date('Y-m-d', $edate2); $bdate3 = $edate2; $edate3 = strtotime(date("Y-m-d", strtotime($bdate3)) . " +3 month"); $edate3 = date('Y-m-d', $edate3); $bdate4 = $edate3; $edate4 = strtotime(date("Y-m-d", strtotime($bdate4)) . " +3 month"); $edate4 = date('Y-m-d', $edate4); $bdate5 = $edate4; $edate5 = strtotime(date("Y-m-d", strtotime($bdate5)) . " +3 month"); $edate5 = date('Y-m-d', $edate5); if ($eval == 1) {$bdate = $bdate1; $edate = $edate1;} if ($eval == 2) {$bdate = $bdate2; $edate = $edate2;} if ($eval == 3) {$bdate = $bdate3; $edate = $edate3;} if ($eval == 4) {$bdate = $bdate4; $edate = $edate4;} if ($eval == 5) {$bdate = $bdate5; $edate = $edate5;} $i = 0; while ($stdid = array_shift($atnstd)) { $rpt = $_REQUEST['rpt'.$i]; $firstname = array_shift($fname); $lastname = array_shift($lname); // echo "rpt is $firstname<br/>"; if ($rpt == 'Y') { onepage($firstname, $lastname,$eval, $bdate, $edate, $classid, $stdid); // echo "<h7 style=/"page-break-after/: always;/">end of report></h7>"; } $i++; }?> In the 1st piece code if I do this :<?phpheader("Content-type: application/vnd.ms-word");header("Content-Disposition: attachment; filename=document/report.doc");//header("Pragma: no-cache");//header("Expires: 0");/*<head><style>@page { size 8.5in 11in; margin: 2cm }div.page { page-break-after: always }</style></head>*/echo "<html>";echo "<body>";echo "Hello";include("../mylibrary/dograde.php");include("../mylibrary/login.php"); login(); function onepage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid) {echo "<h1> xxxxx School of Massage</h2></br>";echo "<h3>17043 El Camino Real, Suite 201 xxxxx</h1></br>";echo "<h2>PROGRESS REPORT</h3></br>"; Hello got written to the word file(and only that). So I do not think naming the file to .doc causes problem.Any suggestions?

Link to comment
Share on other sites

And if I do this to the function code: <?php function onepage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid){header("Content-type: application/vnd.ms-word");header("Content-Disposition: attachment; filename=document/report.doc");echo "<html>";echo "<body>";echo "Hello";include("../mylibrary/dograde.php");include("../mylibrary/login.php"); login(); echo "<h1> xxxxx School of Massage</h2></br>";echo "<h3>17043 El Camino Real, Suite 201 xxxxx</h1></br>";echo "<h2>PROGRESS REPORT</h3></br>"; 'Hello' did not get written to word file. it is a blank file. So I think problem lies between a function andvnd.ms-word interfaces. I do not know how to overcome the problem though!

Link to comment
Share on other sites

I was wrong and JSG is right. Binary data did get written to the file. After I changed this line:header("Content-Disposition: attachment; filename=document/report.doc");toheader("Content-Disposition: attachment; filename=report.htm");I could open it with IE and see my report. New problem is: it has 3 student's report back to back. That is why I wanted to write to a word document and insert page break at the end of each student page. Now it is a .htm document, how do I insert page break?

Link to comment
Share on other sites

I am still puzzled on creating .htm files. So many people have written PHP codes to create .doc files and we seldom heard people unable to see the file content. Only difference I see is most people are using straight PHP code, not calling a function to create .doc files.

Link to comment
Share on other sites

There is almost no difference of using function and using straight codes to do certain work. function gives modularity where each function does specific jobs. and using many functions we divide the problem in small reusable part and we solve bigger problem. I cant see you calling the function. i just see here declaring it. if you want to execute the function you need to call it seprately.http://php.net/language.function. as already said DOC is binary file so you have to write it like so. just specifying the .doc extension in last of file name won't make it doc file. I am not sure what problem you are facing to create html file with that. html file uses <br> as lin break. if it is just a report which is meant to be saved to client it will be better to save as format other than html like xml (if doc option is unavailable)

Link to comment
Share on other sites

Birbal,Thanks for your post.In the 2nd piece of code I posted, it has how the function is called:if ($rpt == 'Y'){onepage($firstname, $lastname,$eval, $bdate, $edate, $classid, $stdid);// echo "<h7 style=/"page-break-after/: always;/">end of report></h7>";}I am not just using .doc extension to make a word file. This line of code should do that:header("Content-type: application/vnd.ms-word");My problem is - A school administrator will run my code that will create a .htm file with many student's progress report in that file.He/she presses the print button, one page will contain 1 and half of next student's report. How to make a page break at the end of each student's report?

Link to comment
Share on other sites

I had an idea. My onepage function is called once for each student to generate progress report. So I added a parm with the output file name. I am looping through 3 students and my echo showed 3 output files report0, report1, report2 should be generated.I thought this will resolve the pagebreak issue since each report is a one page progress report.Guess what? it only created report2 with 3 student's report. Do not know why. Here is part of the code:<?php//header("Pragma: no-cache");//header("Expires: 0");/*<head><style>@page { size 8.5in 11in; margin: 2cm }div.page { page-break-after: always }</style></head>*/include("../mylibrary/dograde.php");include("../mylibrary/login.php"); login(); function onepage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid,$ofile){header("Content-type: application/vnd.ms-word");header("Content-Disposition: attachment; filename=$ofile.htm");?><html><body><?phpecho "create file $ofile";

Link to comment
Share on other sites

Question to Dsonesuk, Save as pdf file, how does that help with page break issue? Say I have a pdf file with 3 sets of info in it. I still need a page break at the end of each set. Can you show me an example to handle that.Not every one has a xps viewer, so I probably will not try to save as xps file.

Link to comment
Share on other sites

header("Content-type: application/vnd.ms-word");
this does not make file doc. it tells the browser about the content type of it so that it can handle properly. but if the content itself does not encoded in correct format. how can browser handle it? the page break you are referring is not possible in html file like those doc or pdf file. but you can create it visually in using css http://www.w3schools.com/cssref/pr_print_pageba.asp.the reason i told to output it to xml that there is many libraries and application which can handle xml file to produce doc file or pdf file. as far i am aware docx (multiplatform extension for doc file) can take xml file. as xml file is meant to be portable and readable be human so it has advantages to use it in such reports. you can use same file to generate different format easily for pdf generation there is php library http://php.net/pdf or third party library written on php http://fpdf.org to do that. it can handle xml file also. Edited by birbal
Link to comment
Share on other sites

you would use the same css style but for print

@media print{div.page { page-break-after: always }}

then use program such as cutepdf writer, select 'print, select 'cutepdf' from printer option list, and save, just like you would with xps, or even MS office 2010 notes option where can save as pdf, doc,xlsx etc within that.

Link to comment
Share on other sites

I created a test.php. When IE browser went to open it, got this error:Fatal error: Call to undefined function PDF_new() in C:\wamp\www\mschool\admin\test.php on line 2Here is test.php:<?php$p = PDF_new();/* open new PDF file; insert a file name to create the PDF on disk */if (PDF_begin_document($p, "", "") == 0) { die("Error: " . PDF_get_errmsg($p));}PDF_set_info($p, "Creator", "hello.php");PDF_set_info($p, "Author", "Rainer Schaaf");PDF_set_info($p, "Title", "Hello world (PHP)!");PDF_begin_page_ext($p, 595, 842, "");$font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");PDF_setfont($p, $font, 24.0);PDF_set_text_pos($p, 50, 700);PDF_show($p, "Hello world!");PDF_continue_text($p, "(says PHP)");PDF_end_page_ext($p, "");PDF_end_document($p, "");$buf = PDF_get_buffer($p);$len = strlen($buf);header("Content-type: application/pdf");header("Content-Length: $len");header("Content-Disposition: inline; filename=hello.pdf");print $buf;//PDF_delete($p);?>

Link to comment
Share on other sites

Is this how I use page break Dsonesuk?<head><style>@media print{div.page { page-break-after: always } }</style></head><?phpecho "<div>";// someloop started here {echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; echo "<tr><td><div align=\"center\">Required Classroom Hours:</div></td>\n"; echo "<td><div align=\"center\">$mttw</div></td></tr>\n"; echo "<tr><td><div align=\"center\">Hours Attended to Date:</div></td>\n"; echo "<td><div align=\"center\">$mttw</div></td></tr>\n"; echo "<tr><td><div align=\"center\">Make-up Hours Needed:</div></td>\n"; echo "<td><div align=\"center\">$mttw - $mttw</div></td></tr>\n"; echo "</table>\n"; // ??????I want a pagebreak here echo "<page>"; } echo "</div>";echo "</body>";echo "</html>";?>

Link to comment
Share on other sites

then you would have something similar to

@media print{.page { page-break-after: always; height:1px;font-size:0; line-height:0; } }

<?phpecho "<div>";// someloop started here{echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n";echo "<tr><td><div align=\"center\">Required Classroom Hours:</div></td>\n";echo "<td><div align=\"center\">$mttw</div></td></tr>\n";echo "<tr><td><div align=\"center\">Hours Attended to Date:</div></td>\n";echo "<td><div align=\"center\">$mttw</div></td></tr>\n";echo "<tr><td><div align=\"center\">Make-up Hours Needed:</div></td>\n";echo "<td><div align=\"center\">$mttw - $mttw</div></td></tr>\n";echo "</table>\n";// ??????I want a pagebreak here echo '<div class="page"> </div>'."\n";} the .page class can be applied to any element, it can be <p></p>, <hr /> otherthan <div></div> if you look through print preview it will show you will end up with, you then just select the print pdf program from printer list and it will show the same result as pdf file I use http://www.cutepdf.com/Products/CutePDF/writer.asp

Edited by dsonesuk
Link to comment
Share on other sites

I ran a short version of code and created report2.htm. Right Clicked it, select print, select CutePDF Writer as printer, clicked print.On save as panel I used report2.pdf as file name.This file can not be opened by Acrobat reader. When sent to printer it printed pages of garbage. Here is the function that was called to generate the file:<head><style>@media print{.page { page-break-after: always; height:1px;font-size:0; line-height:0; } }</style></head><?php function shortpage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid,$ofile){header("Content-type: application/vnd.ms-word");header("Content-Disposition: attachment; filename=$ofile.htm");?><html><body><?phpecho "<div align=\"center\"> School of Massage</div></br>";echo "<div align=\"center\">17043 El Camino Real, Suite 201 ~ Houston, Texas 77058 ~ </div></br>";echo "<div align=\"center\">PROGRESS REPORT</h3></br>";echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; echo "<tr><td><div align=\"center\">Massage Therapy Technique(Written)</div></td>\n"; echo "<td><div align=\"center\">$fname</div></td></tr>\n"; echo "<tr><td><div align=\"center\">Massage Therapy Technique(pratical)</div></td>\n"; echo "<td><div align=\"center\">$eval</div></td></tr>\n"; echo "</table>\n";echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; echo "<tr><td><div align=\"center\">Required Classroom Hours:</div></td>\n"; echo "<td><div align=\"center\">$bdate</div></td></tr>\n"; echo "<tr><td><div align=\"center\">Hours Attended to Date:</div></td>\n"; echo "<td><div align=\"center\">$edate</div></td></tr>\n"; echo "<tr><td><div align=\"center\">Make-up Hours Needed:</div></td>\n"; echo "<td><div align=\"center\">$ofile</div></td></tr>\n"; echo "</table>\n"; echo '<div class="page"> </div>'."\n"; //echo "<br clear='all' style='page-break-after:always'/>";echo "</div>";echo "</body>";echo "</html>";}?>

Link to comment
Share on other sites

I ran THIS short version of code, it looped and showed content 3 times, print preview showed repeated content on three separate pages, selected cutepdf writer, press ok or whateever browser version shows, gave the pdf a file name, pressed save as button, produced same page as displayed in print preview, content was not garbage.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><?php$fname ="holy crap1 bat man";$lname="holy crap2 bat man";$eval="holy crap3 bat man";$bdate="holy crap4 bat man";$edate="holy crap5 bat man";$classid="holy crap6 bat man";$stdid="holy crap7 bat man";$ofile="holy crap8 bat man";function shortpage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid,$ofile){?><?phpecho "<div align=\"center\"> School of Massage</div></br>";echo "<div align=\"center\">17043 El Camino Real, Suite 201 ~ Houston, Texas 77058 ~ </div></br>";echo "<div align=\"center\">PROGRESS REPORT</h3></br>";echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n";echo "<tr><td><div align=\"center\">Massage Therapy Technique(Written)</div></td>\n";echo "<td><div align=\"center\">$fname $lname</div></td></tr>\n";echo "<tr><td><div align=\"center\">Massage Therapy Technique(pratical)</div></td>\n";echo "<td><div align=\"center\">$eval</div></td></tr>\n";echo "</table>\n";echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n";echo "<tr><td><div align=\"center\">Required Classroom Hours:</div></td>\n";echo "<td><div align=\"center\">$bdate</div></td></tr>\n";echo "<tr><td><div align=\"center\">Hours Attended to Date:</div></td>\n";echo "<td><div align=\"center\">$edate</div></td></tr>\n";echo "<tr><td><div align=\"center\">Make-up Hours Needed:</div></td>\n";echo "<td><div align=\"center\">$ofile</div></td></tr>\n";echo "</table>\n";echo '<div class="page"> </div>'."\n";//echo "<br clear='all' style='page-break-after:always'/>";echo "</div>";}?><style>@media print{.page { page-break-after: always; height:1px;font-size:0; line-height:0; }}#wrapper {width:960px; margin:0 auto;}</style></head><body><div id="wrapper"><?phpfor($i=0;$i<3;$i++){shortpage($fname, $lname, $eval, $bdate, $edate, $classid, $stdid,$ofile);}?></div></body></html>

Link to comment
Share on other sites

Yes. Do not know why but now the report2.pdf can be viewed with the right contents. and printed ok. I did not change anything.One small problem. It produces 5 pages with blank 1st and 5th page, real report on pages 2-4.

Link to comment
Share on other sites

My guess is this is IE producing these results, and not other better browsers, what do you expect, crap browsers produce crap results. As usual you will have to google page break IE problem, find out what fix you have to use to make it act like other better browsers. Mind you it could be down to the fact you have unopen/unclosed elements, elements that are incorrectlt closed, that why you should validate before posting code http://validator.w3.org/

Edited by dsonesuk
Link to comment
Share on other sites

Here are some options for creating native Word documents with PHP: http://phpword.codeplex.com/http://www.phpdocx.com/ You can also look into creating RTF documents instead, RTF is a text-based format. If you want to create a PDF document I would recommend using the FPDF library, which does not require any external support on the server. Any of those libraries will have a way to insert a new page if that is your concern.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...