我已经在db表中存储了html数据,因为文本看起来像这样,通过html WYSIWYG编辑器设置样式。
<ul>
<li><span style="background-color:rgb(255, 255, 255); color:rgb(35, 64, 70); font-family:lucida grande,lucida sans unicode,helvetica,arial,verdana,sans-serif; font-size:13px">Static Website</span></li>
<li><span style="background-color:rgb(255, 255, 255); color:rgb(35, 64, 70); font-family:lucida grande,lucida sans unicode,helvetica,arial,verdana,sans-serif; font-size:13px">Number of Pages-20</span></li></ul>
一般来说,它会像
一样正确显示$row['description'];
但是在fpdf中它很随意,就像这样
<ul>
<li><span
style="background-colo
r:rgb(255, 255, 255);
color:rgb(35, 64, 70);
font-family:lucida
grande,lucida sans
unicode,helvetica,arial,
verdana,sans-serif;
font-size:13px">Static
Website</span></li>
<li><span
style="background-colo
r:rgb(255, 255, 255);
color:rgb(35, 64, 70);
font-family:lucida
grande,lucida sans
unicode,helvetica,arial,
我尝试了htmlspecialchars()
,htmlentities()
,mysqli_real_escape_string
....
但它毫无用处。
我的fpdf文件是
invoice.php文件是
class PDF_Invoice extends FPDF
{
// private variables
var $colonnes;
var $format;
var $angle=0;
var $B;
var $I;
var $U;
var $HREF;
function PDF($orientation='P',$unit='mm',$format='A4')
{
//Call parent constructor
$this->FPDF($orientation,$unit,$format);
//Initialization
$this->B=0;
$this->I=0;
$this->U=0;
$this->HREF='';
}
function WriteHTML($html, $bi)
{
//HTML parser
$html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote><hr><td><tr><table><sup>"); //remove all unsupported tags
$html=str_replace("\n",'',$html);
$html=str_replace(" ",'',$html); //replace carriage returns by spaces
$html=str_replace("\t",'',$html); //replace carriage returns by spaces
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach($a as $i=>$e)
{
if($i%2==0)
{
//Text
if($this->HREF)
$this->PutLink($this->HREF,$e);
else
$this->Write(5,$e);
}
else
{
//Tag
if($e{0}=='/')
$this->CloseTag(strtoupper(substr($e,1)));
else
{
//Extract attributes
$a2=explode(' ',$e);
$tag=strtoupper(array_shift($a2));
$attr=array();
foreach($a2 as $v)
if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
$attr[strtoupper($a3[1])]=$a3[2];
$this->OpenTag($tag,$attr);
}
}
}
}
function OpenTag($tag,$attr)
{
//Opening tag
if($tag=='B' or $tag=='I' or $tag=='U')
$this->SetStyle($tag,true);
if($tag=='A')
$this->HREF=$attr['HREF'];
if($tag=='BR')
$this->Ln(10);
}
function CloseTag($tag)
{
//Closing tag
if($tag=='B' or $tag=='I' or $tag=='U')
$this->SetStyle($tag,false);
if($tag=='A')
$this->HREF='';
}
function SetStyle($tag,$enable)
{
//Modify style and select corresponding font
$this->$tag+=($enable ? 1 : -1);
$style='';
foreach(array('B','I','U') as $s)
if($this->$s>0)
$style.=$s;
$this->SetFont('',$style);
}
function PutLink($URL,$txt)
{
//Put a hyperlink
$this->SetTextColor(0,0,255);
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
// other functions
主档
<?php
// (c) Xavier Nicolay
// Exemple de génération de devis/facture PDF
$id= $_GET['order_id'];
include('../connect.php');
include('../admin_auth.php');
require('quote_fp.php');
$pdf = new PDF_Invoice( 'P', 'mm', 'A4' );
$pdf->AddPage();
$cols=array( "HSN/SAC" => 15,
"Item Code" => 15,
"Description" => 30,
"Price" => 15,
"Qty" => 13,
);
$pdf->addCols( $cols);
$cols=array( "HSN/SAC" => "L",
"Item Code" => "L",
"Description" => "L",
"Price" => "L",
"Qty" => "L",
);
// php codes
// $query ="";
while($row = mysqli_fetch_array($query))
{
$line = array( "HSN/SAC" => "".$row['hsn_sac']."",
"Item Code" => "".$row['item']."",
"Description" => "".$row['description']."",
"Price" => "".$row['selling_price']."",
"Qty" => "".$row['quantity']."",
);
}
有人可以指导我吗?
答案 0 :(得分:0)
你的示例代码不是很完整,但按照你所拥有的,你的主要php文件可能看起来像这样
<?php
/*
// (c) Xavier Nicolay
// Exemple de génération de devis/facture PDF
$id = $_GET['order_id'];
include('../connect.php');
include('../admin_auth.php');
require('quote_fp.php');
$pdf = new PDF_Invoice('P', 'mm', 'A4');
$pdf->AddPage();
$cols = array("HSN/SAC" => 15,
"Item Code" => 15,
"Description" => 30,
"Price" => 15,
"Qty" => 13,
);
$pdf->addCols($cols);
$cols = array("HSN/SAC" => "L",
"Item Code" => "L",
"Description" => "L",
"Price" => "L",
"Qty" => "L",
);
// php codes
// $query ="";
while ($row = mysqli_fetch_array($query)) {
$line = array("HSN/SAC" => "" . $row['hsn_sac'] . "",
"Item Code" => "" . $row['item'] . "",
"Description" => "" . $row['description'] . "",
"Price" => "" . $row['selling_price'] . "",
"Qty" => "" . $row['quantity'] . "",
);
}
*/
include_once 'invoice.php';
$pdf = new PDF_Invoice('P', 'mm', 'A4');
$pdf->AddPage();
$pdf->setFont("helvetica");
$html='<ul>
<li><span style="background-color:rgb(255, 255, 255); color:rgb(35, 64, 70); font-family:lucida grande,lucida sans unicode,helvetica,arial,verdana,sans-serif; font-size:13px">Static Website</span></li>
<li><span style="background-color:rgb(255, 255, 255); color:rgb(35, 64, 70); font-family:lucida grande,lucida sans unicode,helvetica,arial,verdana,sans-serif; font-size:13px">Number of Pages-20</span></li></ul>'
;
$pdf->writeHTML($html);
$pdf->Output("D");
我已经对您之前的代码进行了评论,因为它会阻止我的代码工作。
您的invoice.php如下所示
<?php
require_once 'fpdf.php';
class PDF_Invoice extends FPDF {
// private variables
var $colonnes;
var $format;
var $angle = 0;
var $B;
var $I;
var $U;
var $HREF;
function __construct($orientation = 'P', $unit = 'mm', $format = 'A4') {
parent::__construct($orientation, $unit, $format);
$this->B = 0;
$this->I = 0;
$this->U = 0;
$this->HREF = '';
}
function WriteHTML($html) {
//HTML parser
$html = strip_tags($html, "<b><u><i><a><img><p><br><strong><em><font><tr><blockquote><hr><td><tr><table><sup>"); //remove all unsupported tags
$html = str_replace("\n", '', $html);
$html = str_replace(" ", '', $html); //replace carriage returns by spaces
$html = str_replace("\t", '', $html); //replace carriage returns by spaces
$a = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($a as $i => $e) {
if ($i % 2 == 0) {
//Text
if ($this->HREF)
$this->PutLink($this->HREF, $e);
else
$this->Write(5, $e);
}
else {
//Tag
if ($e{0} == '/')
$this->CloseTag(strtoupper(substr($e, 1)));
else {
//Extract attributes
$a2 = explode(' ', $e);
$tag = strtoupper(array_shift($a2));
$attr = array();
foreach ($a2 as $v)
if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$', $v, $a3))
$attr[strtoupper($a3[1])] = $a3[2];
$this->OpenTag($tag, $attr);
}
}
}
}
function OpenTag($tag, $attr) {
//Opening tag
if ($tag == 'B' or $tag == 'I' or $tag == 'U')
$this->SetStyle($tag, true);
if ($tag == 'A')
$this->HREF = $attr['HREF'];
if ($tag == 'BR')
$this->Ln(10);
}
function CloseTag($tag) {
//Closing tag
if ($tag == 'B' or $tag == 'I' or $tag == 'U')
$this->SetStyle($tag, false);
if ($tag == 'A')
$this->HREF = '';
}
function SetStyle($tag, $enable) {
//Modify style and select corresponding font
$this->$tag += ($enable ? 1 : -1);
$style = '';
foreach (array('B', 'I', 'U') as $s)
if ($this->$s > 0)
$style .= $s;
$this->SetFont('', $style);
}
function PutLink($URL, $txt) {
//Put a hyperlink
$this->SetTextColor(0, 0, 255);
$this->SetStyle('U', true);
$this->Write(5, $txt, $URL);
$this->SetStyle('U', false);
$this->SetTextColor(0);
}
}