【问题标题】:Position h1,h2,h3 and other tags with TCPDF使用 TCPDF 定位 h1、h2、h3 和其他标签
【发布时间】:2023-03-06 19:16:02
【问题描述】:

我正在尝试使用 HTML 代码使用 TCPDF 制作 PDF 文档。

目前我使用此代码:

// set font
$pdf->SetFont('dejavusans', '', 36);

// add a page
$pdf->AddPage();

$html = '
<style>
.h1 {
color: #2B6999;
font-weight: normal;
}

</style>

<h1 class="h1">Test</h1>

';

// output the HTML content
$pdf->writeHTML($html, true, false, true, false, 'C');

如何定位此文本?我不能在标签 margin-top 等之间使用。

谁能帮我解决这个问题?

【问题讨论】:

    标签: php html css pdf tcpdf


    【解决方案1】:

    您可以添加如下内容:

    $tagvs = array('h1' => array(0 => array('h' => 1, 'n' => 3), 1 => array('h' => 1, 'n' => 2)),
                   'h2' => array(0 => array('h' => 1, 'n' => 2), 1 => array('h' => 1, 'n' => 1)));
    $pdf->setHtmlVSpace($tagvs);
    

    这是来自文档/示例的格式描述:

    文件:tcppdf.php:

    /**
     * Set the vertical spaces for HTML tags.
     * The array must have the following structure (example):
     * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
     * The first array level contains the tag names,
     * the second level contains 0 for opening tags or 1 for closing tags,
     * the third level contains the vertical space unit (h) and the number spaces to add (n).
     * If the h parameter is not specified, default values are used.
     * @param $tagvs (array) array of tags and relative vertical spaces.
     * @public
     * @since 4.2.001 (2008-10-30)
     */
    

    文件http://www.tcpdf.org/examples/example_061.phps

    // REMOVE TAG TOP AND BOTTOM MARGINS
    //
    // $tagvs = array('p' => array(0 => array('h' => 0, 'n' => 0), 1 => array('h' => 0, 'n' => 0)));
    // $pdf->setHtmlVSpace($tagvs);
    // 
    // Since the CSS margin command is not yet implemented on TCPDF, you
    // need to set the spacing of block tags using the above method.
    

    【讨论】:

      【解决方案2】:

      您正在使用完全输出 HTML 的 writeHTML,您需要使用“$pdf-&gt;Cell”函数。这里有很多例子http://www.tcpdf.org/examples.php

      【讨论】:

      • 如何设置单元格的边距或 x/y 位置?我找到了这一行: $pdf->Cell(0, 110, 'TEST CELL STRETCH: no stretch', 0, 1, '', 0, '', 0);
      • 查看此示例:使用 Multicell() 进行复杂对齐
      猜你喜欢
      • 2014-07-09
      • 2011-01-13
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2014-07-07
      相关资源
      最近更新 更多