Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anon160124's avatar

Add text to a PDF file PHP

I get responses api which contains PDF as byte array. I decode this pdf file and save it there is no problem. The problem is that I would need to print my own text in that file. If anyone can help, I will be grateful. well thank you

        if ($response == true && count(json_decode($response)->PrintLabelsErrorList) == 0 && count(json_decode($response)->Labels) > 0) {
            //Label(s) saving:

            $pdf = implode(array_map('chr', json_decode($response)->Labels));

            $fileName = 'labels-' . time() . '.pdf';

            $status = [
                'success' => true,
                'url'     => $fileName,
            ];

            header('Content-Type: application/pdf');
            header("Content-Transfer-Encoding: Binary");
            header("Content-disposition: attachment; filename=" . $fileName);

            echo $pdf;
            die();
            $text = 'Reklama';
            file_put_contents(__DIR__ . '/../labels/' . $fileName, $pdf);
            readfile(__DIR__ . '/../labels/' . $fileName);
            //ADD CUSTOM TEXT on CUSTOM POSITION
            die();
            return $status;
        } else {
            $status = [
                'success' => false,
                'errors'  => $errors,
            ];

            return $status;
        }
    }

0 likes
1 reply

Please or to participate in this conversation.