来源:http://trac.seagullproject.org/wiki/Integration/PDF/HTML_ToPDF

集成HTML_ToPDF

概述

HTML_ToPDF是用来动态将HTML页面转换成PDF格式输出的一个PHP类。

http://www.rustyparts.com/pdf.php

使用HTML_ToPDF将HTML转换成PDF非常容易 。所以可以使用Flexy创建相应的模板以达到相应的输出格式。

例子

<?php

function _cmd_pdf(&$input, &$output)
{
    SGL::logMessage(null, PEAR_LOG_DEBUG);

    parent::_cmd_view($input, $output);

    if (!isset($language) || empty($language) ) {
        $language = SGL_Translation::getLangID();
    }

    $output->masterTemplate = 'reviewViewPdf.html';

    // Create a unique filename for the resulting PDF
    $linkToPDFFull = $linkToPDF = tempnam(SGL_PATH . '/www/review', 'PDF-');

    // Remove the temporary file it creates
    unlink($linkToPDFFull);

    // Give it an extension
    $linkToPDFFull .= '.pdf';
    $linkToPDF .= '.pdf';

    // Make it web accessible
    $linkToPDF = basename($linkToPDF);

    $output->itemDetails = SGL_Item::getItemDetail(
        $output->leadArticle['reviewItemId'], null, $language, true);

    $view = new SGL_HtmlSimpleView($output);

    $url = SGL_BASE_URL . '/review/' . $linkToPDF;

    // Send the class our HTML and the defaultDomain for images, css, etc.
    $pdf = new HTML_ToPDF($view->render(), '');

    $pdf->setFooter('left', $this->conf['site']['name']);
    $pdf->setFooter('right', '$D');

    // Could turn on debugging to see what exactly is happening
    // (commands being run, images being grabbed, etc.)
    //$pdf->setDebug(true);

    $result = $pdf->convert();
    if (is_a($result, 'HTML_ToPDFException')) {
        SGL::raiseMsg('HTML to PDF error: ' . $result->getMessage());
    }

    copy($result, $linkToPDFFull);
    unlink($result);

    if (file_exists($linkToPDFFull)) {
        header('Location: ' . $url);
    }
}
 
integration/pdf/html_topdf.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2