以pdf格式保存后数据不存在(fpdf)

时间:2018-01-10 08:04:42

标签: php codeigniter pdf fpdf

我使用fpdf在php(codeigniter)中创建pdf文档。要打印的数据已出现在谷歌浏览器的pdf预览中。但是当数据以pdf文档格式保存时,数据不会出现

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");
}

2 个答案:

答案 0 :(得分:1)

我检查了你的一切都很好:)(google chrome,adobbe reader,foxit reader)。

检查我的代码:

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

        $data = [];
        for ($i = 0; $i <= 3; $i++) {
            for ($i2 = 0; $i2 <= 3; $i2++) {
                $data[$i][$i2] = random_string('alnum', 5);
            }
        }

        $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('https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.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");
    }

答案 1 :(得分:1)

如果您希望在用户可以选择下载文档之前先显示该文档,那么您应该考虑使用GET作为查询字符串而不是POST方法$bulan1&amp; $bulan2

当您从上一页预览pdf文档时,它实际上会发送您的POST变量($bulan1&amp; $bulan2),但是当您保存它时,浏览器只是如果请求中没有POST数据,则可以在保存的文档中看到它遗漏了$bulan1&amp; $bulan2值。