【问题标题】:PDFlib PHP place SVG into table cellPDFlib PHP 将 SVG 放入表格单元格
【发布时间】:2021-10-26 15:07:32
【问题描述】:

我想创建一个表格,其中每个项目旁边都有一个连字符,它以 svg 图形的形式提供。我试图将其加载到类似于图像的表格单元格中,但这似乎不起作用。我在 PDFlib Cookbok 中也找不到与此相关的任何内容。

然后我尝试将图形放在表格之外,但现在我遇到了问题,因为它没有连接到表格,如果表格太大,连字符不会转到新页面当前一个并将其余内容加载到该新页面上。所以这似乎也不起作用。有谁知道如何在表格内加载 svg 图形的解决方案,以便它与它的内容一起呈现(这意味着它们也会进入新页面)?

这是我尝试将 svg 加载到表中的代码:

## add hyphen to table ##
$image = $p->load_graphic("auto", $hyphen, "");
if ($image == 0) {
    echo("Couldn't load $image: " . $p->get_errmsg());
    exit(1);
}

 $optlistHyphen = "colwidth=2% margintop=3 graphic=" . $image;

$tbl = $p->add_table_cell($tbl, $col1, $row, '', $optlistHyphen);
if ($tbl == 0) {
    echo("Error: " . $p->get_errmsg());
    exit(1);
}

这是我当前尝试的代码,我将图形放在表格之外:

## add hyphen ##
// load hyphen
$graphics = $p->load_graphics('auto', $hyphen, '');
if ($graphics == 0) {
    echo('Couldn not load logo image: ' . $p->get_errmsg());
    exit(1);
}

// place hyphen
$buf = "scale=0.25";
$p->fit_graphics($graphics, $hyphenX, $hyphenY, $buf);

// calculate new height for hyphen
$hyphenY = $hyphenY - 16;

// Add new empty table cell
$optlistTableCellHyphen = "colwidth=2%";

$tbl = $p->add_table_cell($tbl, $col1, $row, '', $optlistTableCellHyphen);
if ($tbl == 0) {
    echo("Error: " . $p->get_errmsg());
    exit(1);
}

(这两个代码 sn-ps 都在表内容的 foreach 内)

这里还有一张带有最后一个“解决方案”的当前输出的图像:

【问题讨论】:

    标签: php pdf pdflib


    【解决方案1】:

    此外,从您的解释中,我不清楚为什么将 SVG 文件放在表格单元格中不起作用。使用 fitgraphics 选项来定义图形的大小和位置可能很重要。 (另请参见 PDFlib 9.3.1 API 参考,第 5.3 章,表 5.16。)。

    <?php
    try {
        $searchpath = "../data";
        $p = new PDFlib();
        $tbl = 0;
    
        # This means we must not check return values of load_font() etc.
        $p->set_option("errorpolicy=exception stringformat=utf8");
        /* Set the search path for font files */
        $p->set_option("SearchPath={{" . $searchpath . "}}");
    
        /*  open new PDF file; insert a file name to create the PDF on disk */
        $p->begin_document("", "");
        $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
        $graphics = $p->load_graphics('auto', 'PDFlib-logo.svg', '');
    
        for ($i=1; $i < 10; $i++){
        $tf = $p->create_textflow('Feature ' . $i . '<nextline>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna ', 'fontname=NotoSerif-Regular encoding=unicode fontsize=12');
        $tbl = $p->add_table_cell($tbl, 1, $i, '', 'colwidth=30 graphics=' . $graphics . ' fitgraphics={position={center top}} margin=3');
        $tbl = $p->add_table_cell($tbl, 2, $i, '', 'colwidth=250 textflow=' . $tf . ' rowheight=10');
        }
    
        $p->fit_table($tbl, 50, 50, 545, 792, "stroke = {line=horother}");
     
        $p->end_page_ext("");
        $p->end_document("");
    
        $buf = $p->get_buffer();
        $len = strlen($buf);
    
        header("Content-type: application/pdf");
        header("Content-Length: $len");
        header("Content-Disposition: inline; filename=sample.pdf");
        print $buf;
    
    }
    catch (PDFlibException $e) {
        die("PDFlib exception occurred in this sample:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "\n");
    }
    catch (Throwable $e) {
        die("PHP exception occurred: " . $e->getMessage() . "\n");
    }
    
    $p = 0;
    ?>
    

    我整理了一个非常简单的示例,该示例仅使用 PDFlib 包中的示例资源。为了使代码更短,我设置了errorpolicy=exception

    此代码生成的输出应该与您要查找的内容非常接近。 (这里只有一页,但你可能已经看到了你的实现的不同)

    如果要将图形放置在表格之外,在放置表格后使用 info_table() 获取垂直线的 Y 位置可能会很有用(请参阅 PDFlib 9.3.1 API 参考,第 5.3 章,表格5.19)。 这使您可以准确地确定图形的放置位置,即使它继续在另一个页面(实例)上。

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多