【问题标题】:How to generate and display TCPDF pdf on a page that has already loaded?如何在已经加载的页面上生成和显示 TCPDF pdf?
【发布时间】:2017-07-21 14:57:52
【问题描述】:

我正在尝试使用 TCPDF 即时生成 PDF 并将其显示在浏览器中。

我已经……

  • 以下载形式输出 PDF
  • 输出不带任何 HTML 的内联 PDF。 (使用$pdf->Output('example_007.pdf', 'I');

我想做的是……

  • 输出带有已打印 HTML 的内联 PDF,而不清除该 HTML。

我很自然地遇到了TCPDF ERROR: Some data has already been output, can't send PDF file 问题。我设法使用 ob 函数消除了这个错误,例如 ob_startob_flush;但是,这会清除我现有的 HTML。

我尝试在通过 AJAX(纯 JavaScript)调用的 PHP 文件上输出 PDF;但是,我对此没有任何运气。什么都没有出现。

那么,当内容已经输出时,如何在浏览器中显示 PDF?

我或多或少在寻找类似以下的东西:

我目前正在使用测试页面执行此操作,这是我的代码:...大部分来自 TCPDF 的示例。

//The following PHP block was tried but also failed as it wiped existing HTML
<?php
    ob_start();
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Test PDF</title>
    </head>
    <body>
        <h1>Before PDF</h1>
        <?php
            error_Reporting(E_ALL);

            date_default_timezone_set("America/New_York");

            // Include the main TCPDF library (search for installation path).
            require_once $_SERVER["DOCUMENT_ROOT"]."/my-site/TCPDF-master/tcpdf.php";

            //ob_start() Tried this at some point... Didn't work

            // create new PDF document
            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Nicola Asuni');
            $pdf->SetTitle('TCPDF Example 001');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            // set default header data
            $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
            $pdf->setFooterData(array(0,64,0), array(0,64,128));

            // set header and footer fonts
            $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
            $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            // set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

            // set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            // set image scale factor
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

            // set some language-dependent strings (optional)
            if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
                require_once(dirname(__FILE__).'/lang/eng.php');
                $pdf->setLanguageArray($l);
            }

            // ---------------------------------------------------------

            // set default font subsetting mode
            $pdf->setFontSubsetting(true);

            // Set font
            // dejavusans is a UTF-8 Unicode font, if you only need to
            // print standard ASCII chars, you can use core fonts like
            // helvetica or times to reduce file size.
            $pdf->SetFont('dejavusans', '', 14, '', true);

            // Add a page
            // This method has several options, check the source code documentation for more information.
            $pdf->AddPage();

            // set text shadow effect
            $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

            // Set some content to print
            $html = "
                <h1>Hello World!</h1>
            ";

            // Print text using writeHTMLCell()
            $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

            // ---------------------------------------------------------

            // Close and output PDF document
            // This method has several options, check the source code documentation for more information.

            //ob_clean(); Various posts suggested various combinations of these methods
            //ob_flush();
            $pdf->Output('example_007.pdf', 'I');
            //ob_end_flush();
            //ob_end_clean();
        ?>
    </body>
</html>

【问题讨论】:

    标签: php html tcpdf


    【解决方案1】:

    使用与内联显示相同的技术,您可以尝试使用 iframe 加载 PDF 文件:

    <html>
        <head>
        </head>
        <body>
            <p>some content</p>
            <iframe src="/path/to/your/pdf/script.php"></iframe>
        </body>
    </html>
    

    【讨论】:

    • 我使用 HTML 对象元素做了很多麻烦事,尽管我可以尝试使用 Iframe。它需要一些时间来解决它。
    • @gp_sflover 这更像是一个问题形式的答案,而不是澄清的请求。但是感谢您的澄清
    • +1 和一个想象中的蛋糕。我尝试使用对象元素;但是,我从来没有想过将 PDF 代码放在另一个文件中,然后将 src /data 属性指向该文件。该方法似乎有效。
    • 这通常是最简单的解决方案 :)
    • @ClaudioPinto 初始格式(无代码)似乎只是对 OP 的请求。无论如何,您应该始终避免在答案中使用问号:-)。
    猜你喜欢
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    相关资源
    最近更新 更多