如何去除生成时的下载提示

梦宇飞 2009-07-17 05:19:00
<?php
/**

error_reporting(E_ALL);

class clsMsDocGenerator{
var $appName = 'MsDocGenerator';
var $appVersion = '0.2';

var $leftMargin = 2.5;
var $rightMargin = 2.5;
var $topMargin = 3.0;
var $bottomMargin = 3.0;

var $documentBuffer;
var $formatBuffer;
var $cssFile;
var $lastSessionNumber;
var $lastPageNumber;
var $atualPageWidth;

var $tableIsOpen;
var $tableLastRow;

function clsMsDocGenerator($pageOrientation = 'LANDSCAPE', $pageType = 'A4', $cssFile = ''){
$this->documentBuffer = '';
$this->formatBuffer = '';
$this->cssFile = $cssFile;
$this->lastSessionNumber = 0;
$this->lastPageNumber = 0;
$this->atualPageWidth = 0;

$this->tableIsOpen = false;
$this->tableLastRow = 0;

$this->newSession($pageOrientation, $pageType);
$this->newPage();
}//end clsMsDocGenerator()

/**
* public int newSession(const $pageOrientation = 'PORTRAIT', const $pageType = 'A4')
* @param $pageOrientation: The orientation of the pages of the session, 'PORTRAIT' or 'LANDSCAPE'
* @param $pageType: The type of the paper of the pages of the session
* @return int: the number of the new session
*/
function newSession($pageOrientation = 'LANDSCAPE', $pageType = 'A4'){
$this->lastSessionNumber++;

if($this->lastSessionNumber != 1){
$this->endSession();
$this->documentBuffer .= "<br clear=all style='page-break-before:always; mso-break-type:section-break'>\n";
}

$this->atualPageWidth = constant($pageType . '_WIDTH');

$sessionName = "Section" . $this->lastSessionNumber;

$pageSize = constant($pageType . '_' . $pageOrientation . '_SIZE');
$pageMargins = constant($pageType . '_' . $pageOrientation . '_MARGIN');

$this->formatBuffer .= "@page $sessionName\n";
$this->formatBuffer .= " {size: $pageSize;\n";
$this->formatBuffer .= " margin: $pageMargins;\n";
$this->formatBuffer .= " mso-header-margin: 36pt;\n";
$this->formatBuffer .= " mso-footer-margin: 36pt;\n";
$this->formatBuffer .= " mso-paper-source: 0;}\n";
$this->formatBuffer .= "div.$sessionName\n";
$this->formatBuffer .= " {page: $sessionName;}\n\n";

$this->documentBuffer .= "<div class=$sessionName>\n";

return $this->lastSessionNumber;
}//end newSession()

/**
* public int newPage(void)
* @return int: the number of the new page
*/
function newPage(){
$this->lastPageNumber++;
if($this->lastPageNumber != 1)
$this->documentBuffer .= "<br clear=all style='page-break-before:always;'>";
return $this->lastPageNumber;
}//end newPage()

/**
* public void output(void)
*/
function output($filename=''){
$this->endSession();
header('Content-Type: application/msword; charset=gb2312');
if(!$filename){
header("Content-Disposition:filename=$filename");

}
echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"' . "\n";
echo ' xmlns:w="urn:schemas-microsoft-com:office:word"' . "\n";
echo ' xmlns="http://www.w3.org/TR/REC-html40">' . "\n";
$this->outputHeader();
$this->outputBody();

echo '</html>' . "\n";
if($filename):
$file_content= ob_get_contents();
$handle = fopen($filename,"w+");
if(!is_writable($filename))
{
echo '<font color=red>该目录无权限!</font><br />';
return false;
}
if(!fwrite($handle,$file_content))
{
echo "<font color=red>DOC $filename 创建失败!</font><br />";
return false;
}
fclose($handle);
ob_end_clean();
endif;
}

/**
* public void addParagraph(string $content, array $inlineStyle = NULL, string $className = 'normalText')
* @param $content: content of the paragraph
* @param $inlineStyle: array of css block properties
* #param $className: class name of any class defined in extra format file
*/
function addParagraph($content, $inlineStyle = NULL, $className = 'normalText'){
$style = '';
if(is_array($inlineStyle)){
foreach($inlineStyle as $key => $value)
$style .= "$key: $value;";
}
$this->documentBuffer .= "<p class=$className" . ($style != '' ? " style='$style'" : '') . ">".($content == '' ? '<o:p></o:p>' : $content)."</p>\n";
}//end addParagraph()

