简体   繁体   中英

Generating a PDF using CodeIgniter

I am using dompdf to create a pdf file out of an html file that gets created on-the-fly for the sole purpose of it serving as input for the pdf generator, however I am having trouble doing this, I implemented the code in this thread and everything works fine (I could output a simple pdf) however when I try to give it a more specific url I get this error:

An Error Was Encountered Unable to load the requested file

here's the code that has the problem:

function printPDF(){

            //write_file() usa un helper (file)
            $this->load->library('table');
            $this->load->plugin('to_pdf');
             // page info here, db calls, etc.
             $query = $this->db->get('producto');
             $data['table'] = $this->table->generate($query);
             $path_url = base_url().'print/existencias.html';
             write_file($path_url, $data['table']);
             $html = $this->load->view($path_url, 'consulta', true);
             pdf_create($html, 'consulta');
        }

you should use tcpdf to create a pdf. //create controller for example :

public function create_pdf() 
    {
     $this->load->library("Pdf");
     $data['results'] = // your data
     $this->load->view('pdfview',$data);

    }


//pdfview is the page having tcpdf code and your pdf code.

Not sure about the exact problem, but please check this:

1) as stated in CI's manual, load->view's second parameter should be an associative array or an objet, translated to vars via extract. That may generate some problem generating $html.

2) try making $path_url relative to application/views directory, as read in CI's manual.

You can try it like this

public function export_pdf() {
    $filename = 'FILENAME.pdf';
    header("Content-Description: Advertise Report");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/pdf; ");
    $file = fopen('php://output', 'w');
    $header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
    fputpdf($file, $header);
    fputpdf($file, 'YOUR DATA');
    fclose($file);
    exit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM