使用php转换html下载pdf文件


Downloading a pdf file by converting html using php

我想做一个电子商务网站,在那里用户会点击链接,它会使发票,应该在客户端下载我在html中创建的发票的pdf文件,事情是我使用这个代码,它与ms word作为。doc文档工作,但我想做同样的pdf不工作,代码粘贴在下面 // the code to download invoice as word document, it's working <?php header('Content-type: application/vnd.ms-word'); header('Content-Disposition: attachment;Filename='.$result->tracking_number.'.doc'); ?> <!DOCTYPE html> <html> <head>

,但同样的代码是不工作的PDF:(

<?php
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment;Filename='.$result->tracking_number.'.pdf');
?>
<!DOCTYPE html>
<html>
<head>

下载的PDF说格式损坏,怎么办,我必须尽快完成这个:(

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.".pdf");
    } else {
        return $dompdf->output();
    }
}
?>
4) use it in your controllers like this
<?php
function pdf()
{
     $this->load->helper(array('dompdf', 'file'));
     // page info here, db calls, etc.     
     $html = $this->load->view('controller/viewfile', $data, true);
     pdf_create($html, 'filename');
     or
     $data = pdf_create($html, '', false);
     write_file('name', $data);
     //if you want to write it to disk and/or send it as an attachment    
}
?>
https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf