?? html2fpdf.php
字號:
<?php
/*
*** General-use version
DEBUG HINT:
- Inside function printbuffer make $fill=1
- Inside function Cell make:
if($fill==1 or $border==1)
{
// if ($fill==1) $op=($border==1) ? 'B' : 'f';
// else $op='S';
$op='S';
- Following these 2 steps you will be able to see the cell's boundaries
WARNING: When adding a new tag support, also add its name inside the function DisableTags()'s very long string
ODDITIES (?):
. It seems like saved['border'] and saved['bgcolor'] are useless inside the FlowingBlock...
These 2 attributes do the same thing?!?:
. $this->currentfont - mine
. $this->CurrentFont - fpdf's
TODO (in the future...):
- Make font-family, font-size, lineheight customizable
- Increase number of HTML/CSS tags/properties, Image/Font Types, recognized/supported
- allow BMP support? (tried with http://phpthumb.sourceforge.net/ but failed)
- Improve CSS support
- support image side-by-side or one-below-another or both?
- Improve code clarity even more (modularize and get better var names like on textbuffer array's indexes for example)
//////////////////////////////////////////////////////////////////////////////
//////////////DO NOT MODIFY THE CONTENTS OF THIS BOX//////////////////////////
//////////////////////////////////////////////////////////////////////////////
// //
// HTML2FPDF is a php script to read a HTML text and generate a PDF file. //
// Copyright (C) 2004-2005 Renato Coelho //
// This script may be distributed as long as the following files are kept //
// together: //
// //
// fpdf.php, html2fpdf.php, gif.php,htmltoolkit.php,license.txt,credits.txt //
// //
//////////////////////////////////////////////////////////////////////////////
Misc. Observations:
- CSS + align = bug! (?)
OBS1: para textos de mais de 1 p醙ina, talvez tenha que juntar varios $texto_artigo
antes de mandar gerar o PDF, para que o PDF gerado seja completo.
OBS2: there are 2 types of spaces 32 and 160 (ascii values)
OBS3: //! is a special comment to be used with source2doc.php, a script I created
in order to generate the doc on the site html2fpdf.sf.net
OBS4: var $LineWidth; // line width in user unit - use this to make css thin/medium/thick work
OBS5: Images and Textareas: when they are inserted you can only type below them (==display:block)
OBS6: Optimized to 'A4' paper (default font: Arial , normal , size 11 )
OBS7: Regexp + Perl ([preg]accepts non-greedy quantifiers while PHP[ereg] does not)
Perl: '/regexp/x' where x == option ( x = i:ignore case , x = s: DOT gets \n as well)
========================END OF INITIAL COMMENTS=================================
*/
define('HTML2FPDF_VERSION','3.0(beta)');
if (!defined('RELATIVE_PATH')) define('RELATIVE_PATH','');
if (!defined('FPDF_FONTPATH')) define('FPDF_FONTPATH','font/');
require_once(RELATIVE_PATH.'fpdf.php');
require_once(RELATIVE_PATH.'htmltoolkit.php');
class HTML2FPDF extends FPDF
{
//internal attributes
var $HREF; //! string
var $pgwidth; //! float
var $fontlist; //! array
var $issetfont; //! bool
var $issetcolor; //! bool
var $titulo; //! string
var $oldx; //! float
var $oldy; //! float
var $B; //! int
var $U; //! int
var $I; //! int
var $tablestart; //! bool
var $tdbegin; //! bool
var $table; //! array
var $cell; //! array
var $col; //! int
var $row; //! int
var $divbegin; //! bool
var $divalign; //! char
var $divwidth; //! float
var $divheight; //! float
var $divbgcolor; //! bool
var $divcolor; //! bool
var $divborder; //! int
var $divrevert; //! bool
var $listlvl; //! int
var $listnum; //! int
var $listtype; //! string
//array(lvl,# of occurrences)
var $listoccur; //! array
//array(lvl,occurrence,type,maxnum)
var $listlist; //! array
//array(lvl,num,content,type)
var $listitem; //! array
var $buffer_on; //! bool
var $pbegin; //! bool
var $pjustfinished; //! bool
var $blockjustfinished; //! bool
var $SUP; //! bool
var $SUB; //! bool
var $toupper; //! bool
var $tolower; //! bool
var $dash_on; //! bool
var $dotted_on; //! bool
var $strike; //! bool
var $CSS; //! array
var $cssbegin; //! bool
var $backupcss; //! array
var $textbuffer; //! array
var $currentstyle; //! string
var $currentfont; //! string
var $colorarray; //! array
var $bgcolorarray; //! array
var $internallink; //! array
var $enabledtags; //! string
var $lineheight; //! int
var $basepath; //! string
// array('COLOR','WIDTH','OLDWIDTH')
var $outlineparam; //! array
var $outline_on; //! bool
var $specialcontent; //! string
var $selectoption; //! array
//options attributes
var $usecss; //! bool
var $usepre; //! bool
var $usetableheader; //! bool
var $shownoimg; //! bool
function HTML2FPDF($orientation='P',$unit='mm',$format='A4')
{
//! @desc Constructor
//! @return An object (a class instance)
//Call parent constructor
$this->FPDF($orientation,$unit,$format);
//To make the function Footer() work properly
$this->AliasNbPages();
//Enable all tags as default
$this->DisableTags();
//Set default display preferences
$this->DisplayPreferences('');
//Initialization of the attributes
$this->SetFont('Arial','',11); // Changeable?(not yet...)
$this->lineheight = 5; // Related to FontSizePt == 11
$this->pgwidth = $this->fw - $this->lMargin - $this->rMargin ;
$this->SetFillColor(255);
$this->HREF='';
$this->titulo='';
$this->oldx=-1;
$this->oldy=-1;
$this->B=0;
$this->U=0;
$this->I=0;
$this->listlvl=0;
$this->listnum=0;
$this->listtype='';
$this->listoccur=array();
$this->listlist=array();
$this->listitem=array();
$this->tablestart=false;
$this->tdbegin=false;
$this->table=array();
$this->cell=array();
$this->col=-1;
$this->row=-1;
$this->divbegin=false;
$this->divalign="L";
$this->divwidth=0;
$this->divheight=0;
$this->divbgcolor=false;
$this->divcolor=false;
$this->divborder=0;
$this->divrevert=false;
$this->fontlist=array("arial","times","courier","helvetica","symbol","monospace","serif","sans");
$this->issetfont=false;
$this->issetcolor=false;
$this->pbegin=false;
$this->pjustfinished=false;
$this->blockjustfinished = true; //in order to eliminate exceeding left-side spaces
$this->toupper=false;
$this->tolower=false;
$this->dash_on=false;
$this->dotted_on=false;
$this->SUP=false;
$this->SUB=false;
$this->buffer_on=false;
$this->strike=false;
$this->currentfont='';
$this->currentstyle='';
$this->colorarray=array();
$this->bgcolorarray=array();
$this->cssbegin=false;
$this->textbuffer=array();
$this->CSS=array();
$this->backupcss=array();
$this->internallink=array();
$this->basepath = "";
$this->outlineparam = array();
$this->outline_on = false;
$this->specialcontent = '';
$this->selectoption = array();
$this->shownoimg=false;
$this->usetableheader=false;
$this->usecss=true;
$this->usepre=true;
}
function setBasePath($str)
{
//! @desc Inform the script where the html file is (full path - e.g. http://www.google.com/dir1/dir2/dir3/file.html ) in order to adjust HREF and SRC links. No-Parameter: The directory where this script is.
//! @return void
$this->basepath = dirname($str) . "/";
$this->basepath = str_replace("\\","/",$this->basepath); //If on Windows
}
function ShowNOIMG_GIF($opt=true)
{
//! @desc Enable/Disable Displaying the no_img.gif when an image is not found. No-Parameter: Enable
//! @return void
$this->shownoimg=$opt;
}
function UseCSS($opt=true)
{
//! @desc Enable/Disable CSS recognition. No-Parameter: Enable
//! @return void
$this->usecss=$opt;
}
function UseTableHeader($opt=true)
{
//! @desc Enable/Disable Table Header to appear every new page. No-Parameter: Enable
//! @return void
$this->usetableheader=$opt;
}
function UsePRE($opt=true)
{
//! @desc Enable/Disable pre tag recognition. No-Parameter: Enable
//! @return void
$this->usepre=$opt;
}
//Page header
function Header($content='')
{
//! @return void
//! @desc The header is printed in every page.
if($this->usetableheader and $content != '')
{
$y = $this->y;
foreach($content as $tableheader)
{
$this->y = $y;
//Set some cell values
$x = $tableheader['x'];
$w = $tableheader['w'];
$h = $tableheader['h'];
$va = $tableheader['va'];
$mih = $tableheader['mih'];
$fill = $tableheader['bgcolor'];
$border = $tableheader['border'];
$align = $tableheader['a'];
//Align
$this->divalign=$align;
$this->x = $x;
//Vertical align
if (!isset($va) || $va=='M') $this->y += ($h-$mih)/2;
elseif (isset($va) && $va=='B') $this->y += $h-$mih;
if ($fill)
{
$color = ConvertColor($fill);
$this->SetFillColor($color['R'],$color['G'],$color['B']);
$this->Rect($x, $y, $w, $h, 'F');
}
//Border
if (isset($border) and $border != 'all') $this->_tableRect($x, $y, $w, $h, $border);
elseif (isset($border) && $border == 'all') $this->Rect($x, $y, $w, $h);
//Print cell content
$this->divwidth = $w-2;
$this->divheight = 1.1*$this->lineheight;
$textbuffer = $tableheader['textbuffer'];
if (!empty($textbuffer)) $this->printbuffer($textbuffer,false,true/*inside a table*/);
$textbuffer = array();
}
$this->y = $y + $h; //Update y coordinate
}//end of 'if usetableheader ...'
}
//Page footer
function Footer()
{
//! @return void
//! @desc The footer is printed in every page!
//Position at 1.0 cm from bottom
$this->SetY(-10);
//Copyright //especial para esta vers鉶
$this->SetFont('Arial','B',9);
$this->SetTextColor(0);
//Arial italic 9
$this->SetFont('Arial','I',9);
//Page number
$this->Cell(0,10,$this->PageNo().'/{nb}',0,0,'C');
//Return Font to normal
$this->SetFont('Arial','',11);
}
///////////////////
/// HTML parser ///
///////////////////
function WriteHTML($html)
{
//! @desc HTML parser
//! @return void
/* $e == content */
$this->ReadMetaTags($html);
$html = AdjustHTML($html,$this->usepre); //Try to make HTML look more like XHTML
if ($this->usecss) $html = $this->ReadCSS($html);
//Add new supported tags in the DisableTags function
$html=str_replace('<?php ','< ',$html); //Fix '<?php XML' bug from HTML code generated by MS Word
$html=strip_tags($html,$this->enabledtags); //remove all unsupported tags, but the ones inside the 'enabledtags' string
//Explode the string in order to parse the HTML code
$a=preg_split('/<(.*?)>/ms',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach($a as $i => $e)
{
if($i%2==0)
{
//TEXT
//Adjust lineheight
// $this->lineheight = (5*$this->FontSizePt)/11; //should be inside printbuffer?
//Adjust text, if needed
if (strpos($e,"&") !== false) //HTML-ENTITIES decoding
{
if (strpos($e,"#") !== false) $e = value_entity_decode($e); // Decode value entities
//Avoid crashing the script on PHP 4.0
$version = phpversion();
$version = str_replace('.','',$version);
if ($version >= 430) $e = html_entity_decode($e,ENT_QUOTES,'cp1252'); // changes and the like by their respective char
else $e = lesser_entity_decode($e);
}
$e = str_replace(chr(160),chr(32),$e); //unify ascii code of spaces (in order to recognize all of them correctly)
if (strlen($e) == 0) continue;
if ($this->divrevert) $e = strrev($e);
if ($this->toupper) $e = strtoupper($e);
if ($this->tolower) $e = strtolower($e);
//Start of 'if/elseif's
if($this->titulo) $this->SetTitle($e);
elseif($this->specialcontent)
{
if ($this->specialcontent == "type=select" and $this->selectoption['ACTIVE'] == true) //SELECT tag (form element)
{
$stringwidth = $this->GetStringWidth($e);
if (!isset($this->selectoption['MAXWIDTH']) or $stringwidth > $this->selectoption['MAXWIDTH']) $this->selectoption['MAXWIDTH'] = $stringwidth;
if (!isset($this->selectoption['SELECTED']) or $this->selectoption['SELECTED'] == '') $this->selectoption['SELECTED'] = $e;
}
else $this->textbuffer[] = array("護
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -