Search code examples
phpcodeigniterpdffpdf

data not exist after saved in pdf format (fpdf)


I use fpdf to create pdf documents in php(codeigniter). the data to be printed has appeared in the pdf preview of google chrome. but when the data has been saved in the pdf document format, the data does not appear

pdf preview in google chrome

in adobe reader this is my code:

public function laporan()
{
    $bulan1 = $this->input->post('bulan1');
    $bulan2 = $this->input->post('bulan2');
    $tanggal = array(
        'tanggal1'=>$bulan1,
        'tanggal2'=>$bulan2
    );

    $query  = "SELECT costumers.nama AS costumer, alats.nama AS alat, DATE(transaksis.tanggal_ambil) AS tanggal_ambil,DATE(transaksis.tanggal_kembali) AS tanggal_kembali FROM costumers,transaksis,alats WHERE costumers.id=transaksis.costumer_id AND transaksis.alat_id=alats.id AND DATE(transaksis.created_at) BETWEEN '$bulan1' AND '$bulan2' ORDER BY transaksis.created_at DESC";
    $data   = $this->UserModel->query_umum($query)->result();
    $header = array(
        array("label"=>"No", "length"=>8, "align"=>"L"),
        array("label"=>"Peminjam", "length"=>50, "align"=>"L"),
        array("label"=>"Nama Alat", "length"=>60, "align"=>"L"),
        array("label"=>"Tanggal Kirim", "length"=>30, "align"=>"L"),
        array("label"=>"Tanggal Pengembalian", "length"=>40, "align"=>"L")
    );

    $this->cetak_laporan($header,$data,$tanggal);
}

public function cetak_laporan($header,$data,$tanggal)
{
    $laporan = new FPDF();
    $laporan->AddPage();
    $laporan->SetFont('Arial','B',12);
    $laporan->Image(base_url('asset/img/logo-dewiratih.png'),5,4,30,10);

    $laporan->SetY(4);
    $laporan->SetX(37);
    $laporan->Cell(100,5, "CV. DEWI RATIH", 0, 1, 'L');
    $laporan->SetFont('Arial','',6);
    $laporan->SetX(37);
    $laporan->Cell(100,2, "CONTRACTOR-SUPLIER-HEAVY EQUIPMENT RENTAL-STONES CRUISER", 0, 1, 'L');
    $laporan->SetX(37);
    $laporan->Cell(100,2, "Jl. Laut Mororejo Kaliwungu Kendal-Jateng 51372", 0, 1, 'L');
    $laporan->SetX(37);
    $laporan->Cell(100,2, "Telp/Fax (024) 8666225, Email : dewiratih_99@yahoo.com", 0, 1, 'L');
    $laporan->SetLineWidth(0.5);
    $laporan->Line(3,16,205,16);

// $pdf->Line(1,3.2,28.5,3.2);
    $laporan->SetLineWidth(0);
    $laporan->SetFont('Arial','B',14);
    $laporan->Cell(0,15, "LAPORAN PEMINJAMAN ALAT", 0, 1, 'C');
    // $pdf->SetFont('Arial','',12);
    // $pdf->Cell(168,5,"Penyewa : ",0,1,'L');
    $laporan->SetFont('Arial','',9);
    // foreach ($tanggal as $tgl) {
        $laporan->Cell(168,5,"Laporan dari tanggal : ".$tanggal['tanggal1']." Sampai ".$tanggal['tanggal2'],0,1,'L');
    // }
    $laporan->SetFont('Arial','B',10);
    foreach ($header as $kolom) {
        $laporan->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], false);
    }
    $laporan->Ln();
    // $fill=false;
    $laporan->SetFont('Arial','',10);
    $no= 0;
    foreach ($data as $baris) {
        $i = 0;
        $laporan->Cell(8, 5, $no+1, 1, '0', $kolom['align'], false);
        foreach ($baris as $cell) {             
            $laporan->Cell($header[$i+1]['length'], 5, $cell, 1, '0', $kolom['align'], false);
            $i++;
        }
        $no++;
        $laporan->Ln();
    }
    // $pdf->Ln();
    $laporan->Cell(25,5,"Keterangan : ",0,0,'L');
    $laporan->Output("Laporan Peminjaman.pdf","I");
}

Solution

  • If you want to display the document first before the user can optionally download it, then you should consider using GET as a query string instead of POST method for $bulan1 & $bulan2.

    When you are previewing your pdf document from previous page, it actually sending your POST variable ($bulan1 & $bulan2), but when you save it, the browser just making a request without the POST data on it, it can be seen on your saved document that it misses the $bulan1 & $bulan2 value.