Search code examples
phpsyntax-errorfpdf

FPDF doesn't work


I have project to generate pdf file using FPDF library, but I got error like this.

Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file (output started at C:\xampp\htdocs\SIPP-Litbang\admin.php:113)' in C:\xampp\htdocs\SIPP-Litbang\FPDF\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\SIPP-Litbang\FPDF\fpdf.php(1052): FPDF->Error('Some data has a...') #1 C:\xampp\htdocs\SIPP-Litbang\FPDF\fpdf.php(999): FPDF->_checkoutput() #2 C:\xampp\htdocs\SIPP-Litbang\tampilan-admin\lembar_pengesahan.php(11): FPDF->Output() #3 C:\xampp\htdocs\SIPP-Litbang\tampilan-admin\permintaanproposal.php(7): include('C:\xampp\htdocs...') #4 C:\xampp\htdocs\SIPP-Litbang\admin.php(145): include('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\SIPP-Litbang\FPDF\fpdf.php on line 271

And here are the codes:

<?php
ob_start();
require __DIR__ . '/../FPDF/fpdf.php';
    $pdf = new FPDF('P', 'mm', 'A4');
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->Write('20', 'Hello World!');
    $pdf->Ln();
    $pdf->Write('0', 'Project Pertama Menggunakan FPDF');
    ob_end_clean();
    $pdf->Output();
?>

and top of admin.php codes like this:

<?php

        session_start();
        require_once "class.admin.php";

        $admin = new ADMIN();

        $id_admin = $_SESSION['adminSession'];
        $level = $_SESSION['levelSession'];

        $query = $admin->runQuery("SELECT * FROM admin WHERE id_admin = :id_admin AND level = :level");
        $query->bindParam(":id_admin", $id_admin);
        $query->bindParam(":level", $level);
        $query->execute();

        $row = $query->fetch(PDO::FETCH_ASSOC);



?>
<!DOCTYPE html>
<html>
<body>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SIPP-Litbang</title>

    <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css'>
    <link href="tampilan-admin/css/bootstrap.min.css" rel="stylesheet">
    <link href="tampilan-admin/css/font-awesome.min.css" rel="stylesheet">
    <link href="tampilan-admin/css/datepicker3.css" rel="stylesheet">
    <link type="text/css" href="tampilan-admin/css/styles.css" rel="stylesheet">
    <link  href="tampilan-admin/dataTables/dataTables.bootstrap.css" rel="stylesheet" />

    <!--Custom Font-->
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="tampilan-admin/js/html5shiv.js"></script>
    <script src="tampilan-admin/js/respond.min.js"></script>
    <![endif]-->
</head>

output started at admin.php is line 113

 <div class="profile-usertitle-name"><?php echo $row['nama_admin']?></div>

what's wrong with my codes?


Solution

  • Currently you're trying to output the PDF inside the HTML page.

    You need to split this into two parts:

    • HTML page links to URL that generates PDF
    • PDF creation URL only outputs PDF without any HTML