Thứ Sáu, 11 tháng 3, 2022

Chèn ảnh vào PDF bằng FPDI Laravel PHP

 

Dùng thư viện setasign\Fpdi

        // Set source PDF file
        $pdf = new Fpdi();
        $pagecount = $pdf->setSourceFile($path['fullPath']);

        // Add watermark to PDF pages
        for ($i = 1; $i <= $pagecount; $i++) {
            $tpl = $pdf->importPage($i);
            $size = $pdf->getTemplateSize($tpl);
            $pdf->addPage();
            $pdf->useTemplate($tpl, 0, 0, $size['width'], $size['height'], TRUE);

            //Put the watermark
            $xxx_final = ($size['width'] - 60); // lấy độ dài 60 từ bên phải
            $pdf->Image($image, $xxx_final, 2, 0, 0, 'png');
        }
        unlink($image); // xóa ảnh vừa tạo ở textToImage

        $pdf->Output();


            $fpdi = new Fpdi();
            $count = $fpdi->setSourceFile(StreamReader::createByString($fileContent));
            for ($i = 1; $i <= $count; $i++) {
                $template = $fpdi->importPage($i);
                $size = $fpdi->getTemplateSize($template);
                $fpdi->AddPage($size['orientation'], array($size['width'], $size['height']));
                $fpdi->useTemplate($template);
                $fpdi->Image($image, 10, $size['height']-30, 100, 30, '', '');
            }

            unlink($image);
            $this->createIfNotDir($path['attachPath'], $path['name']);   // Tạo thư mục
            if (file_exists($path['fullPath'])) {
                unlink($path['fullPath']);    // xóa file cũ
            }

            $fpdi->Output($path['fullPath'], 'F');



Không có nhận xét nào:

Đăng nhận xét