Dompdf生成pdf并下载到pc但在浏览器上显示错误(Codeigniter)

时间:2016-06-15 12:12:47

标签: php codeigniter dompdf

我正在使用codeigniter并尝试让dompdf工作,当我调用该函数时,它只是将文件下载到桌面上的DOWNLOAD文件夹中,但不会将其输出到屏幕上。

public function generateCreditAggreement(){
        require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');

            $dompdf = new Dompdf();

            $dompdf->load_html('<h1>hello world</h1>');

            // (Optional) Setup the paper size and orientation
            $dompdf->set_paper('A4', 'Potrait');

            // Render the HTML as PDF
            $dompdf->render();

            // Output the generated PDF to Browser
            $dompdf->stream('document.pdf');


    }

2 个答案:

答案 0 :(得分:0)

创建名为的库文件 Pdf.php 把这段代码

<?php defined('BASEPATH') OR exit('No direct script access allowed');


require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';

class Pdf extends DOMPDF
{

protected function ci()
{
    return get_instance();
}


public function load_view($view, $data = array())
{
    $html = $this->ci()->load->view($view, $data, TRUE);

    $this->load_html($html);
}
} ?>

用于在控制器中加载库

function __construct()
{
    parent::__construct();
    $this->load->library("pdf");
}

然后

public function index()
{


        $dompdf = new DOMPDF();

            $html ='
            <html>
            <head>


            </head>

            <body style="font-family:Helvetica;">

                    <table cellpadding="10" cellspacing="0" border="0" width="100%;"  style="border: 1px solid#d8d8d8; line-height:22px; padding:20px; font-size:13px; font-family:Helvetica;">
                    <tr >
                            <td  width="60%;" valign="top">
                               gxcbx</td> 

                        </tr >
                        </td>
                    </table>

            </body>
            </html>';


  $dompdf->load_html($html);
  $dompdf->set_paper('A4', 'Potrait');
  $dompdf->render();

  $dompdf->stream("hello.pdf"); 
  }

试试这种方式。

答案 1 :(得分:0)

Dompdf stream method默认触发文件下载。如果希望PDF显示在浏览器窗口中,请调用以下方法:

$dompdf->stream('document.pdf', array('Attachment'=>0));