【问题标题】:FPDF WriteHTML method - how to reduce line-height of <p> tagFPDF WriteHTML 方法 - 如何减少 <p> 标签的行高
【发布时间】:2018-05-04 07:41:02
【问题描述】:

我遇到了困难,希望您能提供帮助。

我正在改进现有 Web 应用程序的部分内容。目前我正在从 php 表单中生成 PDF。

我正在使用 FPDF 库,但首先它是非常静态的(多单元格没有自动换行符等)。现在请求进来呈现 HTML。

由于几乎整个 PDF 都能正常工作,我宁愿不切换到另一个库,所以我使用了 FPDF 的 WriteHTML 扩展,如下所述: http://www.fpdf.org/en/script/script42.php

一切正常,但当用户输入 PDF 数据(发票行)时,他是通过所见即所得编辑器进行的。

现在,如果用户点击“return”键,它将用 HTML 换行

标签。

例如: “测试一 测试 2"

呈现为:

<p>Test 1</p><p>Test 2</p>.

到目前为止,所有输出都很好,因为它是正确的。但是,行高、边距、填充或其他任何东西都太高了,所以 2 行之间的 linbreak 太多了。我无法以任何方式减少它。

我已附上屏幕截图以进一步解释。

下面的代码显示了我用于 FPDF 以使用 WriteHTML 方法的小插件。由于这种方法生成的内容的换行符只有那么高。使用 Text() 方法或 cell() 时,行高不是那么高,因为我使用 GetY()、SetY() 方法来确定行的开头,所以我很确定我需要在以下提供的代码中更改某些内容,但我不确定是什么。

<?php
//HTML2PDF by Clément Lavoillotte
//ac.lavoillotte@noos.fr
//webmaster@streetpc.tk
//http://www.streetpc.tk

require('fpdf.php');

//function hex2dec
//returns an associative array (keys: R,G,B) from
//a hex html code (e.g. #3FE5AA)
function hex2dec($couleur = "#000000"){
    $R = substr($couleur, 1, 2);
    $rouge = hexdec($R);
    $V = substr($couleur, 3, 2);
    $vert = hexdec($V);
    $B = substr($couleur, 5, 2);
    $bleu = hexdec($B);
    $tbl_couleur = array();
    $tbl_couleur['R']=$rouge;
    $tbl_couleur['V']=$vert;
    $tbl_couleur['B']=$bleu;
    return $tbl_couleur;
}

//conversion pixel -> millimeter at 72 dpi
function px2mm($px){
    return $px*25.4/72;
}

function txtentities($html){
    $trans = get_html_translation_table(HTML_ENTITIES);
    $trans = array_flip($trans);
    return strtr($html, $trans);
}
////////////////////////////////////

class PDF_HTML extends FPDF
{
//variables of html parser
protected $B;
protected $I;
protected $U;
protected $HREF;
protected $fontList;
protected $issetfont;
protected $issetcolor;

function __construct($orientation='P', $unit='mm', $format='A4')
{
    //Call parent constructor
    parent::__construct($orientation,$unit,$format);
    //Initialization
    $this->B=0;
    $this->I=0;
    $this->U=0;
    $this->HREF='';
    $this->fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol');
    $this->issetfont=false;
    $this->issetcolor=false;
}

function WriteHTML($html)
{
    //HTML parser
    $html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote>"); //supprime tous les tags sauf ceux reconnus
    $html=str_replace("\n",' ',$html); //remplace retour à la ligne par un espace
    $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //éclate la chaîne avec les balises
    foreach($a as $i=>$e)
    {
        if($i%2==0)
        {
            //Text
            if($this->HREF)
                $this->PutLink($this->HREF,$e);
            else
                $this->Write(5,stripslashes(txtentities($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(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
                        $attr[strtoupper($a3[1])]=$a3[2];
                }
                $this->OpenTag($tag,$attr);
            }
        }
    }
}

function OpenTag($tag, $attr)
{
    //Opening tag
    switch($tag){
        case 'STRONG':
            $this->SetStyle('B',true);
            break;
        case 'EM':
            $this->SetStyle('I',true);
            break;
        case 'B':
        case 'I':
        case 'U':
            $this->SetStyle($tag,true);
            break;
        case 'A':
            $this->HREF=$attr['HREF'];
            break;
        case 'IMG':
            if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) {
                if(!isset($attr['WIDTH']))
                    $attr['WIDTH'] = 0;
                if(!isset($attr['HEIGHT']))
                    $attr['HEIGHT'] = 0;
                $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
            }
            break;
        case 'TR':
        case 'BLOCKQUOTE':
        case 'BR':
            $this->Ln(5);
            break;
        case 'P':
            $this->Ln(10);
            break;
        case 'FONT':
            if (isset($attr['COLOR']) && $attr['COLOR']!='') {
                $coul=hex2dec($attr['COLOR']);
                $this->SetTextColor($coul['R'],$coul['V'],$coul['B']);
                $this->issetcolor=true;
            }
            if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) {
                $this->SetFont(strtolower($attr['FACE']));
                $this->issetfont=true;
            }
            break;
    }
}

function CloseTag($tag)
{
    //Closing tag
    if($tag=='STRONG')
        $tag='B';
    if($tag=='EM')
        $tag='I';
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,false);
    if($tag=='A')
        $this->HREF='';
    if($tag=='FONT'){
        if ($this->issetcolor==true) {
            $this->SetTextColor(0);
        }
        if ($this->issetfont) {
            $this->SetFont('arial');
            $this->issetfont=false;
        }
    }
}

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);
}

}//end of class
?>

我通过

调用它
$pdf->WriteHTML($value['description']);

$value['description'] = <p>First line</p><p>New Line</p>

有人可以帮助我如何减少这些行之间的换行高度吗?感谢任何帮助。

谢谢

【问题讨论】:

  • 不要使用p标签:/str_replace(['&lt;p&gt;', '&lt;/p&gt;'], ['', '&lt;br&gt;'], $value['description'])

标签: php html regex pdf fpdf


【解决方案1】:

Lawrence Cherone 的评论让我走上了正确的道路。

我已经替换了开口

标记为空白 '',并用
标记替换了结束

标记。

此外,我再次将 html 输出的最后一个
替换为空白 '' - 这只是一个特定的客户端请求)

$desc = $value['description'];
$desc = str_replace('<p>', '', $desc);
$desc = str_replace('</p>', '<br>', $desc);
$desc = str_replace('<br />', '', $desc);

$checkbreak = substr($desc, -4);
if ($checkbreak == '<br>')
    $desc = substr_replace($desc,'',-4);
$pdf->WriteHTML($desc);

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2012-03-17
    • 2013-03-06
    • 1970-01-01
    • 2023-02-14
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多