solmaz babakan
solmaz babakan

Reputation: 13

replace TCPDF logo with costum

in codeigniter 3 i have a controller method to create pdf from html content using TCPDF as follow;

public function generatePDF_correct_numbering1() {
    
    // Initialize TCPDF
    $pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);

    // Set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Your Name');
    $pdf->SetTitle('Generated PDF');
    $pdf->SetSubject('PDF with TOC');
    $pdf->SetKeywords('TCPDF, PDF, CodeIgniter, TOC');
    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    // Add a cover page or title page
    $pdf->AddPage();
    $pdf->SetFont('helvetica', 'B', 16);
    $pdf->Cell(0, 10, 'Generated PDF with Table of Contents', 0, 1, 'C');

    // Placeholder for the Table of Contents
    $pdf->SetFont('helvetica', '', 12);
    $pdf->AddPage();
    $pdf->Cell(0, 10, 'Table of Contents', 0, 1, 'C');
    $tocPage = $pdf->getPage();

    // Content structure
    $formattedData = [
        'Kullanici Profilleri' => [
            'profil_yonetimi_cover1' => 'Content for contents section.',
            'profil_yonetimi_contents' => 'Content for contents section.',
        ],
        'Kullanici Profil Sirketleri' => [
            'profil_yonetimi_company_governing_body' => 'Content for contents section.',
            'profil_yonetimi_company_natural_capital_accounting' => 'Content for natural capital accounting.',
        ],
        'Kullanici Profillerirwrw' => [
            'profil_yonetimi_cover2' => 'Content for cover 1.',
            'profil_yonetimi_parts' => 'Content for contents section.',
        ],
    ];
    // Prepare the Table of Contents entries
    $tocEntries = [];

    // Generate the content
    foreach ($formattedData as $section => $subContent) {
        // Start a new section
        $pdf->AddPage();
        $sectionPage = $pdf->getPage(); // Record starting page for TOC
        $pdf->SetFont('helvetica', 'B', 14);
        $pdf->Write(10, $section);
        $pdf->Ln(10);

        // Save TOC entry for the section
        $tocEntries[] = ['title' => $section, 'page' => $sectionPage];

        // Add subsection content
        foreach ($subContent as $subtitle => $content) {
            // Add subtitle page
            $pdf->AddPage();
            $subtitlePage = $pdf->getPage(); // Record page for the subtitle

            $pdf->SetFont('helvetica', 'I', 12);
            $pdf->Write(8, $subtitle);
            $pdf->Ln(5);

            $pdf->SetFont('helvetica', '', 10);
            $pdf->WriteHTMLCell(0, 0, '', '', $content, 0, 1, false, true, 'L', true);
            $pdf->Ln(5);

            // Save TOC entry for the subtitle
            $tocEntries[] = ['title' => $subtitle, 'page' => $subtitlePage];
        }
    }

    // Go back to TOC page
    $pdf->setPage($tocPage);
    $pdf->SetFont('helvetica', '', 12);

    // Add TOC entries
    foreach ($tocEntries as $entry) {
        $pdf->Cell(0, 10, $entry['title'] . ' ........ ' . $entry['page'], 0, 1, 'L');
    }

    // Output the PDF
    $pdf->Output('generated_pdf_with_toc.pdf', 'I');
}

i want to add my cstum logo as header instead of default tcpdf logo;

i create my logo as follow:

$logo = $this->db->select('profil_resmi')->from('kullanici_profilleri')->where('id', 89)->get()->row();
$full_logo_path = $logo->profil_resmi ? FCPATH . 'assets/image/' . $logo->profil_resmi : FCPATH . 'assets/image/defult_logo-organization.png';

$logo_base64 = 'data:image/jpeg;base64,' . base64_encode(file_get_contents($full_logo_path));
        

how should i replace my logo on pdf?

i can not apply the approach in TCPDF Example 003

i need to create pdf in my own controller not a new controller MYPDF as described in this example.

how should i define headers in my controller to apply costum logo to reports.

thanks in advance.

Upvotes: 0

Views: 20

Answers (0)

Related Questions