Skip to main content

Text im PDF andrucken

Um z.B. den Barcode-Wert auf dem PDF anzudrucken, kann die der UserExit BeforeExport wie folgt angepasst werden:

Voraussetzung:
Squeeze Version: >=1.11.0

<?php

use Squeeze\ImageTools;
use Squeeze\SqueezeConfig;
use Squeeze\xDoc;
use Squeeze\xDocFolders;
use Squeeze\xReturnObject;
use Squeeze\xTools;

/**
 * @param xDoc $doc
 * @return xReturnObject
 */
function BeforeExport(xDoc $doc): xReturnObject
{
    try {
        $dirSep = DIRECTORY_SEPARATOR;
        $config = new SqueezeConfig();
        $repo = $config->get('repository.root');
        $absolutePath = xTools::buildAbsolutePath($repo, $doc->repoPath, false);

        $inputDir = $absolutePath . xDocFolders::INPUT . $dirSep;
        $workDir = $absolutePath . xDocFolders::WORK . $dirSep;

        $printValue = $doc->getFieldByName('DocumentReference')->getValue()->value;

        $files = scandir($inputDir);
        foreach ($files as $file) {
            $extension = strtolower(pathinfo($inputDir . $file, PATHINFO_EXTENSION));
            if ($extension == "pdf") {

                //if($file == "_result.pdf") continue;

                $pdfInfo = ImageTools::getPdfInfo($inputDir . $file, 1, 1);
                $pdfWidth = $pdfInfo['pages'][1]['width'] / 300 * 25.4;
                $pdfHeight = $pdfInfo['pages'][1]['height'] / 300 * 25.4;
                $pdf = ImageTools::pdfPrintText($pdfWidth, $pdfHeight, $printValue, 1, 1, 'Courier', 10, 4, true);
                file_put_contents($workDir . 'textlayer.pdf', $pdf);

                $overlayResult = ImageTools::addLayerToPdf($inputDir . $file, $workDir . 'textlayer.pdf');
                if ($overlayResult !== true) {
                    unlink($workDir . 'textlayer.pdf');
                    throw new Exception('Error while adding a PDF Layer');
                }
                unlink($workDir . 'textlayer.pdf');
            }
        }

        return new xReturnObject(true, 200, 'UserExit BeforeExport successful', null);
    } catch (Exception $e) {
        $logger = Logger::getLogger("main");
        $logger->error($e->getMessage());
        return new xReturnObject(false, 500, $e->getMessage(), null);
    }
}