/**
* public void startTable(array $inlineStyle = NULL, string $className = 'normalTable')
* @param $inlineStyle: array of css table properties, property => value
* @param $className: class name of any class defined in extra format file
*/
function startTable($inlineStyle = NULL, $className = 'normalTable'){
$style = 'border-collapse:collapse;border:none;mso-border-alt:solid windowtext '.BORDER_ALT.'pt;';
$style .= 'mso-yfti-tbllook:480;mso-padding-alt:0cm '.PADDING_ALT_RIGHT.'pt 0cm '.PADDING_ALT_LEFT.'pt;';
$style .= 'mso-border-insideh:'.BORDER_INSIDEH.'pt solid windowtext;mso-border-insidev:'.BORDER_INSIDEV.'pt solid windowtext;';
if(is_array($inlineStyle)){
foreach($inlineStyle as $key => $value)
$style .= "$key: $value;";
}
$this->documentBuffer .= "<table class=$className style='$style' border='0' cellspacing='0' cellpadding='0'>\n";

$this->tableIsOpen = true;
}//end startTable()

?>
...全文
54 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦宇飞 2009-07-18
  • 打赏
  • 举报
回复
function output($filename=''){
$this->endSession();
header('Content-Type: application/msword; charset=gb2312');
if(!$filename){
header("Content-Disposition:filename=$filename");

}
echo ' <html xmlns:o="urn:schemas-microsoft-com:office:office"' . "\n";
echo ' xmlns:w="urn:schemas-microsoft-com:office:word"' . "\n";
echo ' xmlns="http://www.w3.org/TR/REC-html40">' . "\n";
$this->outputHeader();
$this->outputBody();

echo ' </html>' . "\n";
if($filename):
$file_content= ob_get_contents();
$handle = fopen($filename,"w+");
if(!is_writable($filename))
{
echo ' <font color=red>该目录无权限! </font> <br />';
return false;
}
if(!fwrite($handle,$file_content))
{
echo " <font color=red>DOC $filename 创建失败! </font> <br />";
return false;
}
fclose($handle);
ob_end_clean();
endif;
}
梦宇飞 2009-07-17
  • 打赏
  • 举报
回复
include("php2doc.class.php");
$doc = new clsMsDocGenerator();
ob_start();
readfile('./demo.html');
$Content = ob_get_contents();

$doc->addParagraph($Content);

$doc->output('demo.doc');
梦宇飞 2009-07-17
  • 打赏
  • 举报
回复

/**
* public int addTableRow(array $cells, array $aligns = NULL, array $vAligns = NULL, array $inlineStyle = NULL, array $classesName = NULL)
* @param $cells: array with content of cells of the row
* @param $aligns: array with align cell constants in html style, a item for each cell item
* @param $vAligns: array with valign cell constants in html style, a item for each cell item
* @param $inlineStyle: array of css block properties, property => value
* @param $classesName: array with class name of any class defined in extra format file, a item for each cell item
*/
function addTableRow($cells, $aligns = NULL, $vAligns = NULL, $inlineStyle = NULL, $classesName = NULL){
if(! $this->tableIsOpen)
die('ERROR: TABLE IS NOT STARTED');

if(is_array($classesName) && count($classesName) != count($cells))
die('ERROR: COUNT OF CLASSES IS DIFERENT OF COUNT OF CELLS');
if(is_array($aligns) && count($aligns) != count($cells))
die('ERROR: COUNT OF ALIGNS IS DIFERENT OF COUNT OF CELLS');
if(is_array($vAligns) && count($vAligns) != count($cells))
die('ERROR: COUNT OF VALIGNS IS DIFERENT OF COUNT OF CELLS');

$style = 'border:solid windowtext 1.0pt;border-left:none;mso-border-left-alt:solid windowtext .5pt;';
$style = 'mso-border-alt: solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;';
if(is_array($inlineStyle)){
foreach($inlineStyle as $key => $value)
$style .= "$key: $value;";
}

$tableWidth = $this->atualPageWidth * Um_Cent;// - ($this->leftMargin * Um_Cent + $this->rightMargin * Um_Cent);
//$tableWidth -= (BORDER_ALT*2 + PADDING_ALT_RIGHT + PADDING_ALT_LEFT + BORDER_INSIDEH*2 + BORDER_INSIDEV*2);
$cellWidth = floor($tableWidth / count($cells));


$this->documentBuffer .= "<tr style='mso-yfti-irow: $this->tableLastRow'>\n";
for($i = 0; $i < count($cells); $i++){
$align = is_array($aligns) ? $aligns[$i] : 'left';
$vAlign = is_array($vAligns) ? $vAligns[$i] : 'top';
$className = is_array($classesName) ? " class=$classesName[$i]" : '';

$this->documentBuffer .= "<td width=$cellWidth align=$align valign=$vAlign style='$style' ".($className != '' ? " class=$className" : '').">$cells[$i]</td>\n";
}
$this->documentBuffer .= "</tr>\n";

$this->tableLastRow++;
return $this->tableLastRow;
}//end addTableHeaderRow()

