• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

TCPDF - Bilder in das PDF

sveka

Mitglied
Hallo,

ich habe ein kleines Problem. Ich erstelle PDFs in PHP mit TCPDF. Das klappt auch alles super. Nun möchte ich Bilder aus vorhandenen BASE64-Strings in das PDF einbetten. Dies funktioniert lokal getestet super ... online leider gar nicht. Fehlermeldung keine vorhanden.

Mein Code sieht so aus. Grundlage ist ein Text mit Platzhaltern....

PHP:
[...]
// Bild in temporäres Bild umwandeln
// Base64-kodierte Bilddaten
$custom_temp_dir = '../temp/';
$prefix = 'prefix';

$img_base64_encoded = $unterschrift;
$imageContent = file_get_contents($img_base64_encoded);
$path = tempnam($custom_temp_dir , $prefix);
file_put_contents ($path, $imageContent);

$img = '<br /><img src="' . $path . '" width="150px" height="50px"><br /><i>Name</i>';

// BASE64 Stempel
$prefix_stempel = 'prefix_stempel';

$img_base64_encoded_stempel = $stempel;
$imageContent_stempel = file_get_contents($img_base64_encoded_stempel);
$path_stempel = tempnam($custom_temp_dir , $prefix_stempel);
file_put_contents ($path_stempel, $imageContent_stempel);

$unterschrift = '<br /><img src="'.$path_stempel.'" width="150px" height="50px"><br /><i></i>';

$platzhalter = array(
    '##UD##' => $img,  
    '##U##' => $unterschrift,
);

$replacedText = str_replace(array_keys($platzhalter), array_values($platzhalter), $vertragstext);

$pdf = new MYPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->init();
$pdf->SetProtection(array('modify' , 'copy'), '', null, 0, null);
$pdf->create_pdf($zielOrdner, $zielDatei, $replacedText);
unset($pdf);
[...]

Die PDF-Erstellung sieht so aus ...


PHP:
class MYPDF extends TCPDF
{
    // Init
    public function init()
    {
        $this->SetCreator(PDF_CREATOR);    
        $this->SetAuthor('Bildung mit Service UG');
        $this->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
        $this->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $this->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $this->SetDefaultMonospacedFont('helvetica');
        $this->SetFooterMargin(PDF_MARGIN_FOOTER);
        $this->SetMargins(PDF_MARGIN_LEFT, '5', '25', true);
        $this->setPrintHeader(false);
        $this->setPrintFooter(true);
        $this->SetAutoPageBreak(TRUE, 10);
        $this->SetFont('helvetica', '', 10);
    }

    // Page footer
    public function Footer()
    {
        $heute = new DateTime();
        $h = $heute->format('d.m.Y - H:i');

        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Bildung mit Service - www.bildung-service.com - Honorarvertrag - ' . $h . ' Uhr - Seite '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }

    public function create_pdf($userpfad, $neuerDateiname, $output)
    {
        $this->AddPage();
        $this->SetTitle($neuerDateiname);
        $this->writeHTML($output);      
        $this->Output($userpfad . '/' . $neuerDateiname , 'F');
    }
}

Vielleicht habt ihr einen Ansatz für mich ... DANKE
 
Werbung:
Wenn ich dein PHP richtig verstehe, erzeugst Du aus dem Base64 eine Bilddatei und verwendest diese, um sie in das PDF einzufügen.
Möglicher Weise ist dieser Umweg gar nicht nötig, schau mal hier:
Demnach kann man ein Bild direkt aus einem Base64-String einfügen.
 
Werbung:
Die PHP-Version ist es nicht. Ist identisch. Ich habe einen extra Ordner angelegt mit allen erforderlichen Rechten ... Ich werde das obige auf alle Fälle mal testen.
 
Zurück
Oben