如何使用php预定义函数pdf创建html网页的pdf


How to create pdf of html webpage using php predefined function pdf

我有一个用户定义的函数,它会返回html内容。所以我只想把这个html打印成PDF文件,然后直接下载成PDF

提前感谢。。。。

HTML 2 FPDF脚本为我完成了这项工作。http://sourceforge.net/projects/html2fpdf/

// If html2pdf folder placed in root directory
require_once($_SERVER['DOCUMENT_ROOT'] . '/html2pdf/html2fpdf.php');
// Create new HTML2PDF class for an A4 page, measurements in mm
$pdf = new HTML2FPDF('P','mm','A4');
// Optional top margin
$pdf->SetTopMargin(1);
$pdf->AddPage();
// Control the x-y position of the html
$pdf->SetXY(0,0);
$pdf->WriteHTML($html);
// The 'D' arg forces the browser to download the file 
$pdf->Output('MyFile.pdf','D');

请注意,有些元素可能渲染不正确(对我来说是图像),在这种情况下,您必须直接编写这些元素。请参阅fpdf手册。

<?php
    include("MPDF56/mpdf.php");
    $mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->showImageErrors = true;
    $mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list    
    $html='<html><head></head><body>Test</body></html>'; //Assign HTML HERE    
    $cssPath        = base_url('assets/css/voucher.css'); //any CSS File you want to Include

    $stylesheet     =   file_get_contents($cssPath);   
    $mpdf->WriteHTML($stylesheet,1);    // The parameter 1 tells that this is css/style only and no body/html/text
    $mpdf->WriteHTML($html,2);

    if ( $action  ==   'view') 
    {        
        $mpdf->Output('mpdf.pdf','I');           
    }
    else
    {   
        $file_path  =   FTP_PATH."testfile".DIRECTORY_SEPARATOR.$filename.".pdf";    //FTP_PATH = Location where you want to right PDF
        $mpdf->Output( $file_path,'F');      
        file_put_contents(FTP_PATH.'testfile'''.$filename.'.pdf', $output);                 
    }
?>

从这里下载库http://www.mpdf1.com/mpdf/index.php

我想你可以使用这个软件:PDFCrowd,或者在Chrome或FF浏览器中安装一个插件。

另外,您也可以查看:)

  • Htmldoc:请注意,这是2001年在Htmldoc

这个(但不是免费的)PrinceXML:

它绝对是最好的HTML/CSS到PDF转换器,尽管它不是免费的(但嘿,你的编程也不是免费的,所以如果它为你节省了10个小时的工作,你就可以在家自由了。)

哦,是的,我有没有提到这是第一个(可能也是唯一一个)实现完整ACID2的HTML2PDF解决方案!?