简体   繁体   English

通过使用php转换html下载pdf文件

[英]Downloading a pdf file by converting html using php

I'm trying to make an e-commerce website, where the user will click on the link and it will make an invoice and should download on client end a pdf file of the invoice which i've created in html, the thing is i used this code and it worked with ms word as .doc document but i want to do the same with pdf not working, code pasted below // 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> 我正在尝试建立一个电子商务网站,用户将在该链接上单击并创建发票,并应在客户端下载我在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> // 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>

but same with this code is not working for pdf :( 但与此代码相同不适用于pdf :(

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

the pdf downloaded says the format is corrupted, what to do, i've to finish this asap :(( 下载的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 https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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