【问题标题】:Downloading a pdf file by converting html using php通过使用php转换html来下载pdf文件
【发布时间】:2014-03-21 09:09:39
【问题描述】:

我正在尝试制作一个电子商务网站,用户将在其中单击链接并制作发票,并应在客户端下载我在 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显示格式已损坏,该怎么办,我必须尽快完成:((

【问题讨论】:

  • 你应该使用像 mpdf 或 TCPDF 这样的 pdf 库
  • 我也试过了,但没用:(
  • 什么不起作用?你试过什么?我们可以看看一些代码吗?
  • 顺便说一句,因为你使用 codeigniter 看到这个页面:github.com/EllisLab/CodeIgniter/wiki/…
  • 我遵循了上述文章中的所有内容,但是下载了一个 pdf 文件,但是当我打开它时说它已损坏:(

标签: php html codeigniter pdf


【解决方案1】:
<?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

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 2012-05-05
    • 2012-02-11
    • 2013-05-27
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多