/**
* void endTable(void)
*/
function endTable(){
$this->documentBuffer .= "</table>\n";

$this->tableIsOpen = false;
$this->tableLastRow = 0;
}//end endTable()

/**
* private void endSession(void)
*/
function endSession(){
$this->documentBuffer .= "</div>\n";
}//end newSession()

/**
* private void prepareDefaultHeader(void)
*/
function prepareDefaultHeader(){
$this->formatBuffer .= 'p.normalText, li.normalText, div.normalText' . "\n";
$this->formatBuffer .= ' {mso-style-parent: "";' . "\n";
$this->formatBuffer .= ' margin: 0cm;' . "\n";
$this->formatBuffer .= ' margin-bottom: 6pt;' . "\n";
$this->formatBuffer .= ' mso-pagination: widow-orphan;' . "\n";
$this->formatBuffer .= ' font-size: 12.0pt;' . "\n";
$this->formatBuffer .= ' font-family: "Arial";' . "\n";
$this->formatBuffer .= ' mso-fareast-font-family: "Arial";}' . "\n";

if($this->cssFile != ''){
if(file_exists($this->cssFile))
$this->formatBuffer .= filegetcontents($this->cssFile);
}
}//end prepareDefaultHeader()

/**
* private void outputHeader(void)
*/
function outputHeader(){
echo '<head>' . "\n";
echo '<meta http-equiv=Content-Type content="text/html; charset=gb2312">' . "\n";
echo '<meta name=ProgId content=Word.Document>' . "\n";
echo '<meta name=Generator content="'.$this->appName.' '.$this->appVersion.'">' . "\n";
echo '<meta name=Originator content="'.$this->appName.' '.$this->appVersion.'">' . "\n";

echo '<style>' . "\n";
echo '<!--' . "\n";
echo '/* Style Definitions */' . "\n";

$this->prepareDefaultHeader();

echo $this->formatBuffer . "\n";

echo '-->' . "\n";
echo '</style>' . "\n";
echo '</head>' . "\n";
}//end outputHeader()

/**
* private void outputBody(void)
*/
function outputBody(){
echo "<body lang=PT-BR style='tab-interval:35.4pt'>\n";

echo $this->documentBuffer . "\n";

echo "</body>\n";
}//end outputBody()
}//end class clsMsDocGenerator


/****************************************************
* constant definition
***************************************************/
define('Um_Cent', 28.35);//1cm = 28.35pt

//paper sizes
define('A4_WIDTH', 21.0);
define('A4_HEIGHT', 29.7);
define('A5_WIDTH', 14.8);
define('A5_HEIGHT', 21.0);
define('LETTER_WIDTH', 21.59);
define('LETTER_HEIGHT', 27.94);
define('OFFICE_WIDTH', 21.59);
define('OFFICE_HEIGHT', 35.56);


//table values
define('BORDER_ALT', 0.5);
define('PADDING_ALT_RIGHT', 5.4);
define('PADDING_ALT_LEFT', 5.4);
define('BORDER_INSIDEH', 0.5);
define('BORDER_INSIDEV', 0.5);

//remove in new version
define('A4_PORTRAIT_SIZE', '21.0cm 842.0pt');
define('A4_LANDSCAPE_SIZE', '842.0pt 21.0cm');
define('A4_PORTRAIT_MARGIN', '70.85pt 3.0cm 70.85pt 3.0cm');
define('A4_LANDSCAPE_MARGIN', '3.0cm 70.85pt 3.0cm 70.85pt');

define('LETTER_PORTRAIT_SIZE', '612.0pt 792.0pt');
define('LETTER_LANDSCAPE_SIZE', '792.0pt 612.0pt');
define('LETTER_PORTRAIT_MARGIN', '70.85pt 3.0cm 70.85pt 3.0cm');
define('LETTER_LANDSCAPE_MARGIN', '3.0cm 70.85pt 3.0cm 70.85pt');

/****************************************************
* function definition
***************************************************/

if(! function_exists('filegetcontents')){
function filegetcontents($filename, $useIncludePath = '', $context = ''){
if(empty($useIncludePath)){
return implode('',file($filename));
}elseif(empty($content)){
return implode('',file($filename, $useIncludePath));
}else{
return implode('',file($filename, $useIncludePath, $content));
}
}//end filegetcontents()
}//end if

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