首页 文章

如何使用fpdf和pdf_label将动态生成的qrcodes添加到pdf中

提问于
浏览
0

我有一些记录存储在数据库中 . 我想将它们打印到一些Avery标签上 . 我可以使用FPDF来创建pdf文档 . 我可以使用扩展FPDF的PDF_Label来轻松定位信息以与Avery标签对齐 . 到现在为止还挺好 .

除了输出文本,我想在qr代码中表示一些数据 . 我可以创建qr代码,将它们临时存储在磁盘上作为.png文件 . 我可以将qr_codes作为图像插入到.pdf文档中 . 到现在为止还挺好 .

但问题在于:PDF_label创建的“单元格”与Avery标签对齐,专门处理文本,而不是图像 . 如何“轻松”将每个qr代码插入每个标签?

我已经在网上看了两天,但似乎很少有这方面的指导 - 除了一条建议,不要提示不要将图像插入FPDF细胞 .

一种选择可能是使用TCPDF,但我会很感激有关扩展的一些建议,如果有用的话 .

为了记录,到目前为止我的代码是:

<?php
    require_once( 'vendor/fpdf181/fpdf.php' );
    require_once( 'vendor/fpdf_label/PDF_Label.php');
    require_once( 'vendor/phpqrcode/qrlib.php' );

   // fetch data from database
   /* … works fine and creates an array of locations … */

   foreach( $locations as $location )
   {
        $title          = "Location " . $location['id'];
        $name           = $location['name'];
        $footer         = "https://www.example.com/something/";
        $barcode_text   = "https://www.example.com/something/?t=1&u=1&l=" . $location['id'];

        // create a temporary file to hold the barcode.png
        $filePath       = './qr_codes/temp_'. $location['id'] . '.png';

        $qr_code        = new QRcode();
        QRcode::png( $barcode_text, $filePath );



        $text  = "";
        $text .= sprintf( $title . "\n" . $name . "\n");
        $text .= $pdf->Image( $filePath, $pdf->GetX(), $pdf->GetY(), 20 );
        $text .= sprintf( "\n" . $footer );


        //$pdf->Add_Label($text);
        $pdf->Add_Label($text);
}

从7条记录中,这在pdf上只生成3个qrcodes . 可能有几个图像相互叠加 . qr_codes垂直对齐每行标签的顶部,但未正确水平放置 . 大概这是因为这是GetX和GetY在调用这些方法时给出的位置 .

任何帮助非常感谢...

1 回答

相关问题