Php 在fpdf中显示带有存储在数据库中的样式的html标记

Php 在fpdf中显示带有存储在数据库中的样式的html标记,php,mysql,fpdf,Php,Mysql,Fpdf,我将html数据存储在db表中,文本如下所示,通过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">Sta

我将html数据存储在db表中,文本如下所示,通过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>
但在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,
<?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");
主文件

<?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']."",
);
}

您的示例代码不太完整,但根据您现有的情况,您的主php文件可能如下所示

<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,
<?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");

<?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("&nbsp;", '', $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);
    }

}